python-opencv 的minAreaRect的详解

来源:互联网 发布:mysql 添加外键约束 编辑:程序博客网 时间:2024/05/30 02:25

minAreaRect返回矩形的中心点坐标,长宽,旋转角度

度值取值范围为[-90,0),当矩形水平或竖直时均返回-90

# -*- coding:UTF_8 -*-
import cv2
# 读取图片img = cv2.imread('1.jpg')cv2.imshow("src", img)# 灰度处理gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 二值化ret, img2 = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)# 寻找连通矩形contours, hierarchy = cv2.findContours(img2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)for contour in contours:    # 获取最小包围矩形    rect = cv2.minAreaRect(contours[0])    # 中心坐标    x, y = rect[0]    cv2.circle(img, (int(x), int(y)), 3, (0, 255, 0), 5)    # 长宽,总有 width>=height    width, height = rect[1]    # 角度:[-90,0)    angle = rect[2]    cv2.drawContours(img, contour, -1, (255, 255, 0), 3)    print 'width=', width, 'height=', height, 'x=', x, 'y=', y, 'angle=', anglecv2.imshow("contour", img)rows, cols = img2.shape# 逆时针旋转30度M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 30, 1)img = cv2.warpAffine(img, M, (cols, rows))cv2.imshow("rotation", img)cv2.waitKey()cv2.destroyAllWindows()


原创粉丝点击