在 Python 中尝试使用 OpenCV 中的 cv2.matchShapes()

Fra*_*lla 6 python opencv numpy

我在白板上随机画了一张,NAO 机器人拍了一张照片并试图重新创建相同的画。

我的画:

在此处输入图片说明

NAO的图:

在此处输入图片说明

在这一点上我想写一些关于它的结论,特别是我想从两张图片中提取轮廓并使用OpenCV函数匹配轮廓cv2.matchShapes()

但是,我为此编写了一个小Python代码脚本,它给了我一些错误。这是代码:

import numpy as np
import cv2

#get the pictures from the forlder
original = cv2.imread('eightgon.jpg')
drawn = cv2.imread('eightgon1.jpg')

#make them gray    
originalGray = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)
drawnGray = cv2.cvtColor(drawn, cv2.COLOR_BGR2GRAY)

#apply erosion
kernel = np.ones((2, 2),np.uint8)
originalErosion = cv2.erode(originalGray, kernel, iterations = 1)
drawnErosion = cv2.erode(drawnGray, kernel, iterations = 1)

#retrieve edges with Canny
thresh = 175
originalEdges = cv2.Canny(originalErosion, thresh, thresh*2)
drawnEdges = cv2.Canny(drawnErosion, thresh, thresh*2)

#extract contours
originalContours, Orighierarchy = cv2.findContours(originalEdges, cv2.cv.CV_RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
drawnContours, Drawnhierarchy = cv2.findContours(drawnEdges, cv2.cv.CV_RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

print cv2.matchShapes(drawnContours,originalContours,cv2.cv.CV_CONTOURS_MATCH_I1, 0.0)
Run Code Online (Sandbox Code Playgroud)

当我运行这个简单的代码时,它会返回这个错误:

File "C:/Python27/getResults.py", line 32, in <module>
    ret = cv2.matchShapes(drawnContours,originalContours,cv2.cv.CV_CONTOURS_MATCH_I1, 0.0)
TypeError: contour1 is not a numpy array, neither a scalar
Run Code Online (Sandbox Code Playgroud)

由于错误告诉我轮廓应该是数组..我对代码进行了如下轻微更改:

cnt1 = np.asarray(drawnContours, np.int0)
cnt2 = np.asarray(originalContours, np.int0)
print cv2.matchShapes(cnt1,cnt2,cv2.cv.CV_CONTOURS_MATCH_I1, 0.0)
Run Code Online (Sandbox Code Playgroud)

在这种情况下,它返回给我这个错误: ValueError: setting an array element with a sequence.

我究竟做错了什么?任何帮助表示赞赏!

小智 -3

检查 opencv 版本,旧版本应该有不同的 matchShapes 或 findContours API。

因为遗憾的是,某些 NAO 软件版本包含一些相当旧的 opencv 版本。

考虑更新您的 NAO 软件,或者尝试获取一些测试版...