Problem with Watershed algorithm

asked 2016-12-27 23:03:55 -0600

Nuz gravatar image

updated 2016-12-27 23:36:41 -0600

How to find the contour of only the leg and foot parts?

Original image:image description

Denoised image: image description

Result: image description

Here is my code:

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include "opencv2/opencv.hpp"
#include <string>

using namespace cv;
using namespace std;

class WatershedSegmenter {
private:
    cv::Mat markers;
public:
    void setMarkers(cv::Mat& markerImage)
    {
        markerImage.convertTo(markers, CV_32S);
    }

    cv::Mat process(cv::Mat &image)
    {
        cv::watershed(image, markers);
        markers.convertTo(markers, CV_8U);
        return markers;
    }
};


int main(int argc, char* argv[])
{
    cv::Mat image = cv::imread("C:/Proj/Denoised.jpg");
    cv::Mat blank(image.size(), CV_8U, cv::Scalar(0xFF));
    cv::Mat dest;
    imshow("originalimage", image);

    // Create markers image
    cv::Mat markers(image.size(), CV_8U, cv::Scalar(-1));
    //Rect(topleftcornerX, topleftcornerY, width, height);
    //top rectangle
    markers(Rect(0, 0, image.cols, 5)) = Scalar::all(1);
    //bottom rectangle
    markers(Rect(0, image.rows - 5, image.cols, 5)) = Scalar::all(1);
    //left rectangle
    markers(Rect(0, 0, 5, image.rows)) = Scalar::all(1);
    //right rectangle
    markers(Rect(image.cols - 5, 0, 5, image.rows)) = Scalar::all(1);
    //centre rectangle
    int centreW = image.cols / 4;
    int centreH = image.rows / 4;
    markers(Rect((image.cols / 2) - (centreW / 2), (image.rows / 2) - (centreH / 2), centreW, centreH)) = Scalar::all(2);
    markers.convertTo(markers, CV_BGR2GRAY);
    imshow("markers", markers);

    //Create watershed segmentation object
    WatershedSegmenter segmenter;
    segmenter.setMarkers(markers);
    cv::Mat wshedMask = segmenter.process(image);
    cv::Mat mask;
    convertScaleAbs(wshedMask, mask, 1, 0);
    double thresh = threshold(mask, mask, 1, 255, THRESH_BINARY);
    bitwise_and(image, image, dest, mask);
    dest.convertTo(dest, CV_8U);

    imshow("final_result", dest);

    //save img
    imwrite("C:/Proj/watershed.jpg", dest);

    cv::waitKey(0);

    return 0;
}
edit retag flag offensive close merge delete

Comments

take a look at this you can try to detect skin

sturkmen gravatar imagesturkmen ( 2016-12-28 03:03:36 -0600 )edit

I don't think it is possible with watershed. With grabcut it is possible if you choose mask carefully

LBerger gravatar imageLBerger ( 2016-12-28 04:53:28 -0600 )edit