【发布时间】:2020-04-24 06:21:27
【问题描述】:
我对以下代码有疑问: 我想在这个函数中返回 2 个值,一个是 orig_image 很好,另一个是 cx,cx 是最大轮廓中心的 x 坐标,它是一定范围内的区域。终端说“ValueError:解压的值太多(预期为 2)”。(已解决) 下一个问题弹出:“IndexError: index 0 is out of bounds for axis 0 with size 0?” 可以知道原因和解决方法吗?非常感谢!
def findContours(image):
orig_image = image.copy()
image2 = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, image2 = cv2.threshold(image2, 127, 255, 1)
image2 = cv2.Canny(image2,30,200)
# Find contours
contours, hierarchy = cv2.findContours(image2.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
max_area = 200000000000000
min_area = 100
areaList=[]
cxList=[]
if contours is not None:
for c in contours:
area = cv2.contourArea(c)
if min_area < area < max_area:
areaList.append(area)
approx = cv2.approxPolyDP(c, 0.03*cv2.arcLength(c,True),False)
cv2.drawContours(orig_image, [c], 0,(0,255,0), -1)
# cv2.drawContours(orig_image, [c], 0,(225,0,0), -1)
x,y,w,h = cv2.boundingRect(c)
# cv2.rectangle(orig_image3,(x,y),(x+w,y+h),(0,0,255),2)
cx=x+int(w/2)
cxList.append(cx)
cy=y+int(h/2)
cv2.circle(orig_image, (cx,cy), 5, (0,0,255), -1)
if areaList:
maxArea=max(areaList)
index=np.where(areaList==maxArea)
targetCx= cxList[index[0][0]]
return targetCx #this is where the problem occur
return orig_image #this is where the problem occur
def main():
targetCx , original_image = findContours(original_image) #this is where
the problem occur
终端:
targetCx , original_image = findContours(original_image)
ValueError: too many values to unpack (expected 2)
如果我将最后一部分更改为:
if areaList:
maxArea=max(areaList)
index=np.where(areaList==maxArea)
targetCx= cxList[index[0][0]]
return targetCx
else:
return None
return orig_image
终端节目:
targetCx , original_image = findContours(original_image)
TypeError: cannot unpack non-iterable NoneType object
更新
def findContours(image):
orig_image = image
image2 = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, image2 = cv2.threshold(image2, 127, 255, 1)
image2 = cv2.Canny(image2,30,200)
# Find contours
contours, hierarchy = cv2.findContours(image2.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
max_area = 200000000000000
min_area = 100
areaList=[]
cxList=[]
targetCx =None
if contours is not None:
for c in contours:
area = cv2.contourArea(c)
if min_area < area < max_area:
areaList.append(area)
approx = cv2.approxPolyDP(c, 0.03*cv2.arcLength(c,True),False)
cv2.drawContours(orig_image, [c], 0,(0,255,0), -1)
# cv2.drawContours(orig_image, [c], 0,(225,0,0), -1)
x,y,w,h = cv2.boundingRect(c)
# cv2.rectangle(orig_image3,(x,y),(x+w,y+h),(0,0,255),2)
cx=x+int(w/2)
cxList.append(cx)
cy=y+int(h/2)
cv2.circle(orig_image, (cx,cy), 5, (0,0,255), -1)
if areaList:
maxArea=max(areaList)
index=np.where(areaList==maxArea)
targetCx = cxList[index[0][0]]
return targetCx,orig_image
终端:
targetCx = cxList[index[0][0]]
IndexError: index 0 is out of bounds for axis 0 with size 0
【问题讨论】:
-
你能分享错误来自哪一行吗?
-
好的,我已经添加了,请看一下thx!
-
根据 OpenCV 版本,
findContours具有不同的返回签名。在 OpenCV 3.4.X 中它返回 3 个项目,而 OpenCV 4.1.X 返回 2 个项目。查看this post了解更多信息 -
where可能没有找到任何真实值。单独看index。 -
测试
shoulds的唯一方法是debug或打印一些中间值。我不信任shoulds,我的或其他任何人的。
标签: python numpy opencv matplotlib image-processing