0

I am having problems finding bounding boxes and circles after using watershed. I have used Opencv 2 Computer Vision Application Programming Cookbook book for watershed algorithm and opencv.org example for creating bounding boxes and circles.

My problem is that I am getting too many boxes and circles for my object. Also, why does it add border to the image?

I am using OpenCV 2.4.11 and Visual Studio Express 2013.

This is my code:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "header.h"

using namespace cv;
using namespace std;

int main()
{
    // Read Input Image
    Mat src = imread("toy.png");

    // Convert the input image to gray-scale
    Mat src_gray;
    cvtColor(src, src_gray, CV_BGR2GRAY);

    // Threshold the gray-input image
    Mat src_thresh;
    threshold(src_gray, src_thresh, 100, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);

    // Detect the foreground.
    Mat fg;
    erode(src_thresh, fg, Mat(), Point(-1, -1), 6);

    // Detect the background
    Mat bg;
    dilate(src_thresh, bg, Mat(), Point(-1, -1), 6);
    threshold(bg, bg, 1, 128, THRESH_BINARY_INV);

    // Get the markers
    Mat markers(src_thresh.size(), CV_8U, Scalar(0));
    markers = fg + bg;

    // Create watershed segmentation object
    WatershedSegmenter segmenter;

    // Set markers and process
    segmenter.setMarkers(markers);
    segmenter.process(src);

    // Get segmentation result
    Mat seg = segmenter.getSegmentation();

    // Get watersheds
    Mat ws = segmenter.getWatersheds();

    // Display the result
    imshow("Watershed", ws);

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    /// Find contours
    findContours(ws, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    /// Approximate contours to polygons + get bounding rects and circles
    vector<vector<Point> > contours_poly(contours.size());
    vector<Rect> boundRect(contours.size());
    vector<Point2f>center(contours.size());
    vector<float>radius(contours.size());

    for (int i = 0; i < contours.size(); i++)
    {
        approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
        boundRect[i] = boundingRect(Mat(contours_poly[i]));
        minEnclosingCircle((Mat)contours_poly[i], center[i], radius[i]);
    }


    /// Draw polygonal contour + bonding rects + circles
    Mat drawing = Mat::zeros(ws.size(), CV_8UC3);
    for (int i = 0; i< contours.size(); i++)
    {
        drawContours(drawing, contours_poly, i, Scalar(0, 0, 255), 1, 8, vector<Vec4i>(), 0, Point());
        rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), Scalar(255, 0, 0), 2, 8, 0);
        circle(drawing, center[i], (int)radius[i], Scalar(0, 255, 0), 2, 8, 0);
    }

    /// Show in a window
    imshow("Contours", drawing);

    waitKey();
    return 0;

}

This is header.h file:

#if !defined WATERSHS
#define WATERSHS

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>

class WatershedSegmenter {

private:

    cv::Mat markers;

public:

    void setMarkers(const cv::Mat& markerImage) {

        // Convert to image of ints
        markerImage.convertTo(markers, CV_32S);
    }

    cv::Mat process(const cv::Mat &image) {

        // Apply watershed
        cv::watershed(image, markers);

        return markers;
    }

    // Return result in the form of an image
    cv::Mat getSegmentation() {

        cv::Mat tmp;
        // all segment with label higher than 255
        // will be assigned value 255
        markers.convertTo(tmp, CV_8U);

        return tmp;
    }

    // Return watershed in the form of an image
    cv::Mat getWatersheds() {

        cv::Mat tmp;
        markers.convertTo(tmp, CV_8U, 255, 255);

        return tmp;
    }
};


#endif

toy.png: toy.png

watershed.png: watershed.png

contours.png: contours.png

Thank you.

Community
  • 1
  • 1
massakrienen
  • 266
  • 2
  • 3
  • 14

1 Answers1

2

The problem is that you're passing to findContour a binary image where the edges (foreground) are set to 0, and the rest (background) is set to 255.

findContours expects instead that the foreground is >0, and the background is 0. So you need to invert the binary image you're passing to findContour, like:

/// Find contours
Mat ws_negate = ~ws;
findContours(ws_negate, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

The result image will be:

enter image description here

Miki
  • 40,887
  • 13
  • 123
  • 202