When I want to detect contours of watershed results, interesting thing emerged.!
Applied image
file:///C:/E/Figure/Test/marker.jpg
marker image
file:///C:/E/Figure/Test/ws.bmp
watershed result
file:///C:/E/Figure/Test/contours.jpg
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
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include
#include
#include
using namespace cv;
using namespace std;
Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
vector> contours;
vector 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);
}
↧