How to identify same objects in a image with different background, size and dimension

1.4k Views Asked by At

My requirement is something different. I have an image of a key which is on table top. I have same key which in on floor. The dimension and size of photos and keys are different but keys are same. Now I want to compare only keys and show that those are same. How to do with python and OpenCV. My current code is analyzing with histogram and gray image of entire image but I want it to for a particular object (Here is Key) in the image.

My current code is;

# Original image
      image = cv2.imread(values[0])
      gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
      histogram = cv2.calcHist([gray_image], [0], 
                                 None, [256], [0, 256])

           
# Input1 image
      image1 = cv2.imread(values[1])
      gray_image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
      histogram1 = cv2.calcHist([gray_image1], [0], 
                                  None, [256], [0, 256])

     
  
   
c1 = 0
   

i = 0
while i<len(histogram) and i<len(histogram1):
    c1+=(histogram[i]-histogram1[i])**2
    i+= 1
c1 = c1**(1 / 2)
   
  

if(c1==0):
    print("Input image is matching with original image.")
elif(c1>0 or c1<0):
 

print("Input image is not matching with original image")

1

There are 1 best solutions below

5
On

You can use OpenCv findHomography and perspectiveTransform as shown in this example in the documentation https://docs.opencv.org/2.4/doc/tutorials/features2d/feature_homography/feature_homography.html#feature-homography (old version).

Updated for Python: https://docs.opencv.org/master/d1/de0/tutorial_py_feature_homography.html

The idea is to find the same object features in two images considering homographies:

enter image description here