Ask Your Question
0

assertion Failed in Mat::at

asked 2016-06-26 06:58:44 -0600

If I try to execute this code:

image = imread(s);
GrayImage.create(Size(image.cols, image.rows), CV_8UC1);
    for (int st = 0; st < image.cols * image.rows * 3; st += 3)
           GrayImage.at<unsigned char>(st / 3) = g.getValue(BGR(image.at<unsigned char>(st),
                                    image.at<unsigned char>(st + 1), image.at<unsigned char>(st)));

I get the error:

OpenCV Error: Assertion failed (elemSize() == (((((DataType<_Tp>::type) & ((512 - 1) << 3)) >> 3) + 1) << ((((sizeof(size_t)/4+1)16384|0x3a50) >> ((DataType<_Tp>::type) & ((1 << 3) - 1))2) & 3))) in cv::Mat::at

(I debugged it the error occurs when calling GrayImage.at[...]) Does somebody know what this error means and how to fix it? I googled it already, but there wasn't a working solution until now.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-06-27 02:31:43 -0600

berak gravatar image

first - try to avoid per-pixel loops. opencv is a highly optimized vector/matrix library. kittens will be killed, whenever you attempt something like above. it could be a plain:

Mat image = imread(s);
Mat gray; // NO preallocation.

cvtColor(image, gray, COLOR_BGR2GRAY);
// done. 
// gray holds grayscale image of original size.

then : almost anything in your loop construct is illegal. don't try to be smart there, you have to play by the rules first !

for (int r=0; r<image.rows; r++)
for (int c=0; c<image.cols; c++)
{
    Vec3b pix = image.at<Vec3b>(r,c); // row,col ! correct type !
    gray.at<uchar>(r,c) = some_operation_with(pix);  
}

please have a look at the most basic tutorials

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-06-26 06:58:44 -0600

Seen: 2,420 times

Last updated: Jun 27 '16