I am working on the following code snippets to compute the similarity of two images:
import cv2, sys [5/981]
import numpy as np
def compute_hisgram(path):
hog = cv2.HOGDescriptor()
im = cv2.imread(path)
img_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
h = hog.compute(im)
return h
def histogram_intersection(h1, h2):
# minima = np.minimum(h1, h2)
# intersection = np.true_divide(np.sum(minima), np.sum(h2))
#return intersection
return cv2.compareHist(h1, h2, cv2.HISTCMP_INTERSECT)
h1 = compute_hisgram(sys.argv[1])
h2 = compute_hisgram(sys.argv[2])
h12 = histogram_intersection(h1, h2)
# to normalize, we need to divide by the original image.
print (h12/np.sum(h2))
And when executing the above code with two images as the input, it outputs a value that is close to 1.0, which seems quite normal.
python3 t.py image1.png image2.png
0.9932124283243112
However, to my very much surprise, when the last statement is written in the following way:
print (h12/sum(h2))
The output is different, and it is a number larger than 1! And the code is just much slower than before.
python3 t.py image1.png image2.png
[1.1126189]
Is it a bug of Python sum
function? Or am I missed anything here? Thanks.
======== update
Here is the output of print (h2)
:
[[0.0924307 ]
[0.05680538]
[0.07150667]
...
[0.10983132]
[0.17328948]
[0.0688285 ]]
And h12
:
4517725.058263534