Ask Your Question
1

findContours outputs different result, if the camera is parallel to the contour border

asked 2020-03-24 17:03:03 -0600

link1375 gravatar image

updated 2020-03-24 17:04:30 -0600

Actually this some kind of repetition of my previous post , but now I know where the problem is.

So basically, I can switch between Threshold (threshValue does not have an effect here, but is not important for the question)

thresh = Imgproc.threshold(gray, edged, threshValue, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU );
Imgproc.findContours(edged, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

and canny edge detection

Imgproc.Canny(gray, edged, 100, 200, 3);
Imgproc.findContours(edged, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

As the problem occur for both of them, I will only show it for Threshold. I applied some filters to limit the found contours, but the only one, which are relevant are the number of contour points and the convexity of the contour.

MatOfPoint2f approxCurve = new MatOfPoint2f();

Imgproc.approxPolyDP(tempMatPoint2f, approxCurve, contours[i].rows() * detectorParameters.get_polygonalApproxAccuracyRate(), true);

var numberPoints = approxCurve.rows();

if (filterNumberCorners && (numberPoints < minNumberCorners || numberPoints > maxNumberCorners))
{
    contours.RemoveAt(i);
    continue;
}

if (filterConvexity && !Imgproc.isContourConvex(new MatOfPoint(approxPoints)))
{
    contours.RemoveAt(i);
    continue;
}

As I adapted some parts from the offical c++ implementation of the aruco detection (I use OpenCVForUnity), there are still parts which look similars, like detectorParameters.get_polygonalApproxAccuracyRate().

So if the camera is a bit rotated and both of the above mentioned filters are applied, all contours I want to detect are found

image description

If I hold the camera parallel to the contour border and apply only the convexity filter and set the max number of contour points to 18, also all contours are found.

image description

But if I then filter them by convexity I get this

image description

This is how the straight and rotated black/white looks like.

image description

image description

So my question is: How can I make it happen, that the contours in the last two images are found, even if I apply the two filters?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-03-27 11:12:58 -0600

link1375 gravatar image

The problem was contours[i].rows() * detectorParameters.get_polygonalApproxAccuracyRate(). If I set it to a fixed value like 3, it works.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-03-24 17:03:03 -0600

Seen: 321 times

Last updated: Mar 27 '20