Detecting shapes in an image

Had been using the bounding rect to compute the duplicates. Not IOU specifically. Will include its use.

Consider this image (doesn’t have a duplicate contour) it highlights the limitation of using bounding rect alone.

Image with contour highlighted

Image with bounding rect highlighted:

As seen in the images the bounding rect erroneously, as per human expectations (misaligned mostly), highlights are larger area.

Using other contour features listed here appears to be helpful.

Do let me know if other ideas could be explored.

I would use masks instead of bounding rects. maskA is an image where contour A is drawn (filled), maskB for contour B, then
IoU(A,B) = countNonZero(maskA * maskB)/countNonZero(maskA + maskB)

Thanks for the formula @Micka. Results are better. However, when total contours detected are large, performance degrades drastically. Looking for ways to improve the performance. Check if contours intersect before computing the IoU, could help. The contour images also can be cached.

Would be great if you can share any other approaches to speed up the processing.

You can first use the bounding rect intersections for a fast estimate whether contours might intersect.

You can even compare the boundingRect sizes as a first indication of the possible-best IoU those contours can have. For example, if A is very big and B ist very small, the IoU can’t be high.

Once you precomputed the contour masks you can run the IoU computation on subimages of region boundingRectA | boundingRectB.

To save memory you can even draw “local masks” which are only as big as the bounding rect of that contour, but that makes IoU computation on those masks more complicated and a tiny bit slower, so maybe dont think about that unless you are working with very big images.

1 Like

Thanks for sharing ideas to optimize @Micka. Had been away due to a personal exigency.

Yes, had implemented this to short-circuit and fail-fast.

Will look at implementing other approaches for optimization.