xercool
xercool

Reputation: 4609

OpenCV match template without mask?

I have two images:

Left: key.png, right: frame.png

Key.png Frame.png

Both images have size: 200x157.

And i have some probles when applying for that cv2.matchTemplate

cv2.matchTemplate(cv2.imread('frame.png'), cv2.imread('key.png'), cv2.TM_CCOEFF_NORMED)
array([[ 0.86354846]], dtype=float32)

Why i have result 0.863 when it's not a similar images? Can anybody explain me behavior of this function and suggest how to fix it or another way?

in 90% of cases it is working properly but not here... Why?

Note: I can't use feature detection and detection actually because i need find similarity of really visually similar images;

UPDATE:

For those people who think that everything is good right:

key.png frame.png

>>> cv2.matchTemplate(cv2.imread('frame.png'), cv2.imread('key.png'), cv2.TM_CCOEFF_NORMED)
array([[ 0.90551066]], dtype=float32)

Image subtraction:

subtraction

Image difference:

difference

Ok, for my comparison i need image subtraction algorythm like as in Photoshop and GIMP for analysis different point quantity.

UPDATE:

I'm trying to release the function getMSSIM in python, but results isn't perfect:

def calcMssim(i1, i2):
C1 = 6.5025
C2 = 58.5225

d = cv2.CV_32F
I1 = numpy.float32(i1)
I2 = numpy.float32(i2)

I1_2 = cv2.multiply(I1, I1)
I2_2 = cv2.multiply(I2, I2)
I1_I2 = cv2.multiply(I1, I2)

mu1 = cv2.GaussianBlur(I1, (11,11), 1.5)
mu2 = cv2.GaussianBlur(I2, (11,11), 1.5)

mu1_2 = cv2.multiply(mu1, mu1)
mu2_2 = cv2.multiply(mu2, mu2)
mu1_mu2 = cv2.multiply(mu1, mu2)

sigma1_2 = cv2.GaussianBlur(I1_2, (11,11), 1.5)
sigma1_2 = sigma1_2 - mu1_2
sigma2_2 = cv2.GaussianBlur(I2_2, (11,11), 1.5)
sigma2_2 = sigma2_2 - mu2_2
sigma12 = cv2.GaussianBlur(I1_I2, (11,11), 1.5)
sigma12 = sigma12 - mu1_mu2

t1 = 2 * mu1_mu2 + C1
t2 = 2 * sigma12 + C2
t3 = cv2.multiply(t1, t2)

t1 = mu1_2 + mu2_2 + C1
t2 = sigma1_2 + sigma2_2 + C2
t1 = cv2.multiply(t1, t2)

ssim_map = cv2.divide(t3, t1)

return cv2.mean( ssim_map )

Results:

example #1

уbz fy7

./mssim.py yBZzJ_e.png Fy7Xu_m.png 
 (0.8257484750741396, 0.7267644621469662, 0.7066612513808068, 0.0)

Match k ~ 0.74 First image more lighten that second.

example #2

Key.png Frame.png

./mssim.py key.png frame.png
(0.7317456233025181, 0.7624613566388057, 0.7645396253480031, 0.0)

Match k ~ 0.75 First image and second is really visually different!

PSNR comparison:

    def calcPSNR(I1, I2):
        s1 = cv2.absdiff(I1, I2)
        s1 = numpy.float32(s1)
        s1 = cv2.multiply(s1, s1)

        s = cv2.sumElems(s1)
        sse = s[0] + s[1] + s[2]

        if (sse <= 1e-10):
             return 0
        else:
             mse = sse/(len(I1.shape) * I1.shape[0]*I1.shape[1])
             psnr = 10*math.log((255*255)/mse, 10)
             return psnr

Results is:

example 1:

   ./psnr.py yBZzJ_e.png Fy7Xu_m.png 
   26.4697468901

example 2:

   ./psnr.py key.png frame.png
   15.4679854768 

it's not correct too because: ./psnr.py key.png key.png 0

Upvotes: 1

Views: 2439

Answers (1)

immi1988
immi1988

Reputation: 11

did you take a look at PSNR? This calculates the difference of each pixel of your images. For further information and an OpenCV example read: http://docs.opencv.org/doc/tutorials/highgui/video-input-psnr-ssim/video-input-psnr-ssim.html#videoinputpsnrmssim

Upvotes: 1

Related Questions