Ask Your Question
0

how to flood fill to differentiate between obstacle and the path that can be taken

asked 2015-03-02 20:39:34 -0600

delaila gravatar image

updated 2015-03-03 20:28:21 -0600

)I try this coding and I got the result..I want the object and wall to be black while the floor which is the path that can be taken by the robot is in white..But, the result that I have obtained is different from what I expected (object and floor are black; wall is white)..can anyone help me?..any suggestion?

the original image : C:\fakepath\box_1.jpg

This is the result that I got..

after edge detection : image description

last result : image description

    Mat img_bw;
    threshold(grad, img_bw, 128, 255, CV_THRESH_BINARY);

    // Loop through the border pixels and if they're black, floodFill from there
    cv::Mat mask;
    img_bw.copyTo(mask);

    for (int j = 0; j < mask.rows; j++)
    {

        for (int i = 0; i < mask.cols ; i++)
        {
            if (mask.at<char>(i,j) == 0)
            {
                cv::floodFill(mask, cv::Point(i, j), 255, 0, 10, 10);
            }
            else
            {
                break;
            }
        }
    }

    // Compare mask with original.
    cv::Mat newImage;
    img_bw.copyTo(newImage);
    for (int row = 0; row < mask.rows; ++row) {

        for (int col = 0; col < mask.cols; ++col) {
            if (mask.at<char>(row, col) == 0) {
                newImage.at<char>(row, col) = 255;
            }
        }
    }
    Mat img_bw1;
    threshold(newImage, img_bw1, 128, 255, CV_THRESH_BINARY_INV);
edit retag flag offensive close merge delete

Comments

2

I think that the approach that you are following might be a bit wrong, I think that you need to apply some pre-processing before you threshold the image. If you could upload the original image, which I guess is a depth image or not? might get some feedback.

theodore gravatar imagetheodore ( 2015-03-03 07:47:58 -0600 )edit

Okay..I have already upload the original image.. :)

delaila gravatar imagedelaila ( 2015-03-03 20:18:30 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2015-03-04 13:44:06 -0600

theodore gravatar image

updated 2015-03-07 19:11:55 -0600

I do not know how your system works but without changing much in your code what you could do is to extract the foreground object (i.e. the box) with the following:

Mat bw;
cv::threshold(gray, bw, 40, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);
cv::imshow("bw", bw);

image description

add it to the image that you are showing as your "last result", then possibly you will need to pass from some morphological operation in order to discard any noise and finally bitwise_nor() the result.


edit: (check the update below)

int main()
{
    Mat src = imread("box.jpg");

    if(!src.data)
    {
        cerr << "Problem loading image!!!" << endl;
        return EXIT_FAILURE;
    }

    imshow("src", src);

image description

    // Convert image to grayscale
    Mat gray;
    cvtColor(src, gray, COLOR_BGR2GRAY);
    imshow("gray", gray);

image description

    // Convert image to binary
    Mat bin;
    threshold(gray, bin, 50, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);

    imshow("bin", bin);

image description

    cv::floodFill(gray, cv::Point(0, 0), 255, 0, 10, 5); // take the first pixel since we are
                                                         // expecting to be on the wall?

    // Add pictures
    gray += bin;

    // Filter noise
    Mat kernel = Mat::ones(3, 3, CV_8UC1);
    dilate(gray, gray, kernel);


    imshow("floodFill", gray);

image description

    threshold(gray, bin, 50, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);

    imshow("out", bin);

image description

    waitKey(0);
    return 0;
}
edit flag offensive delete link more

Comments

1

thank you very much for your answer @theodore. :) I got the result like what you did after I added threshold at the 'last result'. But, the result still same as before. I think there are something wrong at for loops coding. I try to change j++ with j-- (same with i) , but the result shows completely white. I don't know how to solve it.

delaila gravatar imagedelaila ( 2015-03-05 21:32:04 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-03-02 20:35:33 -0600

Seen: 2,436 times

Last updated: Mar 07 '15