Problem when I try to use findContour function

asked 2015-08-19 03:15:41 -0600

xyansdu gravatar image

updated 2015-08-19 03:52:05 -0600

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 <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);
}
edit retag flag offensive close merge delete

Comments

Please edit your question because all of the images are missing

LorenaGdL gravatar imageLorenaGdL ( 2015-08-19 03:36:21 -0600 )edit

You don't need this

Mat wsgray(watershedim.rows,watershedim.cols,CV_8UC1),    wscontour(watershedim.rows,watershedim.cols,CV_8UC3);
            cvtColor(watershedim,wsgray,COLOR_BGR2GRAY);

this is enough

Mat wsgray,    wscontour(watershedim.rows,watershedim.cols,CV_8UC3);
cvtColor(watershedim,wsgray,COLOR_BGR2GRAY);

About findContours becarefull Source image is modified by this function. Also, the function does not take into account 1-pixel border of the image (it’s filled with 0’s and used for neighbor analysis in the algorithm), therefore the contours touching the image border will be clipped.

LBerger gravatar imageLBerger ( 2015-08-19 03:45:02 -0600 )edit

I don't konw why I can't upload pictures. Thanks for your help! In fact, I tested some other sample images, there were no such problem, I guess maybe it is because that the contour is not smooth

xyansdu gravatar imagexyansdu ( 2015-08-19 04:01:18 -0600 )edit