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 with SQDIFF_NORMED clips at 1.0 #15215

Open
jumostedu opened this issue Aug 3, 2019 · 1 comment
Open

matchTemplate with SQDIFF_NORMED clips at 1.0 #15215

jumostedu opened this issue Aug 3, 2019 · 1 comment

Comments

@jumostedu
Copy link
Contributor

jumostedu commented Aug 3, 2019

System information (version)
  • OpenCV => latest master build Aug. 1 2019 (f9cefc8) -> 4.1.1-dev
  • Operating System / Platform => Linux
  • Compiler => gcc
Detailed description

With method SQDIFF_NORMED matchTemplate() returns 1.0 in all places where the actual result according to the formula in the documentation it should be >1.0. Notice that in contrast to the other _NORMED-methods the normalization of SQDIFF_NORMED is mathematically not guaranteed to be <=1.

Steps to reproduce

For one value:

import cv2
import numpy as np

i = np.array([[5, 1],
              [2, 8]], dtype=np.float32)
t = np.array([[1, 5],
              [7, 1]], dtype=np.float32)
m = np.ones(t.shape, dtype=t.dtype)

r = cv2.matchTemplate(i, t, cv2.TM_SQDIFF_NORMED)
print(r)

# Calculate SQDIFF_NORMED by hand
sqdiff = np.sum((i - t)**2)
norm = np.sqrt(np.sum(t**2) * np.sum(i**2))
print(sqdiff/norm)

With a larger array: you can see that there are multiple values with value exactly 1.0, so this already seems off, if you calculate the correct value at those points you see that they should be greater.

import cv2
import numpy as np

def genimg(size):
    return np.round(2*np.random.uniform(size=size).astype(np.float32))
i = genimg(42).reshape((6, 7))
t = i[:2,:2].copy()
m = np.ones(t.shape, dtype=t.dtype)
r = cv2.matchTemplate(i, t, cv2.TM_SQDIFF_NORMED)
print(r)
@asmorkalov
Copy link
Contributor

opencv/opencv_extra#916

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants