OpenCV: using a loop to find white pixels and fill

I have the following image:

nine

I am trying to create a loop that will take into account the color of each pixel and fill the fill from any white dots that it finds.

At the moment I have this code:

        for(int y=0; y<image.rows; y++)
    {
        for(int x=0; x<image.cols; x++)
        {
            image.at<cv::Vec3b>(y,x);

            if(image.at<cv::Vec3b>(y,x)[0] == 255 && image.at<cv::Vec3b>(y,x)[1] == 255 && image.at<cv::Vec3b>(y,x)[2] == 255)
            {   
                /*image.at<cv::Vec3b>(y,x)[0] = 155;
                image.at<cv::Vec3b>(y,x)[1] = 0;
                image.at<cv::Vec3b>(y,x)[2] = 0;*/

                int filling = cv::floodFill(image, cv::Point(y,x), 255, (cv::Rect*)0, cv::Scalar(), 200);

                //cout << "x:" << x << " y:" << y;
            }

        }
    } 

As you can see, it iterates over each pixel, and if it is white, it fills its stream from there. I also left some old code in the loop that repainted every single pixel and worked, but when I try to fill the fill of my image, it leaves me with something like this:

ninefail

As a result, the stream fills the image more than 4000 times, which takes a lot of time and does not even fill the entire area.

Any ideas what I did wrong? Or is there a better way to do this?

+4
1

, (0, 0), , (x, y). `at < > () - .

:

cv::Vec3b white(255, 255, 255);

for(int y=0; y<image.rows; y++)
{
    cv::Vec3b* row = image.ptr<cv::Vec3b>(y)
    for(int x=0; x<image.cols; x++)
    {

        if(row[x] == white)
        {   
            cv::floodFill(image, cv::Point(x,y), cv::Scalar(255,0,0), (cv::Rect*)0, cv::Scalar(), cv::Scalar(200,200,200));
        }

    }
} 
+1

Source: https://habr.com/ru/post/1547761/


All Articles