ret, thresh = cv2.threshold(imm, 127, 255, cv2.THRESH_BINARY) # 寻找图像中的边缘 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}, TopLeft=({int(rect[0][0])}, {int(rect[0][1])}), Width={rect[1][0]:.0f}, Height={rect[1][1]:.0f}, Angle={rect[2]:.0f}')将此代码中心点坐标改为角的坐标
时间: 2024-01-31 22:03:40 浏览: 138
要将中心点坐标改为角的坐标,只需要将代码中的 `rect[0]` 改为 `box[0]` 即可,修改后的代码如下:
```python
ret, thresh = cv2.threshold(imm, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img_bgr = cv2.cvtColor(imm, cv2.COLOR_GRAY2BGR)
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}, TopLeft=({int(box[0][0])}, {int(box[0][1])}), Width={rect[1][0]:.0f}, Height={rect[1][1]:.0f}, Angle={rect[2]:.0f}')
```
阅读全文