我是新來的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