Ask Your Question

Indexing beyond size of Mat still gives a result

asked 2014-07-24 03:36:22 -0500

I have loaded a jpg image that is 480 columns by 640 rows into a Mat and I want to return the pixel value at a certain position. I load a colour image and then I convert it to greyscale. Obviously I want to return pixel values between columns 0 to 479 and rows 0 to 639 but if I use an index of say (500,500) then I still get an answer without any crash or error. In fact an index up to (0,622575) works or even (1,622095), basically 622575 minus the length of 1 row (480). How is is it possible to index beyond the size of the Mat ? I am running this in Xcode 5.5.1 (debug mode) and I am using OpenCV 2.4.9 installed via MacPorts.

Mat colourImage = imread("/Path_to/a.jpg");

Mat greyImage;

int rowpos = 500;

int colpos = 500;


uchar gg =< uchar>(rowpos,colpos);

cout << "pixel value " << (int)gg << endl;

edit retag flag offensive close merge delete



if you run a debug build, you should get an exception.

berak gravatar imageberak ( 2014-07-24 03:38:29 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2014-07-24 04:02:36 -0500

Your a lucky guy (or not… because it could crash at any time…). As @berak says, in Debug, you should get an exception as bounds are checked. It's your job, as a developer to checks the bounds of images you are scanning. The bounds are not checked in release, and if you want to be sure that there is not out-of-bound access, and obviously don't want to use the rows and cols members, have a look at the iterator (begin and end) of the cv::Mat class. They are similar to STL iterator.

If you are interested in the many ways you could use to scan your image, have a look at that tutorial that will show you most of the common way to scan an image, and highlight the performance you could expect for each of them.

edit flag offensive delete link more



Thanks berak and Mathieu. I am in debug build and it was checking the bounds of my arrays that lead me to this short piece of test code. By doing a bit more googling I found that my version of OpenCV from MacPorts may have been compiled without debug symbols and by luck and luck alone it does not crash!

I will try my own build using the source code form the OpenCV website

eugclark gravatar imageeugclark ( 2014-07-24 05:25:23 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2014-07-24 03:36:22 -0500

Seen: 85 times

Last updated: Jul 24 '14