C++ OpenCV 错误:断言失败,mat.cpp 行 537
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20217044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV Error: Assertion failed, mat.cpp line 537
提问by user2923864
http://pastebin.com/5ZeMvm2Cis my header file in my project.
http://pastebin.com/5ZeMvm2C是我项目中的头文件。
There are skeleton.at(yaxis,xaxis+1) at line 249. When i type this code in my project i got this error:
第 249 行有 skeleton.at(yaxis,xaxis+1)。当我在项目中键入此代码时,出现此错误:
**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/gqJ5RpBUis also my .cpp file.
http://pastebin.com/gqJ5RpBU也是我的 .cpp 文件。
回答by Alessandro Jacopson
As the error message says, you have an OpenCV runtime assertion that is failed.
正如错误消息所说,您有一个失败的 OpenCV 运行时断言。
As you wrote in your question, the failed assertion is inside the Mat::at
function.
正如您在问题中所写,失败的断言在Mat::at
函数内部。
You have to find in your code the call (or the calls) to Mat::at
that give you the error.
你必须在你的代码中找到Mat::at
给你错误的调用(或调用)。
As you can see at the OpenCV help pageMat::at
is a template function with one, two or three arguments, the failure in the assertion can have various causes:
正如您在OpenCV 帮助页面上看到的那样,Mat::at
是一个带有一个、两个或三个参数的模板函数,断言失败可能有多种原因:
- you are using the wrong template parameter (see for example Using Mat::at(i,j) in opencv for a 2-D Mat object)
- 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 between0
and the number of rows minus one,j
is supposed to be between0
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 errorsare common in this case.
- 您使用了错误的模板参数(例如,请参阅在 opencv 中使用 Mat::at(i,j) for a 2-D Mat object)
- 参数是错误的,例如在对 的调用中
template<typename T> T& Mat::at(int i, int j)
,i
应该0
在行数减一之间,j
应该在0
列数减一之间。如果您有一个包含 100 行的图像并且您在第 101 行要求一个元素,则断言将失败。在这种情况下,逐一错误很常见。
To be more specific, the assertion failed because at least one of the following bool
s is false
:
更具体地说,断言失败的原因至少是以下bool
s 之一false
:
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())
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())
The above bool
s are meaningful inside the scope of Mat
class.
上述bool
s 在Mat
类的范围内是有意义的。
Furthermore please note that help says that:
此外请注意,帮助说明:
For the sake of higher performance, the index range checks are only performed in the Debug configuration
为了更高的性能,索引范围检查只在Debug配置中进行
and so in your Release configuration you will not have the failed assertion but probably a crash somewhere.
所以在你的 Release 配置中,你不会有失败的断言,但可能会在某处崩溃。
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 bool
s is false
.
从你链接的源代码来看,在我看来你是在 Windows 上,如果这是真的,如果你有 Visual Studio,我建议你从源代码构建 OpenCV,在里面放一个断点Mat::at
,然后调试你的代码为了看看前面的bool
s 是什么false
。
回答by oscarwin
Mat::at()
method has been implemented as a template, you must know the type of image before you used the function.
Mat::at()
方法已经实现为模板,在使用该函数之前,您必须知道图像的类型。
check the channels of the image. for single channel image(8UC1), you should manipulate the image pixels as in:
检查图像的通道。对于单通道图像(8UC1),您应该像这样操作图像像素:
image.at<uchar>(row, col) = 255;
for three channel color image(8UC3), you should use the function as in:
对于三通道彩色图像(8UC3),您应该使用如下函数:
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.
如果信道是没有问题的,你应该检查的参数中at(i, j)
,目前我行,J目前山坳。换句话说, i 等于 Point.y, j 等于 Point.x。