When I want to detect contours of watershed results, interesting thing emerged.! image description Applied image marker image watershed result contour result Problem description: It is obvious that the contours of some shapes are not continuous but split into several different groups. Code:
/* findContour */
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/features2d/features2d.hpp>
#include <cstdio>
#include <fstream>
using namespace cv;
using namespace std;
Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/** @function thresh_callback */
void thresh_callback(int, void* )
{
Mat canny_output;
/// Detect edges using canny
Canny( src_gray, canny_output, thresh, thresh*2, 3 );
/// Find contours
findContours( canny_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
}
int _tmain(int argc, _TCHAR* argv[])
{
/* watershed program has been omitted*/
// find contour
Mat wsgray(watershedim.rows,watershedim.cols,CV_8UC1), wscontour(watershedim.rows,watershedim.cols,CV_8UC3);
cvtColor(watershedim,wsgray,COLOR_BGR2GRAY);
imshow("wsgray",wsgray);
waitKey(0);
watershedim.copyTo(src);
wsgray.copyTo(src_gray);
char* source_window = "Source";
namedWindow( source_window, CV_WINDOW_AUTOSIZE );
imshow( source_window, src );
createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
thresh_callback( 0, 0 );
waitKey(0);
for(int k = 0; k < contours.size(); k++)
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( wscontour, contours, k, color, 2, 8, hierarchy, 0, Point() );
}
imshow("wscontour", wscontour);
waitKey(0);
}