首页 > 解决方案 > 如何识别具有不同背景、大小和尺寸的图像中的相同对象

问题描述

我的要求是不同的。我有一张钥匙的图片,它在桌面上。我有相同的钥匙,在地板上。照片和按键的尺寸和大小不同,但按键相同。现在我只想比较键并显示它们是相同的。如何使用 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], [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("输入图像与原始图像不匹配")

标签: pythonopencvimage-processingimage-comparisonimageai

解决方案


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

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

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

在此处输入图像描述


推荐阅读