Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV: get image data as array

Tags:

opencv

I am beginning to learn OpenCV and am having a bit of trouble with some basic operations. So, I can read a video file as follows:

cv::VideoCapture cap("media.avi");
cv::Mat imgNew;
while (cap.read(imgNew)) {
}

Now, I have another function that has the following signature:

template<class T> 
bool get_estimate(const T * image_data, int num_pixels)

Now, this takes the image data as this linear array of type const T where T in this case could be unsigned char and the num_pixels is the number of pixels in the image.

Can someone tell me how I can use this cv::Mat type with this method. I am hoping there is some easy way to get the underlying array and its type without copying the data into a temporary array but I am not sure. I can always assume that the video image being read is always converted to grayscale.

like image 690
Luca Avatar asked Jul 08 '26 23:07

Luca


1 Answers

The Mat class in OpenCV has a uchar *data member which can be used to access the underlying data. OpenCV Mat.

Mat myImg;                       //assume single channel 2D matrix.
unsigned char *p;
p = myImg.data;
for( unsigned int i=0; i< myImg.cols*myImg.rows; i++ )
    std::cout<< p[i];

Cast p[i] to suitable type.

like image 109
a-Jays Avatar answered Jul 14 '26 04:07

a-Jays



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!