Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Problem when I try to use findContour function

When I want to detect contours of watershed results, interesting thing emerged.! image description Applied image image description marker image image description watershed result image description 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);
}

Problem when I try to use findContour function

When I want to detect contours of watershed results, interesting thing emerged.! image description file:///C:/E/Figure/Test%20Image/ls.jpg Applied image image description file:///C:/E/Figure/Test%20Image/marker.jpg marker image image description file:///C:/E/Figure/Test%20Image/ws.bmp watershed result image description file:///C:/E/Figure/Test%20Image/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);
}

Problem when I try to use findContour function

When I want to detect contours of watershed results, interesting thing emerged.! file:///C:/E/Figure/Test%20Image/ls.jpg image description Applied image file:///C:/E/Figure/Test%20Image/marker.jpg marker image file:///C:/E/Figure/Test%20Image/ws.bmp watershed result file:///C:/E/Figure/Test%20Image/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);
}

Problem when I try to use findContour function

When I want to detect contours of watershed results, interesting thing emerged.! image description emerged.!

Applied image file:///C:/E/Figure/Test%20Image/marker.jpg marker image file:///C:/E/Figure/Test%20Image/ws.bmp watershed result file:///C:/E/Figure/Test%20Image/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);
}

Problem when I try to use findContour function

When I want to detect contours of watershed results, interesting thing emerged.!

Applied image file:///C:/E/Figure/Test%20Image/marker.jpg file:///C:/E/Figure/Test/marker.jpg marker image file:///C:/E/Figure/Test%20Image/ws.bmp file:///C:/E/Figure/Test/ws.bmp watershed result file:///C:/E/Figure/Test%20Image/contours.jpg 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);
}