Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I'm going to put my StephenPuttemans hat and ask, why not use the more modern and easier to use C++ interface?

The C++ interface has a very handy function: at(), which allows for random access of pixel values in a Mat object, the OpenCV image storage structure. Example: if you have a grayscale image (meaning the stored elements are chars), you can access the values like so:

image.at<char>(49,39);

The code above accesses the char element of the object 'image' at row 49, column 39.

That said, the above method is best for random access. The most efficient way to loop over the Mat object is pointer access, and you can access the pointer with the Mat member function ptr(). But dealing with pointers can be hairy, so you can alternatively use MatIterator_ to access the elements. See this tutorial for a better breakdown of the different methods of pixel access.

HOWEVER IF YOU INSIST ON USING THE C INTERFACE, the C interface provides a macro: CV_IMAGE_ELEM(). And you can also use pointer access like before.

I'm going to put on my StephenPuttemans hat and ask, why not use the more modern and easier to use C++ interface?

The C++ interface has a very handy function: at(), which allows for random access of pixel values in a Mat object, the OpenCV image storage structure. Example: if you have a grayscale image (meaning the stored elements are chars), you can access the values like so:

image.at<char>(49,39);

The code above accesses the char element of the object 'image' at row 49, column 39.

That said, the above method is best for random access. The most efficient way to loop over the Mat object is pointer access, and you can access the pointer with the Mat member function ptr(). But dealing with pointers can be hairy, so you can alternatively use MatIterator_ to access the elements. See this tutorial for a better breakdown of the different methods of pixel access.

HOWEVER IF YOU INSIST ON USING THE C INTERFACE, the C interface provides a macro: CV_IMAGE_ELEM(). And you can also use pointer access like before.