简体   繁体   中英

OpenCV unhandled exception about cvFloodFill() function

I want to label the connected component of a binary picture, but when I run to the cvFloodFill function first time, there is a unhandled exception. There is a message box said: 在此处输入图片说明

And the cmd said: 在此处输入图片说明

Here is the code:

Mat resSlt = Mat(IMG_HEIGHT, IMG_WIDTH, CV_8UC1, slt, IMG_WIDTH * sizeof(uchar));
cvNamedWindow("resSlt");

imshow("resSlt",resSlt);
waitKey(60000);

int color = 254;
int colorsum[255] = {0};
for (int r = 0; r < resSlt.rows; r++)
{
    for (int c = 0; c < resSlt.cols; c++)
    {
        if (color > 0)
        {
           if (resSlt.at<Vec3b>(r, c)[0] == 255)
           {
               cvFloodFill(&resSlt, cvPoint(c, r), CV_RGB(color, color, color));
               --color;
           }
        }
    }
}

Can anyone tell me what happened? Thanks!

there's more than one thing wrong here:

// buffer overflow:
resSlt.at<Vec3b>(r, c)[0] 
// it's a CV_8U image, so use 
resSlt.at<uchar>(r, c)[0] 

please don't mix c++ and c api calls, stick with the c++ api.

use:

cv::floodFill(resSlt, cv::Point(c, r), cv::Scalar(color, color, color));

(the adress of a cv::Mat is not an IplImage*)

also note, that you can't draw colours into a 8bit,1channel image.

last but not least, resSlt has a borrowed pointer to the pixels in slt. if you want to use resSlt after slt has gone out of scope, you'll have to use resSlt.clone(), or face a dangling pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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