Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have this image Original Image:- ( ,And after processing it above we want the following output [output image] :-(

What I have tried:

int main()
        // Read image
        Mat imgOffice = imread("img29.jpg");

        // Remove background using chroma keying
        Mat imgNoBg;
        double threshold = 5; // adjust as necessary
      //  cv::cvtColor(imgOffice, imgOffice, COLOR_BGR2HSV); // Convert to HSV color space
      //  cv::inRange(imgOffice, Scalar(-200, -200, -200), Scalar(150, 50, 50), imgNoBg); // Detect green color range and set as the background
        cv::inRange(imgOffice, Scalar(0, 0, 0), Scalar(50, 110, 100), imgNoBg);
      //  cv::morphologyEx(imgNoBg, imgNoBg, cv::MORPH_CLOSE, cv::getStructuringElement(cv::MORPH_ERODE, cv::Size(250, 10))); // Fill small holes in the foreground
       //  cv::morphologyEx(imgNoBg, imgNoBg, cv::MORPH_CLOSE, cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(150, 20)));
         cv::morphologyEx(imgNoBg, imgNoBg, cv::MORPH_CLOSE, cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(255, 20)));
        // Display both images
        namedWindow("image", WINDOW_NORMAL);
        namedWindow("output", WINDOW_NORMAL);
        imshow("image", imgOffice);
        imshow("output", imgNoBg);
        // imwrite("final-output1(MORPH_ELLIPSE).jpg", imgNoBg);
        // imwrite("o2.jpg", imgNoBg);
        return 0;

I expecting this result is When I process on the original image, the output image given above should come in the same result as this.'
Updated 5-Mar-23 2:52am

1 solution

I would try it as follows. On the one hand I would use MORPH_CLOSE first and then binarize, on the other hand the parameter Size could be much too large. To determine the parameters it could also make sense to call the two functions dilate() and erode() separately instead of morphologyEx(). On the one hand you can see better which parameters you need, on the other hand you have more influence on the result.
Note: OpenCV seems to be extremely slow. This should actually be much faster.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900