我在白板上画了一幅随机图案,NAO机器人拍摄了照片并试图重新绘制同样的图案。
我的绘画:
NAO的绘画:
此时,我想写一些关于它的结论,具体来说,我想从两张图片中提取轮廓,并使用OpenCV函数cv2.matchShapes()匹配这些轮廓。
然而,我为此编写了一个小的Python代码脚本,但它给了我一些错误。以下是代码:
当我运行这段简单的代码时,它返回了以下错误:
在这种情况下,它返回给我这个错误:
我的绘画:
![enter image description here](https://istack.dev59.com/TFs44.webp)
![enter image description here](https://istack.dev59.com/Hm4Xd.webp)
然而,我为此编写了一个小的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)
当我运行这段简单的代码时,它返回了以下错误:
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
由于错误提示我轮廓应该是数组..所以我对代码进行了轻微的更改,像这样:
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)
在这种情况下,它返回给我这个错误:
ValueError: setting an array element with a sequence.
我做错了什么?
任何帮助都将不胜感激!