-1

In python 2.7, I want to compare 2 image so that It return similarity percentage to me , How to do this? please show me step by step. Thanks!

user7442628
  • 23
  • 1
  • 8

2 Answers2

0

A very simple and fast approach to do this without openCV and any library for computer vision is to norm the picture arrays by

import numpy as np
picture1 = np.random.rand(100,100)
picture2 = np.random.rand(100,100)
picture1_norm = picture1/np.sqrt(np.sum(picture1**2))
picture2_norm = picture2/np.sqrt(np.sum(picture2**2))

After defining both normed pictures (or matrices) you can just sum over the multiplication of the pictures you like to compare:

1) If you compare similar pictures the sum will return 1:

In[1]: np.sum(picture1_norm**2)
Out[1]: 1.0

2) If they aren't similar, you'll get a value between 0 and 1 (a percentage if you multiply by 100):

In[2]: np.sum(picture2_norm*picture1_norm)
Out[2]: 0.75389941124629822

Please notice that if you have colored pictures you have to do this in all 3 dimensions or just compare a greyscaled version. I often have to compare huge amounts of pictures and that's a really fast way to do so.

Franz
  • 623
  • 8
  • 14
  • How can i define picture and how will I have 3 dimensions to compare colored picture? – user7442628 Jun 09 '17 at 13:23
  • Greyscale pictures in most times are represented by m x n arrays, containing values between 0 and 255 or 0 and 1. So my picture arrays are just examples for picture data. A colored picture typically contains a size 3 array at every item in the m x n array. The 3 entries represent the colors r,g,b. – Franz Jun 09 '17 at 13:27
  • If you don't mind please tell me the step by step procedure to color image. I shall be thankful to you – user7442628 Jun 09 '17 at 13:36
  • The solution depends on the format of your original data. How is your data looking like? By the way: If you like to compare the content of two pictures this isn't the right approach. In that case you should use opencv. But if you just like to compare pixels or overlaps thats the right approach. – Franz Jun 09 '17 at 13:42
  • No I don't want to compare pixel and overlaps I want to compare content. Basically I have 2 URLs which contain 2 different images I just want to compare both of them. – user7442628 Jun 09 '17 at 13:44
  • That is a more difficult task and I would recommend you to read https://stackoverflow.com/questions/11541154/checking-images-for-similarity-with-opencv?rq=1 – Franz Jun 09 '17 at 13:46
  • I just need a simple solution, I am not good in programming just need a simple solution – user7442628 Jun 09 '17 at 14:01
  • Please help @franz – user7442628 Jun 09 '17 at 22:56
  • I'm sorry but I don't believe that there is a very simple and robust solution. I would recommend you to read this article: http://www.pyimagesearch.com/2014/09/15/python-compare-two-images/ Maybe it's the solution you're looking for. – Franz Jun 10 '17 at 10:12
0

You can do something like:

#Dimension tuppel
dim = (100,100,3) #Image dim in y,x,channels
pic1 = np.random.rand(dim)
pic2 = np.random.rand(dim)
#Either use operations that can be performed on np arrays
#or use flatten to make your (100,100,3) Immage a 100*100*3 vector
#Do your computation with 3 channels

#reshape the image if flatten np.reshape(output,(dim))
DONE
Max Krappmann
  • 490
  • 5
  • 19