Contours matching by matchShapes whitch return big difference for two similar shapes

Hello.
I have two similar images:


As you can see - the images absolutely match!
here is my c++ code:

vector<vector<cv::Point>> LoadImage1(cv::Mat m)
{
    cv::Mat g_gray, g_blur, g_binary;
    

    cv::cvtColor(m, g_gray, cv::COLOR_BGR2GRAY, 1);
    cv::blur(g_gray, g_blur, cv::Size2i(3, 3));
    cv::threshold(g_blur, g_binary, 80, 255, cv::THRESH_BINARY_INV);

    //cv::OutputArrayOfArrays cnt1;
    vector< vector< cv::Point> > cnt;
    cv::findContours(g_binary, cnt, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);
    return cnt;
}
int main()
{
    cv::Mat m1 = cv::imread("C:\\..Here First image..jpg", cv::IMREAD_COLOR);
    cv::Mat m2 = cv::imread("C:\\..Here Second image..jpg", cv::IMREAD_COLOR);
    vector<vector<cv::Point>> cnt1 = LoadImage1(m1);
    vector<vector<cv::Point>> cnt2 = LoadImage1(m2);
    
    cv::drawContours(m1, cnt1, -1, cv::Scalar::all(255));
    cv::drawContours(m2, cnt2, -1, cv::Scalar::all(255));

    
    double res = cv::matchShapes(cnt1[0], cnt2[0], cv::CONTOURS_MATCH_I1, 0);
    printf("res=%f", res);
    
    while (true) {
        if (cv::waitKey(100 /* milliseconds */) == 27) break;
    }
}

And, on the same image, there are two contours. and as you can see the result of matchShapes function = 244

And i don’t understand the result… Why so big number? contour is similar, very similar.
What i done wrong. Help

May be to understand this value (244) you can print

  • hu moments for cnt1[0] and cnt2[0]
  • check cnt1[[0], cnt2[0] points

ok, i asked wrong question. Ofcourse i understand that result is just a formula (cv::CONTOURS_MATCH_I1). I saw this formula and i understand why result value equal to 244. I expected the result will be <1. Is this means that all theory about hu moments is NOT WORKING?
Look at this:


here i compare first shape ( bracket) with circle ( symbol ‘o’ externel). and the result is 3.99.
OpenCV think’s that bracket more similar to circle than to second ( similar) bracket. It’s totally wrong!. How can i use OpenCV for shape matching when the result’s is wrong?
Maybe i make a mistake? Plese Help!.

you draw ALL contours but only work with the first?

do you expect contours to be sorted by size?

have you checked that you have exactly one contour?

have you looked at the Hu moments of each contour? please post these values

undeclared crosspost:

Here is code with comments:

Yes, i work only with one contour from cnt1[0] and cnt2[0].
Size?

please, please, no useless screenshots of code, here.

Sorry… This is just for the first time.

1 Like

curious problem.

m00 is the number of pixels. they’re very close, i.e. roughly the same height and width too.

the other “regular” moments are harder to interpret because they are location-dependent. the centroid is m10/m00, m01/m00

Hu moments are made to be invariant to various things. the first two are fairly close. I can’t explain the third being “off” (doesn’t mean it’s unexplainable, only that I don’t have the brain capacity now).

this is old technology. don’t expect too much out of it. since it seems to break down with your contours, you’ll have to trace every step of the matchshapes matching mode of your choice, and also of the calculations for those Hu moments.

I’d recommend shifting the contours so the centroid is at (0,0). just calculate it and subtract it from each point of a contour. that’ll make some of these numbers easier to eyeball.

I modify the program. I calc bounding Rect, find x center and y center and subtract xc and yc from each point of contour. And result is - Hu moments stay the same. And the result still the same =244
But , m00,m10 are changed!
1011