python - 如何识别具有不同背景、大小和尺寸的图像中的相同对象
问题描述
我的要求是不同的。我有一张钥匙的图片,它在桌面上。我有相同的钥匙,在地板上。照片和按键的尺寸和大小不同,但按键相同。现在我只想比较键并显示它们是相同的。如何使用 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("输入图像与原始图像不匹配")
解决方案
您可以使用 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
这个想法是在考虑单应性的两个图像中找到相同的对象特征:
推荐阅读
- r - 使用 R 中的列表替换值
- python - 从烧瓶中使用 Google API
- javascript - AsyncStorage 不会在组件加载时增加值
- c++ - 编程新手:如何在函数中调用结构的变量然后更新它们?(C++)
- javascript - 为不同的videoID添加不同的功能
- javascript - 当您的显示器屏幕分辨率不是正方形时,如何绘制圆形而不是椭圆?
- vb.net - 如果我 selectedindex = 2,面板显示/隐藏在 ComboBox Visual Basic 中不起作用
- python - Python 定时器冷却示例
- php - 在 php 中导入 .csv 文件时修复请求实体太大错误的方法
- reactjs - 尝试调用虚拟方法'android.graphics.drawable.drawable android.graphics.drawable.Drawable$constantState.newDrawable nullobject reference