Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Directly apply find contours with flag CV_RETR_EXTERNAL in Image 2, without passing it through canny.

     #include <iostream>
     #include "opencv2/highgui/highgui.hpp"
     #include "opencv/cv.h"

      using namespace cv;
      using namespace std;
      int main()
     {

 Mat src = imread("/home/aniket/Downloads/LGPL/opencvqa.png"); //Load source image
 Mat src_gray;
 cvtColor(src,src_gray, CV_BGR2GRAY);
 Mat dst(src_gray.rows,src_gray.cols,CV_8UC1,Scalar::all(0));
 vector<vector<Point> > contours; // Vector for storing contour
 vector<Vec4i> hierarchy;

 findContours( src_gray, contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image

 for( int i = 0; i< contours.size(); i++ ) // iterate through each contour. 
      {

    Scalar color( 255,255,255);
    drawContours( dst, contours,i, color,0, 8, hierarchy ); 
       }

      imshow( "largest Contour", dst );
      waitKey(0);
     }

external edge

To avoid noise use area as parameter. If you want to use canny then you have to set precise parameters for canny input.

I hope this may help you to solve your problem.