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 :)
hi, zetain, for a single channel img, access will be :
binaryImage.at<uchar>(x,y) == 255
in general, the type whithin the at<T> method has to match the image type
again, here's a short translation table:
.. you'll get the picture !
ahhh! I am such an idiot. Thank you very much :)
mh ... I tried what you have proposed but it is still giving me the same error. My program runs perfectly when i use this line of code instead but is really slow:
so the rest of my program is not the problem. I also have checked if the picture has the amount of channels and the expected depth of 8Bit.
hehe, row(y) will return a whole row, also col(i). [that#s a lot of copying] . you want:
binaryImage.at<uchar>(y,x) == 255;