简体   繁体   English

OpenCV错误:断言失败/访问垫子像素时

[英]OpenCV Error: Assertion failed / when visit the pixel of mat

I'm using OpenCV to try to calculate the sum of each pixel from a grayscale picture. 我正在使用OpenCV尝试从灰度图片计算每个像素的总和。

cv::Mat dst;
dst = imread("dst.png", CV_LOAD_IMAGE_GRAYSCALE);
for (i = 0; i < dst.cols; i++)
{
    for (j = 0; j < dst.rows; j++)
    {
        dstSum += dst.at<Vec3b>(i, j)[0];
    }
}

And then the error comes: 然后出现错误:

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 cv::Mat::at, OpenCV错误:断言失败(尺寸<= 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 cv :: Mat :: at,

I googled this error message, it looks like I visit the pixel out of the matrix. 我搜索了此错误消息,看起来我访问了矩阵之外的像素。

But I do have the i < dst.cols and j < dst.rows to make sure that situtation won't happen, right? 但是我确实有i < dst.colsj < dst.rows来确保不会发生j < dst.rows调整,对吗?

So what's the possible reason for this error... Can anybody help me on this? 那么,发生此错误的可能原因是什么?有人可以帮我吗?

dst = imread("dst.png", CV_LOAD_IMAGE_GRAYSCALE);

You read the dst as a gray scale 8-bit image, that means each pixel have 1 channel, and 8-bit depth for this channel. 您将dst读取为灰度8位图像,这意味着每个像素具有1个通道,并且该通道具有8位深度。 So inside the loop should be 所以循环内应该是

dstSum += dst.at<uchar>(i, j);

You can read more details here. 您可以在此处阅读更多详细信息

As you can see here , OpenCV uses a (row,col) indexation. 如您在此处看到的,OpenCV使用(row,col)索引。

Try 尝试

for(i=0;i<dst.rows;i++){
    for(j=0;j<dst.cols;j++){
        dstSum += dst.at<uchar>(i, j);
    }
}

instead of your (col, row) indexation. 而不是您的(col,row)索引。

You're doing two things wrong: 您做错了两件事:

  1. You image is grayscale ( uchar ), so you need to access it with .at<uchar>(...) 您的图像是灰度( uchar ),因此您需要使用.at<uchar>(...)访问它
  2. You are accessing the image as (col, row) , while the OpenCV (and any matrix-related) convention is (row, col) . 您以(col, row)形式访问映像,而OpenCV(以及任何与矩阵相关的)约定是(row, col) You can also use mnemonics in your iteration: r and c for row and col respectively. 您还可以在迭代中使用助记符: rc分别用于rowcol

     for(int r=0; r<dst.rows; r++) { for( c=0; c<dst.cols; c++){ dstSum += dst.at<uchar>(r, c); } } 

You can also improve your code a little: 您还可以对代码进行一些改进:

  1. You can use Mat1b and access it like at(i,j) 您可以使用Mat1b并像at(i,j)一样at(i,j)访问

     Mat1b dst = imread(...); for(int r=0; r<dst.rows; r++) { for( c=0; c<dst.cols; c++){ dstSum += dst(r, c); } } 
  2. The new OpenCV name is IMREAD_GRAYSCALE : 新的OpenCV名称为IMREAD_GRAYSCALE

     Mat1b dst = imread("path_to_image", IMREAD_GRAYSCALE); 
  3. You can use cv::sum to sum up all the pixels. 您可以使用cv::sum求和所有像素。 The resulting improved code will be: 产生的改进代码将是:

     Mat1b dst = imread("path_to_image", IMREAD_GRAYSCALE); int dstSum = sum(dst)[0]; // take only first channel 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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