简体   繁体   中英

OpenCV Error Assertion failed on some Pixal Values

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...

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM