Getting outermost contour using findContours
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
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.
And this is the output that I need to achieve
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);
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.
Can I get the full code to try this code?
@Yoga Dwi Pranata, please do not post answers, if you have none, also - plain begging for other folks code is not really welcome here.