Opencv:图像寻找轮廓外接矩形Python实现
一、内容
图像二值图像的每个轮廓,OpenCV都提供了API可以求取轮廓的外接矩形,其中求取轮廓外接矩形API解释如下:
atedRect cv::minAreaRect(
InputArray points
)
输入参数points可以一系列点的集合,对轮廓来说就是该轮廓的点集
返回结果是一个旋转矩形,包含下面的信息:
- 矩形中心位置
- 矩形的宽高
- 旋转角度
二、代码
import cv2 as cv
import numpy as np
# canny边缘检测
def canny_demo(image):
t = 80
canny_output = cv.Canny(image, t, t * 2)
cv.imshow("canny_output", canny_output)
cv.imwrite("D:/vsprojects/images/canny_output.png", canny_output)
return canny_output
# 读取图像
src = cv.imread("D:/vsprojects/images/ri.jpg")
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)
# 调用
binary = canny_demo(src)
k = np.ones((3, 3), dtype=np.uint8)
binary = cv.morphologyEx(binary, cv.MORPH_DILATE, k)
# 轮廓发现
contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
for c in range(len(contours)):
rect = cv.minAreaRect(contours[c])
cx, cy = rect[0]
box = cv.boxPoints(rect)
box = np.int0(box)
cv.drawContours(src,[box],0,(0,255,0),2)
cv.circle(src, (np.int32(cx), np.int32(cy)), 2, (255, 0, 0), 2, 8, 0)
cv.drawContours(src, contours, c, (0, 0, 255), 2, 8)
# 图像显示
cv.imshow("contours_analysis", src)
cv.imwrite("D:/vsprojects/images/contours_analysis.png", src)
cv.waitKey(0)
cv.destroyAllWindows()
三、结果
1.原始输入图像
2.canny边缘检测图像
3.轮廓外接矩形图像
版权声明:本文为qq_39071739原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。