opencv定义的旋转框角度情况说明
目录
1. 环境
2. 函数参数说明
3. 实际操作
4. 图解
'''
python 3.5+
opencv-python 4.5.1+
由于本人探索了一段时间的影像旋转目标检测,只要涉及角度定义,总是出现问题,
百度到的博客,都是老旧的,和实际执行效果有所出入,因此,重新可视化,发现问题。
opencv版本,是角度定义处理的关键,不同版本定义有很大差别,
这里以最新的opencv-python4.5.1为例:
如图,如代码,角度定义为:“顺时针”与“X轴正向”最近的边的夹角,角度范围为[0,90],而非(0,90]
'''
points为 shape=[N,2]的Ndarray,要求使用int32、int64的array,对于float32不推荐,因为结果不一样!
eg:points=np.array([[x1,y2],[x2,y2],[x3,y3],[x4,y4],[x5,y5]], dtype=np.int64)
import cv2
import numpy as np
def draw_rbbox(img, points):
img = cv2.drawContours(img, [points], 0, (255, 0, 0), 2)
rect = cv2.minAreaRect(points)
print(rect)
points_rect = cv2.boxPoints(rect)
box = np.int0(points_rect)
img = cv2.drawContours(img, [box], 0, (0, 0, 255), 2)
return img
print(cv2.__version__) # 4.5.1
img = np.zeros([500,500,3], dtype=np.uint8)
# 样例1
points1 = np.array([[100,100],[180,200],[140,270],[60,160]])
# 样例2
points2 = np.array([[320.4896, 306.2144],
[320.4896, 281.0297],
[381.2337, 281.0297],
[381.2337, 306.2144]], dtype=np.int64)
# 样例3
points3 = np.array([[222.4992, 345.3907],
[222.4992, 317.4076],
[281.2018, 317.4076],
[281.2018, 345.3907]], dtype=np.int64)
# 样例4
points4 = np.array([[450,100],[480,160],[200,300],[150,240]])
img = draw_rbbox(img, points1)
img = draw_rbbox(img, points2)
img = draw_rbbox(img, points3)
img = draw_rbbox(img, points4)
cv2.imwrite(r'/home/111.jpg', img)
'''
样例依次对应xywh-angle
((120.000, 185.0000), (157.735, 74.963), 51.3401)
((350.499, 293.499), (60.999, 24.999), 0.0)
((251.5, 331.0), (28.0, 59.0), 90.0)
((316.7883, 203.832), (75.515, 332.871), 64.9831)
'''