Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.

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;
}