Skip to content

Commit a3e3295

Browse files
committed
minAreaRect-旋转矩形
1 parent 56d4e8d commit a3e3295

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/7/27 12:33
3+
# @Author : play4fun
4+
# @File : minAreaRect-旋转矩形.py
5+
# @Software: PyCharm
6+
7+
"""
8+
minAreaRect-旋转矩形.py:
9+
"""
10+
11+
import cv2
12+
import numpy as np
13+
14+
img = cv2.imread('../data/lightning.png')
15+
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
16+
17+
image, contours, hierarchy = cv2.findContours(imgray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
18+
contours = [cnt for cnt in contours if cv2.contourArea(cnt) > 100]
19+
print('len(contours)', len(contours))
20+
cnt = contours[0]
21+
22+
rect = cv2.minAreaRect(cnt)
23+
box = cv2.boxPoints(rect)
24+
box = np.int0(box)
25+
img = cv2.drawContours(img, [box], 0, (0, 0, 255), 2)
26+
27+
cv2.imshow('fd', img)
28+
cv2.waitKey(0)
29+
cv2.destroyAllWindows()

ch21-轮廓Contours/凸包-凸性检测-边界矩形-最小外接圆-拟合.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
凸包-凸性检测-边界矩形-最小外接圆-拟合.py:
99
"""
1010
import cv2
11+
import numpy as np
1112

13+
img=cv2.imread('../data/lightning.png',0)
14+
15+
image, contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
16+
17+
cnt=contours[0]
1218
'''
1319
函数 cv2.convexHull() 可以用来检测一个曲线是否具有凸性缺 并能纠 正缺 。一般来 凸性曲线总是凸出来的 至少是平的。如果有地方凹 去 了就 叫做凸性缺
1420
例如下图中的手。红色曲线显示了手的凸包 凸性缺 双箭头标出来了。
@@ -30,17 +36,20 @@
3036

3137
# 边界矩形
3238
'''
33-
直边界矩形 一个直矩形 就是没有旋 的矩形 。它不会考虑对象是否旋转。 所以边界矩形的 积不是最小的。可以使用函数 cv2.boundingRect() 查 找得到。
39+
直边界矩形 一个直矩形 就是没有旋转的矩形 。它不会考虑对象是否旋转。 所以边界矩形的 积不是最小的。可以使用函数 cv2.boundingRect() 查 找得到。
3440
x y 为矩形左上角的坐标 w h 是矩形的宽和 。
3541
'''
3642
x, y, w, h = cv2.boundingRect(cnt)
3743
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
3844

3945
'''
40-
旋 的 界矩形 个 界矩形是 积最小的 因为它考 了对 的旋 。用 到的函数为 cv2.minAreaRect()。 回的是一个 Box2D 结构 其中包含 矩形左上 点的坐标 x y 矩形的宽和 w h 以及旋 度。但是 绘制 个矩形 矩形的 4 个 点 可以 函数 cv2.boxPoints() 获 得。
46+
旋转矩形
47+
这里,以最小面积绘制边界矩形,因此也考虑旋转。使用的功能是cv2.minAreaRect()。它返回一个Box2D结构,其中包含以下条件 - (中心(x,y),(宽度,高度),旋转角度)。但是要绘制这个矩形,我们需要矩形的四个角。它是通过函数cv2.boxPoints()
4148
'''
42-
x, y, w, h = cv2.boundingRect(cnt)
43-
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
49+
rect = cv2.minAreaRect(cnt)
50+
box = cv2.boxPoints(rect)
51+
box = np.int0(box)
52+
cv2.drawContours(img,[box],0,(0,0,255),2)
4453

4554
# 最小外接圆
4655
# 函数 cv2.minEnclosingCircle() 可以帮我们找到一个对 的外切圆。它是所有能够包括对 的圆中 积最小的一个。
@@ -52,7 +61,7 @@
5261
# 椭圆拟合
5362
# 使用的函数为 cv2.ellipse() 回值其实就是旋 界矩形的内切圆
5463
ellipse = cv2.fitEllipse(cnt)
55-
im = cv2.ellipse(im, ellipse, (0, 255, 0), 2)
64+
im = cv2.ellipse(img, ellipse, (0, 255, 0), 2)
5665

5766
# 直线拟合
5867
# 我们可以根据一组点拟合出一条直线 同样我们也可以为图像中的白色点 拟合出一条直线。

data/lightning.png

4.18 KB
Loading

0 commit comments

Comments
 (0)