[英]Sort associated Hierarchy with already sorted Contours in OpenCv in Python
[英]sort the crop contours python and opencv
contours, hierarchy = cv2.findContours(opening,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
idx =0
for cnt in contours:
if cv2.contourArea(cnt) >3000:
idx += 1
x,y,w,h = cv2.boundingRect(cnt)
roi=img[y:y+h,x:x+w]
cv2.imwrite(str(idx) + ".jpg", roi)
cv2.rectangle(img,(x,y),(x+w,y+h),(200,0,0),2)
它有效,但是作物轮廓是随机保存的。 我需要保存的裁剪图像相对于X值从低到高。
等高线以(大约)递增的Y顺序生成-因此边界框的X坐标将是伪随机的(仅取决于图像)
您将必须累积BoundingRects,将它们按x排序,然后进行区域划分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.