Ask Your Question

My contours smaller than a certain value are Not being removed from list of contours, despite of correct logic. Why?

asked 2016-04-21 00:22:40 -0500

Solace gravatar image

updated 2016-06-26 18:49:41 -0500

I started with this image:

[![enter image description here][1]]

Then I applied Canny Edge Detector like:

Mat originalMatGreyScale = new Mat();
Imgproc.cvtColor(originalPhotoMat, originalMatGreyScale, Imgproc.COLOR_BGR2GRAY);
Mat edgesMat = new Mat();
Imgproc.Canny(originalMatGreyScale, edgesMat , 50, 70);

I got:

[![enter image description here][2]]

Then I found a list of contours (contours) by Imgproc.findContours().Then I did some coding to (1)find the area of largest contour (maximumContourArea) (2) Remove from contours any contour which has an area less than the maximumContourArea. Code given as follows.

What I was expecting was that all those little smudges and noise should be removed by this, but instead I still got this:

[![enter image description here][3]]

Moreover, the Log.i() messages in the following code print this:

Number of contours initially: 27

Number of contours after processing: 27

List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(edgesMap, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

Log.i(TAG, "Number of contours initially: " + contours.size());//check

double maximumContourArea = 0;

Iterator<MatOfPoint> contoursIterator = contours.iterator();

while(contoursIterator.hasNext()) {
    MatOfPoint nextContour =;
    double nextContourArea = Imgproc.contourArea(nextContour);

    if (nextContourArea > maximumContourArea) {
        maximumContourArea = nextContourArea;

while(contoursIterator.hasNext()) {
    MatOfPoint nextContour =;
    if (Imgproc.contourArea(nextContour) < maximumContourArea*(10 / 100)) {

Log.i(TAG, "Number of contours after processing: " + contours.size());//check
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-04-21 00:36:19 -0500

berak gravatar image

updated 2016-04-22 06:59:50 -0500

10/100 == 0 (integer division)

To avoid this, do a double division which is been done by 10/100.0

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-04-21 00:22:40 -0500

Seen: 224 times

Last updated: Jun 26 '16