1

I am newbie in programming, so please help me. I want to retrieve only the outer contour of an object but the problem is I got another contour like a border. How can I get the only outer contour of object without any other contour?

An example image:

enter image description here

Here is the code I made:

// cvAutoHeight.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "opencv\cvaux.h"
#include "opencv\cxmisc.h"
#include "opencv\highgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdlib.h>

using namespace std;
using namespace cv;

int main(int, char**)
{
 Mat threshold_output;
 int thresh = 100;
 vector<vector<Point> > contours;
 vector<Vec4i> hierarchy;
 RNG rng(12345);

 CvCapture* capture = cvCaptureFromCAM(0);

cv::Mat frame; cv::Mat src_gray;

while(1) {
    frame = cvQueryFrame( capture );

    cvtColor( frame,src_gray, CV_BGR2GRAY );
    blur( src_gray, src_gray, Size(3,3) );

    //Canny( src_gray, threshold_output, 128, 255, 3 );
    threshold( src_gray, threshold_output, 100, 200, THRESH_BINARY );
findContours( threshold_output, contours, hierarchy,CV_RETR_TREE,
    CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    /// Find the rotated rectangles and ellipses for each contour
    vector<RotatedRect> minRect( contours.size() );

  for( int i = 0; i < contours.size(); i++ )
     { 
         minRect[i] = minAreaRect( Mat(contours[i]) );
      }

  /// Draw contours + rotated rects + ellipses
  Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
  for( int i = 0; i< contours.size(); i++ )
     {

   Scalar color = Scalar( rng.uniform(0,0), rng.uniform(0,0), rng.uniform(250,250) );
       // contour
       drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0,
               Point() );

       // rotated rectangle
       Point2f rect_points[4]; minRect[i].points( rect_points );
       for( int j = 0; j < 4; j++ )
          line( frame, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );
     }


  namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
  imshow( "Contours", frame );

    cvWaitKey(33);
}
return 0;
 }

`

bjou
  • 1,077
  • 1
  • 7
  • 19

1 Answers1

0

by default, out of borders have 0 pixel value = black. after threshold, you get your black blob with white background. this why you have 2 contours. choose one of the solutions:

  • use binary threshold inverted.
  • apply canny after threshold.
baci
  • 2,418
  • 12
  • 27
  • Thanks, but I got another problem. I noticed that I have detect also the inner contour of coin but I want only to detect the outer contour and make rectangle box. Please give me some solution. – user2529081 Jun 30 '13 at 08:21
  • Thank you very much and it is very helpful. I want to measure the height and the width of the image using the rectangle. Do have any idea or solution? – user2529081 Jul 01 '13 at 11:34
  • the minimum area rectangle function returns you a [rotatedRect](http://docs.opencv.org/modules/core/doc/basic_structures.html#rotatedrect) structure, which already has the width and height properties for you to use. Type minRect.size.width and use it. – baci Jul 01 '13 at 11:46
  • I am the same person in the other question. I am really confusing now. Can you show me some samples or edit my codes? Thanks for your help. – user2529081 Jul 01 '13 at 14:02
  • I cannot edit your code, it is not moral. You just need to track the width and height properties of your rectangle object. You will get pixel-wise length of them. It shouldn't be so hard, as you were the one who wrote this code. If you want to obtain real-world height and width of the object, it is related with "camera calibration" and another [issue](http://stackoverflow.com/questions/12138567/find-a-height-of-object-using-opencv). – baci Jul 01 '13 at 16:41
  • Please send me some links and tutorials on how to do it. – user2529081 Jul 01 '13 at 16:58