Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Using Mat::at with single channel image

Hello,

i am trying to access the pixels of a single channel grayscale image. I am using this code:

for( int x = 1; x < binaryImage.cols; x++ )
{ 
for( int y = 1; y < binaryImage.rows; y++ )
    { 
     if(binaryImage.at<Vec3b>(x,y)[0] == 255)
     {
        redPixelsInCol[x] += 1;
        totalRedPixels++;
     }
     redCol[x] = redPixelsInCol[x] * x;
     }
 }

I get an error in VisualStudio telling me that i try to access memory i am forbidden to access. I can imagine that this is caused by the datatype Vec3b which forces the at-method to read 3 values every iteration. Due to the fact that a grayscale image only has one channel there aren't 3 values left at the end of the iteration so breaking the size of the mat object.

I am forced to specify the datatype in which the pixel value is returned but this must be an array or a pointer. Which type should i give the at-method for a single channel image? Or is there another reason causing this code to break?

thank you for your help :)