4

I'm using the cv2.matchShapes() function in OpenCV to find the shape in an image most similar to another shape.

It's giving me some weird results, e.g. when I match the shape (a circular coin) against the shape of a sweater it returns 0.09, a close score, and a better score than when it matches against an actual coin. output output

This is my code:

    for contour in cnts:

    box = bounding_box(contour)
    orig = image.copy()
    cv2.drawContours(orig, [contour, reference_contour],
            -1, (0, 0, 255), 2)
    cv2.putText(
            orig, "SIMILARITY: {0:.4f}".format(
                cv2.matchShapes(
                    contour, reference_contour, 1, 0.0)
                ),
            (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 0, 0), 2)
    cv2.imshow("coin_metric_cnn.py", orig)
    cv2.waitKey(0)

Am I doing something wrong?

  • 5
    `matchShapes()` uses [Hu invariants](https://en.wikipedia.org/wiki/Image_moment); descriptions of shapes that are rotation/translation/scale invariant. Even though your result seems weird, the reality is that coin shape is very ellipsoidal, moreso than the shirt, which means that if you blew them up all to be the same size, the ellipse from the coin has more "mass" (area) farther away from the center than the shirt does. Ergo, the shirt is more similar for `matchShapes()`. – alkasm Sep 10 '18 at 23:51
  • Ah. Is there a similar, relatively stretch-invariant function? Or will I have to rely on resizing the contour – user2461616 Sep 10 '18 at 23:52
  • No, strech and skewness are major descriptors for shapes, so it wouldn't be a good thing to remove. You *could* try the different shape matching methods, see the different types [here](https://docs.opencv.org/3.1.0/df/d4e/group__imgproc__c.html#gacd971ae682604ff73cdb88645725968d). What's the actual problem you're trying to solve? – alkasm Sep 10 '18 at 23:54
  • 2
    Solved it, just made a function that stretches the contour to have a square bounding box, so that coins viewed at an angle appear as they would face-on. This could work for more complicated shapes... – user2461616 Sep 11 '18 at 01:21
  • 2
    So, that would work for the current case, but if you detected a contour that was like a really oblique ellipsoidal shape, it would also think that is a match. Might be ok for your purpose, but just want to point that out :) – alkasm Sep 11 '18 at 02:31
  • 1
    @user2461616 can you answer your own question below? possibly sharing the code that you used for this purpose? – Sabito stands with Ukraine Sep 11 '20 at 20:20

0 Answers0