opencv—python 最小外接矩形
(编程小白,如有问题还请各位大佬多指教)
cv2.threshold() —— 阈值处理
cv2.findContours() —— 轮廓检测
cv2.boundingRect() —— 最大外接矩阵
cv2.rectangle() —— 画出矩形
cv2.minAreaRect —— 找到最小外接矩形(矩形具有一定的角度)
cv2.boxPoints —— 外接矩形的坐标位置
cv2.drawContours(image, [box], 0, (0, 0, 255), 3) —— 根据点画出矩形
import cv2
import numpy as np
image = cv2.imread('new.jpg')
img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img, 230, 255, cv2.THRESH_BINARY_INV)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
# 找到边界坐标
x, y, w, h = cv2.boundingRect(c) # 计算点集最外面的矩形边界
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
# 找面积最小的矩形
rect = cv2.minAreaRect(c)
# 得到最小矩形的坐标
box = cv2.boxPoints(rect)
# 标准化坐标到整数
box = np.int0(box)
# 画出边界
cv2.drawContours(image, [box], 0, (0, 0, 255), 3)
# 计算最小封闭圆的中心和半径
(x, y), radius = cv2.minEnclosingCircle(c)
# 换成整数integer
center = (int(x),int(y))
radius = int(radius)
# 画圆
cv2.circle(image, center, radius, (0, 255, 0), 2)
cv2.drawContours(image, contours, -1, (255, 0, 0), 1)
cv2.imshow("img", image)
cv2.imwrite("img_1.jpg", image)
cv2.waitKey(0)