Ask Your Question
0

Getting outermost contour using findContours

asked 2014-08-25 12:55:22 -0600

Chorongiee gravatar image

updated 2014-08-26 12:49:37 -0600

Hi everyone! I'm trying to get the outermost contour of an object after detecting it using findContours, I've tried using Imgproc.RETR_EXTERNAL as the method but it still includes the contours inside it.

This is my reference photo

image description

This is the contours that I get after applying a greyscale, threshold and canny edge. As you can see, there are still remaining contours inside the biggest circle.

image description

And this is the output that I need to achieve

image description

Please note that the reference image is subjected to change, as the app gives the user a shape that he will look for in the real world and capture it using the camera. That captured photo will be used then for checking if it is indeed the same as the given shape.

If I've done something wrong with my approach, please feel free to correct me. I'm hoping for a quick response, thanks!

EDIT:

Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.qwe);
    //Utils.matToBitmap(m, bm);
    //convert to mat:
    int iCannyLowerThreshold = 60, iCannyUpperThreshold = 100;      
    Mat m = new Mat(bm.getWidth(), bm.getHeight(), CvType.CV_8UC1);
    Utils.bitmapToMat(bm, m);
    Mat thr = new Mat(m.rows(),m.cols(),CvType.CV_8UC1); 
    Mat dst = new Mat(m.rows(), m.cols(), CvType.CV_8UC1, Scalar.all(0));
    Imgproc.cvtColor(m, thr, Imgproc.COLOR_BGR2GRAY);
    Imgproc.threshold(thr, thr, 100, 255, Imgproc.THRESH_BINARY);        
    Imgproc.Canny(thr, thr, iCannyLowerThreshold, iCannyUpperThreshold);
    //Imgproc.findContours(m, contours, new Mat(), 0, 1);
    Imgproc.findContours( thr, contours, new Mat(),Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0,0) );
    Scalar color = new Scalar( 255,255,255);
    Imgproc.drawContours(dst, contours, -1, color, 3);    
    //Core.rectangle(m, bounding_rect.br(), bounding_rect.tl(), new Scalar(0,255,0));


    Utils.matToBitmap(dst,bm);
edit retag flag offensive close merge delete

Comments

If you want the outer contour, loop over your contour set and simply remove a contour if the area size is lower than the previous one. This will remain you with the largest contour possible. I think this is the most easiest way. However you must be doing something wrong when calculating your contours, since I only retrieve the outer one myself on your image.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-08-26 07:27:53 -0600 )edit

Can I get the full code to try this code?

Yoga Dwi Pranata gravatar imageYoga Dwi Pranata ( 2016-11-15 00:36:31 -0600 )edit

@Yoga Dwi Pranata, please do not post answers, if you have none, also - plain begging for other folks code is not really welcome here.

berak gravatar imageberak ( 2016-11-15 01:30:20 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2014-08-26 01:53:58 -0600

Hi @Chorongiee!

This image work pretty good in my code & it is correctly detecting your required border. Check your Canny image output it seems like it is containing some white pixels in the outer border. other than that it looks good.

If you want some other better approach try this.

  1. Load BGR image & convert it into HSV Image.
  2. Now filter the black regions by filtering the HSV Range V- 100 to 255. The result will look like this.

image description

  1. Apply Gaussian smooth & threshold binary so smooth the edges if you want.
  2. Find contours with RETR_TYPE.CV_RETR_EXTERNAL to find only the outer most contours.
  3. Result is your required output image.
edit flag offensive delete link more

Comments

You can do also some kind of morphology (like closing) for getting rid of the small regions.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-08-26 02:34:11 -0600 )edit

Hi! I'm currently trying to apply the other approach that you gave me, I'm stuck at the part where I need to filter the black regions by filtering the HSV range. Can you post a code snippet on how to do it? I've also edited my post and have added the code where I image the process. Please check it and tell me the parts where I've done wrong. Thanks!

Chorongiee gravatar imageChorongiee ( 2014-08-26 12:48:19 -0600 )edit

Can I get the full code to try this code?

Yoga Dwi Pranata gravatar imageYoga Dwi Pranata ( 2016-11-15 00:35:57 -0600 )edit

Question Tools

Stats

Asked: 2014-08-25 12:55:22 -0600

Seen: 12,007 times

Last updated: Aug 26 '14