Ask Your Question
0

Mat::at throws error: Assertion failed ((unsigned)i0 < (unsigned)size.p[0]) in opencv\build\include\opencv2\core\mat.inl.hpp, line 1100

asked 2018-10-24 14:22:22 -0500

PerhapsS44 gravatar image

Hello guys! I am new to OpenCV and I have tried to write a program that takes as input the images my camera captures live and modify some pixels. Right now I was trying to invert the values of a grayscale image when this error appeared. I think I got sth wrong because I have used a line of code before and it worked just fine. Can you please help me out?

Here is the code:

    while(1){
    VideoCapture cap(0);
    if (!cap.isOpened())
       return 1;
    Mat src,dst,gray;
    cap.read(src);
    cvtColor(src, gray, COLOR_BGR2GRAY);
    dst = gray;

    for (int y = 0; y < dst.cols; y++) {
        for (int x = 0; x < dst.rows; x++) {
            dst.at<uchar>(y, x) = 255 - dst.at<uchar>(y, x);
        }
    }
    if (waitKey(30) == 's')
        break;
    }

I have used the line: "dst.at<uchar>(y, x) = 255 - dst.at<uchar>(y, x);" before and it worked just fine. What could be wrong?

Error: Assertion failed ((unsigned)i0 < (unsigned)size.p[0]) in cv::Mat::at, file ..\opencv\build\include\opencv2\core\mat.inl.hpp, line 1100

edit retag flag offensive close merge delete

Comments

1

dst.at<uchar>(row,col)

and why do you open VideoCapture in while loop?

Don't loop over pixels

 for (int y = 0; y < dst.cols; y++) {
        for (int x = 0; x < dst.rows; x++) {
            dst.at<uchar>(y, x) = 255 - dst.at<uchar>(y, x);
        }
    }

becomes dst =255-dst

LBerger gravatar imageLBerger ( 2018-10-24 14:28:40 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-10-24 18:42:03 -0500

berak gravatar image

What could be wrong?

the whole "write per-pixel loops" approach !

you're still confusing rows and cols.

for (int y = 0; y < dst.cols; y++) // no. y goes from 0 to dst.**rows** !

please use bitwise_not instead, or it's short form (an operator):

dst = ~dst;

or, like mentioned before:

dst = 255 - dst;

also, yes, the:

VideoCapture cap(0);
if (!cap.isOpened())
   return 1;

part must go before starting the

while(1) {

loop.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-10-24 14:22:22 -0500

Seen: 341 times

Last updated: Oct 24 '18