Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matchTemplate inconsistent results with M1 Chip #22486

Open
4 tasks done
hammad-looper opened this issue Sep 8, 2022 · 0 comments
Open
4 tasks done

matchTemplate inconsistent results with M1 Chip #22486

hammad-looper opened this issue Sep 8, 2022 · 0 comments
Labels
bug platform: arm ARM boards related issues: RPi, NVIDIA TK/TX, etc platform: ios/osx

Comments

@hammad-looper
Copy link

hammad-looper commented Sep 8, 2022

System information (version)
  • OpenCV => 4.6.0
  • Operating System / Platform => MacOS Monterey 12.5.1
  • Chip => Apple M1 Pro
  • Python 3.10.6
Detailed description

We have heavy use of matchTemplate for different use cases our team has been developing on all platform

  • Windows
  • Linux
  • Mac x86

Recently when some of developers upgraded with Mac M1 Chip they started to have issues same code that has confidence of 1 is not returning nan for only Mac M1.

Steps to reproduce

We are using OpenCV via python here is requirements.txt

opencv-python-headless==4.6.0.66
opencv-contrib-python==4.6.0.66

And here is the code snippet

import cv2 as cv
from matplotlib import pyplot as plt
img_grey = cv.cvtColor(cv.imread('1.png'), cv.COLOR_BGR2GRAY)
template = cv.cvtColor(cv.imread('horizontal_small.png'), cv.COLOR_BGR2GRAY)
mask = cv.cvtColor(cv.imread('horizontal_small-mask.png'), cv.COLOR_BGR2GRAY)
w, h = template.shape[::-1]
methods = ['cv.TM_CCOEFF_NORMED']
for meth in methods:
    method = eval(meth)
    # Apply template Matching
    res = cv.matchTemplate(image=img_grey, templ=template, method=method, mask=mask)
    min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
    if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)
    cv.rectangle(img_grey,top_left, bottom_right, 255, 2)
    plt.subplot(121),plt.imshow(res,cmap = 'gray')
    plt.title(f'Matching Result {max_val}'), plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(img_grey,cmap = 'gray')
    plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
    plt.suptitle(meth)
    plt.show()

Code above return confidence of 1 on other platforms and nan on Mac M1
horizontal_small-mask
horizontal_small
1
result_M1

Issue submission checklist
  • I report the issue, it's not a question
  • I updated to the latest OpenCV version and the issue is still there
  • I checked the problem with documentation, FAQ, open issues,
    forum.opencv.org, Stack Overflow, etc and have not found any solution
  • There is reproducer code and related data files: videos, images, onnx, etc
@asmorkalov asmorkalov added bug platform: ios/osx platform: arm ARM boards related issues: RPi, NVIDIA TK/TX, etc labels Sep 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug platform: arm ARM boards related issues: RPi, NVIDIA TK/TX, etc platform: ios/osx
Projects
None yet
Development

No branches or pull requests

2 participants