Ask Your Question
1

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 -0600

Solace gravatar image

updated 2020-10-11 14:37:26 -0600

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 = contoursIterator.next();
    double nextContourArea = Imgproc.contourArea(nextContour);

    if (nextContourArea > maximumContourArea) {
        maximumContourArea = nextContourArea;
    }
}

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

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
5

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

berak gravatar image

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

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

Question Tools

1 follower

Stats

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

Seen: 590 times

Last updated: Jun 26 '16