Classical Computer Vision: Similarity Measures in Images

 Why Measure Similarity?

    One of the most important things we do when looking into an image is classification. We look at something and our brain tries to give it a meaning.

    Sometimes, we classify things by inferring it based on something else. Something that is similar to what we previously know. 

    The same thing happens to computer vision. Let's imagine that we have an image-A that we know what it is: it's a car. If image-B is another car, but with a different color. Don't we wish that computers, somehow, were able to infer that image-B is also a car, just because it's so similar to A?

    Image Similarity tries to tackle with this problem. The goal here is to extract from two images a comparable rank with which we could say that image-A is probably the same image as B, or at least it's very close to B.




    One of the ways to do that is to use algorithms to calculate similarity or image distancing

    Euclidean Distance

        Euclidean Distance can be used to measue image distance in a pretty straightforward way. Obviously, it's a very simple technique, so don't expect it to perform so great. Nevertheless, for differentiate images that are a lot far away from a reference, it works.

    Let's use the example of La Gioconda to try this technique in Python. First and foremost, euclidean distance is calculated by taking the square-root of the sum of the squared difference of two list of values.

    So, for example, let's say that array1 = [ 1, 3, 1, -2, 5, 0 ]  and array2 = [0, 7, -3, -1, 4, -1 ]. What's the euclidean distance between array1 and array2?

    first, we calculate array1 - array2: [ 1-0 , 3 - 7, 1 - -3, -2 - -1, 5 - 4, 0 - -1]  = [ 1, -4, 4, -1, 1, 1]
    next, we square the difference: [ 1, 16, 16, 1, 1, 1 ]
    now we sum all the values: 1 + 16 + 16 + 1 + 1 + 1= 36
    finally, we take the square-root of 36, which is 6.

    So, the euclidean distance of array1 and array2 is 6.

    Since an image is nothing but a huge array of integers, ranging from 0 to 255, it's easy to see that we could apply the same concept and get the euclidean distance for two entire images and check whether it's greater or lower than another measure. 

    





import numpy as np
import skimage
import skimage.io as io
from skimage.color import rgb2gray, rgba2rgb
import matplotlib.pyplot as plt
import math
def calc_euclidean(image1, image2):
max_row_img2 = image2.shape[0]
max_col_img2 = image2.shape[1]

euclid = 0.0

for row in range(0, image1.shape[0]):
for col in range(0, image1.shape[1]):
if (row < max_row_img2 and col < max_col_img2):
p = (image1[row][col] - image2[row][col]) ** 2
euclid = euclid + p/100
return 10 * math.sqrt(euclid)


if __name__ == '__main__':
monalisa = rgb2gray(io.imread('monalisa.png'))
cat_monalisa = rgb2gray(io.imread('cat_monalisa.png'))
not_monalisa = rgb2gray(rgba2rgb(io.imread('not_monalisa.png')))

print ("Euclidian distance between monalisa and cat monalisa: " +
str(calc_euclidean(monalisa, cat_monalisa)))
print ("Euclidian distance between monalisa and fake monalisa: " +
str(calc_euclidean(monalisa, not_monalisa)))



Euclidian distance between monalisa and cat monalisa: 61.65221942021926

Euclidian distance between monalisa and fake monalisa: 114.37413116635821


    

Comentários