我正在使用开放式cv进行图像分析,但索引超出了范围错误。有没有办法使轮廓的长度等于x和y的mass_centres。任何帮助将不胜感激。谢谢
frame = cv2.imread('mes.jpg')
if frame is None:
print('Error loading image')
exit()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ret, frame = cv2.threshold(frame, 127, 255, cv2.THRESH_BINARY)
frame, contours, hierarchy = cv2.findContours(frame, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
areas = []
for i in range(0, len(contours)):
areas.append(cv2.contourArea(contours[i]))
mass_centres_x = []
mass_centres_y = []
for i in range(0, len(contours)):
if M["m00"] != 0:
M = cv2.moments(contours[i], 0)
mass_centres_x.append(int(M['m10']/M['m00']))
mass_centres_y.append(int(M['m01']/M['m00']))
print('Num particles: ', len(contours))
for i in range(0, len(contours)):
print ('Area', (i + 1), ':', areas[i])
for i in range(0, len(contours)):
print ('Centre',(i + 1),':', mass_centres_x[i], mass_centres_y[i])
cv2.imshow("Frame", frame)
cv2.waitKey(0)