contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) img_bgr = cv2.cvtColor(imm, cv2.COLOR_GRAY2BGR) # 绘制边缘 # imm = cv2.drawContours(img_bgr, contours, -1, (0, 0, 255), 1) # 遍历轮廓 rects = [] for contour in contours: rect = cv2.minAreaRect(contour) rects.append(rect) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(img_bgr, [box], 0, (0, 0, 255), 2) for i, rect in enumerate(sorted(rects, key=lambda x: x[1][0] * x[1][1], reverse=True)): print(f'Rank {i + 1}: Size={rect[1][0] * rect[1][1]:.0f}, ({int(rect[0][0])}, {int(rect[0][1])}), Width={rect[1][0]:.0f}, Height={rect[1][1]:.0f}, Angle={rect[2]:.0f}')打印的具体是什么帮我再加上矩形轮廓的四个角的坐标
时间: 2024-01-16 22:02:42 浏览: 128
python cv2截取不规则区域图片实例
除了之前提到的矩形的信息,代码还绘制了矩形轮廓的四个角的坐标,并将其打印出来。例如,“[(45, 45), (55, 45), (55, 55), (45, 55)]”表示矩形轮廓的四个角的坐标为(45,45),(55,45),(55,55),(45,55)。下面是修改后的代码:
```
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img_bgr = cv2.cvtColor(imm, cv2.COLOR_GRAY2BGR)
# 绘制边缘
# imm = cv2.drawContours(img_bgr, contours, -1, (0, 0, 255), 1)
# 遍历轮廓
rects = []
for contour in contours:
rect = cv2.minAreaRect(contour)
rects.append(rect)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(img_bgr, [box], 0, (0, 0, 255), 2)
# 获取矩形的四个角的坐标
pts = np.int0(cv2.boxPoints(rect))
# 打印矩形的四个角的坐标
print(f'Rectangle Points: {pts.tolist()}')
for i, rect in enumerate(sorted(rects, key=lambda x: x[1][0] * x[1][1], reverse=True)):
print(f'Rank {i + 1}: Size={rect[1][0] * rect[1][1]:.0f}, ({int(rect[0][0])}, {int(rect[0][1])}), Width={rect[1][0]:.0f}, Height={rect[1][1]:.0f}, Angle={rect[2]:.0f}')
```
阅读全文