0

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")

ADS KUL
  • 33
  • 1
  • 9

1 Answers1

2

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

frab
  • 1,162
  • 1
  • 4
  • 14
  • they are available in python: https://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html?highlight=findhomography#findhomography – frab May 10 '21 at 12:28
  • updated python tutorial (less detailed): https://docs.opencv.org/master/d1/de0/tutorial_py_feature_homography.html – frab May 10 '21 at 12:29
  • I have implemented the solution of homography but facing a new issue. It is considering background of the object too instead of comparing object only. Any suggestions? – ADS KUL May 10 '21 at 16:35
  • you should cut the image with a bounding box – frab May 10 '21 at 17:00
  • and a mask, optionally – frab May 10 '21 at 17:00