OpenCV Error: Assertion failed, mat.cpp line 537

2020-04-12 07:16发布

http://pastebin.com/5ZeMvm2C is my header file in my project.

There are skeleton.at(yaxis,xaxis+1) at line 249. When i type this code in my project i got this error:

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

// mat.cpp line 537 is:
    template<typename _Tp> inline _Tp& Mat::at(int i0, int i1)
        {
            CV_DbgAssert( dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] &&
                (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) &&
                CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1());
            return ((_Tp*)(data + step.p[0]*i0))[i1];
        }

What's wrong?

http://pastebin.com/gqJ5RpBU is also my .cpp file.

2条回答
女痞
2楼-- · 2020-04-12 07:46

Mat::at() method has been implemented as a template, you must know the type of image before you used the function.

check the channels of the image. for single channel image(8UC1), you should manipulate the image pixels as in:

image.at<uchar>(row, col) = 255; 

for three channel color image(8UC3), you should use the function as in:

image.at<cv::Vec3b>(row, col)[channel] = 255;

if the channel is no problem, you should check the arguments of the at(i, j), i present the row, j present the col. in other word, i equal to Point.y, j equal to Point.x.

查看更多
干净又极端
3楼-- · 2020-04-12 07:52

As the error message says, you have an OpenCV runtime assertion that is failed.

As you wrote in your question, the failed assertion is inside the Mat::at function.

You have to find in your code the call (or the calls) to Mat::at that give you the error.

As you can see at the OpenCV help page Mat::at is a template function with one, two or three arguments, the failure in the assertion can have various causes:

  1. you are using the wrong template parameter (see for example Using Mat::at(i,j) in opencv for a 2-D Mat object)
  2. the arguments are wrong, for example in a call to template<typename T> T& Mat::at(int i, int j), i is supposed to be between 0 and the number of rows minus one, j is supposed to be between 0 and the number of column minus one. If you have an image with 100 rows and you ask for an element at row 101 the assertion will fail. Off-by-one errors are common in this case.

To be more specific, the assertion failed because at least one of the following bools is false:

  1. dims <= 2
  2. data
  3. (unsigned)i0 < (unsigned)size.p[0]
  4. (unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels())
  5. CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1())

The above bools are meaningful inside the scope of Mat class.

Furthermore please note that help says that:

For the sake of higher performance, the index range checks are only performed in the Debug configuration

and so in your Release configuration you will not have the failed assertion but probably a crash somewhere.

From the source you linked, it seems to me that you are on Windows, if that is true and if you have Visual Studio, I suggest you to build OpenCV from the source code, to put a breakpoint inside Mat::at and then to debug your code in order to see what of the previous bools is false.

查看更多
登录 后发表回答