3

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.

2
  • Is this similar? stackoverflow.com/questions/20980723/… Commented Oct 6, 2014 at 5:26
  • The problem is that the matrix to array conversion is with copying. I was wondering if there is a way to do it without copying.. Commented Oct 6, 2014 at 7:53

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is not correct . If the image is packed (has extra data at the end of each row) then the size will be smaller than the actual data size. In the above example this would just mean that not all the data is printed; but if you tried to allocate a buffer using the code above, the buffer would be too small. For a correct answer see stackoverflow.com/a/26441073/638048.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.