My contours smaller than a certain value are Not being removed from list of contours, despite of correct logic. Why?
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