0
votes

I loaded an Image to a Mat:

Mat Mask = cvLoadImage(filename);

Its an 3744 X 5616 RGB Image. On the next Step i convert it to an Grayscale.

cvtColor(Mask,Mask,CV_BGR2GRAY);

after this i normalize it to use the full Grayscale later:

normalize(Mask,Mask,0,255,NORM_MINMAX,CV_8U);

Now i need the specific Grayscale values and getting an Error on some Values:

for(int i=0;i<(Picture.rows);i++)
{
    for(int j=0;j<(Picture.cols);j++)
    {
Vec3b  masked = Mask.at<Vec3b>(i,j);
//some stuff
}
}

I'm getting the Following Error on some Pixels:

OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (unsigned)i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in unknown function, file c:\opencv\build\include\opencv2\core\mat.hpp, line 537

Anyone can tell me what i did wrong? It's strange that it appears only on some Pixel Values

Edit: Additional Information: If I load my Mask as Grayscale everything works fine. But when I use cvtColor() or Mat Mask = imread(filename,CV_LOAD_IMAGE_GRAYSCALE); on the image the error appears. Very Strange...

1
wild guess - are those min/max pixels in the original image? - KobeJohn
Sadly not. This was my first guess too. In the original Image when the error on the first line appears the Values on these are around 196-200 and normalized around 211-216. The max on the original is 237 and normalized 255. - David_D

1 Answers

2
votes

I think your problem is you are accessing a binary image with .at<Vec3b>(i,j). Instead you want to access each pixel with .at<uchar>(i,j). cvtColor(Mask,Mask,CV_BGR2GRAY); changes the 3 channel BGR image to a one channel grayscale image. .at<Vec3b>(i,j) is trying to access a 3 channel image which will eventually go past the end of the image array in memory causing problems or tripping those assertions.

The inner part of your for loop should look like this:

unsigned char masked = Mask.at<uchar>(i,j);