import cv2
import numpy as np
def find_min_area_rect_and_rotate(image_path, target_angle=0):
# 读取图片并转换为灰度图
img = cv2.imread(image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 对灰度图进行二值化处理
_, thresh = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)
# 查找轮廓
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 找到最大的轮廓(假设为白色区域)
max_area = 0
max_cnt = None
for cnt in contours:
area = cv2.contourArea(cnt)
if area > max_area:
max_area = area
max_cnt = cnt
# 计算最小外接矩形
rect = cv2.minAreaRect(max_cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
python opencv 二值图,计算白色区域的最小外接矩形,并判断角度,然后旋转到指定角度
最新推荐文章于 2024-09-06 09:14:53 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)