Ask Your Question
0

How do I draw only external contour?

asked 2018-01-13 01:41:31 -0600

Santhosh1 gravatar image

Trying to extract the edge of Object(Example I have taken is Ginger here)

Image 1

image description

I did blurring and the adaptive threshold to get this image below

Image 2

image description

Then I did Canny Edge Extraction

Image 3

image description

As I am interested in the edge of the object I need to use cv2.RETR_EXTERNAL inside cv2.findContours

But External is not actually giving me the external contour here. I don't know what is going wrong here. May while going from Image 2 to Image 3 I might be losing the external edge as well.

Is there any other way in which I can extract the edge from Image 2?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2018-01-13 02:54:19 -0600

ak1 gravatar image

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.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-01-13 01:41:31 -0600

Seen: 11,961 times

Last updated: Jan 13 '18