我是新来的python和我试图编写一个代码,从图像中提取轮廓和排序他们轮廓列表中元素长度的升序。当我使用sort()或list.sort()时,出现错误:操作数不能与形状一起广播(1776,1,2)(3896,1,2) 我该如何解决这个问题? 这是我正在使用的image。错误:操作数无法与形状一起广播(1776,1,2)(3896,1,2)
我得到的错误信息是:
Traceback (most recent call last):
File "/home/dehaoliu/opencv_test/Engineering drawings/example.py", line 19, in <module>
contours.sort()
ValueError: operands could not be broadcast together with shapes (1776,1,2) (3896,1,2)
下面是缩短代码产生错误:
import cv2
import numpy as np
from math import sqrt
name='20_right_5-1'
img = cv2.imread(name+'.JPG')
im = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imwrite(name+"_dilation.jpg", closing)
im = cv2.imread(name+'_dilation.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
#ret,thresh = cv2.threshold(imgray,127,255,cv2.THRESH_BINARY_INV)
blur = cv2.GaussianBlur(imgray,(5,5),0)
ret,thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cv2.drawContours(im, contours, -1, (0,255,0), 3)
cv2.namedWindow("Contours")
cv2.imshow("Contours", im)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite(name+"_contour.jpg", im)
print "this is contours"
print contours
print type(contours)
contours.sort()
这很容易_make错误走开,确保结果是你想要的结果并不容易。你是否了解这些维度在错误中的含义,以及预期结果的大小应该是多少? –
我相信它们是列表中第一个和第二个元素的尺寸 – Alizay
对不起如果这是一个愚蠢的问题。我以前没有使用python的经验,现在我一直试图弄清楚这一点。 – Alizay