In [1]:
import cv2

In [2]:
# aim to find distance matrix between two images

In [4]:
# utility functions
def show(img):
    cv2.imshow('Img', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

In [3]:
# Understanding images on black/white background.

img = cv2.imread('/home/kushashwaravishrimali/Pictures/opencv_work/800px_COLOURBOX10167106.jpg', 0)

In [8]:
show(img[150:190])

In [9]:
ret, thresh1 = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)

In [10]:
show(thresh1)

In [11]:
import sys
from scipy.misc import imread
from scipy.linalg import norm
from scipy import sum, average

In [19]:
def main():
    file1 = '/home/kushashwaravishrimali/Pictures/opencv_work/800px_COLOURBOX10167106.jpg'
#     file2 = '/home/kushashwaravishrimali/Pictures/opencv_work/800px_COLOURBOX10167106_vibrance.jpg'
#     file2 = '/home/kushashwaravishrimali/Pictures/opencv_work/800px_COLOURBOX10167106_effect.jpg'
    file2 = '/home/kushashwaravishrimali/Pictures/opencv_work/basketball.jpg'
    img1 = to_grayscale(imread(file1).astype(float))
    img2 = to_grayscale(imread(file2).astype(float))
    
    # compare
    n_m, n_0 = compare_images(img1, img2)
    print("Manhattan norm:", n_m, "/ per pixel:", n_m/img1.size)
    print("Zero norm:", n_0, "/ per pixel:", n_0*1.0/img1.size)

In [13]:
def compare_images(img1, img2):
    # normalize to compensate exposure of both images
    img1 = normalize(img1)
    img2 = normalize(img2)
    
    # calculate difference
    diff = img1 - img2 # elementwise, scipy arrays
    m_norm = sum(abs(diff)) # manhattan norm
    z_norm = norm(diff.ravel(), 0) # zero norm
    return (m_norm, z_norm)

In [14]:
def to_grayscale(arr):
    if(len(arr.shape) == 3):
        return average(arr, -1) # average over the last column
    else:
        return arr

In [15]:
def normalize(arr):
    rng = arr.max() - arr.min() # range
    amin = arr.min()
    return (arr - amin) * 255/rng

In [20]:
if __name__ == "__main__":
    main()


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-20-89026442c8a6> in <module>()
      1 if __name__ == "__main__":
----> 2     main()

<ipython-input-19-e9e143874bde> in main()
      8 
      9     # compare
---> 10     n_m, n_0 = compare_images(img1, img2)
     11     print("Manhattan norm:", n_m, "/ per pixel:", n_m/img1.size)
     12     print("Zero norm:", n_0, "/ per pixel:", n_0*1.0/img1.size)

<ipython-input-13-6c7e6d262e4e> in compare_images(img1, img2)
      5 
      6     # calculate difference
----> 7     diff = img1 - img2 # elementwise, scipy arrays
      8     m_norm = sum(abs(diff)) # manhattan norm
      9     z_norm = norm(diff.ravel(), 0) # zero norm

ValueError: operands could not be broadcast together with shapes (800,800) (1685,3000) 

In [ ]: