如何识别图像中具有不同背景、大小和尺寸的相同对象

问题描述

我的要求有所不同。 我有一张放在桌面上的钥匙的图像。我在地板上有相同的钥匙。照片和密钥的尺寸和大小不同,但密钥相同。现在我只想比较键并表明它们是相同的。如何使用 python 和 OpenCV。我当前的代码正在分析整个图像的直方图和灰度图像,但我希望它针对图像中的特定对象(这里是关键)。

我当前的代码是;

# 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],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("输入图片与原图不匹配")

解决方法

您可以使用 OpenCv findHomographyperspectiveTransform,如文档 https://docs.opencv.org/2.4/doc/tutorials/features2d/feature_homography/feature_homography.html#feature-homography(旧版本)中的示例所示。

针对 Python 更新:https://docs.opencv.org/master/d1/de0/tutorial_py_feature_homography.html

这个想法是在考虑单应性的两个图像中找到相同的对象特征:

enter image description here