import numpy as np
rectangle = [cx,cy,w,h,θ]
x1 = int(np.cos(rectangle[4])*(-rectangle[2]/2) - np.sin(rectangle[4])*(-rectangle[3]/2) + rectangle[0])
x2 = int(np.cos(rectangle[4])*(rectangle[2]/2) - np.sin(rectangle[4])*(-rectangle[3]/2) + rectangle[0])
x3 = int(np.cos(rectangle[4])*(-rectangle[2]/2) - np.sin(rectangle[4])*(rectangle[3]/2) + rectangle[0])
x4 = int(np.cos(rectangle[4])*(rectangle[2]/2) - np.sin(rectangle[4])*(rectangle[3]/2) + rectangle[0])
y1 = int(np.sin(rectangle[4])*(-rectangle[2]/2) + np.cos(rectangle[4])*(-rectangle[3]/2) + rectangle[1])
y2 = int(np.sin(rectangle[4])*(rectangle[2]/2) + np.cos(rectangle[4])*(-rectangle[3]/2) + rectangle[1])
y3 = int(np.sin(rectangle[4])*(-rectangle[2]/2) + np.cos(rectangle[4])*(rectangle[3]/2) + rectangle[1])
y4 = int(np.sin(rectangle[4])*(rectangle[2]/2) + np.cos(rectangle[4])*(rectangle[3]/2) + rectangle[1])
关于怎么绘制实心矩形,因为openCV本身的cv2.rectangle只能绘制角度为0的矩形,没办法用,所以一开始打算遍历每个点判断是否在矩形内,来绘制
from shapely import geometry
def ifPointsInside(polygon, Points):
line = geometry.LineString(polygon)
point = geometry.Point(Points)
polygon = geometry.Polygon(line)
return polygon.contains(point)
结果因为每个点都要遍历计算,我是用来处理深度学习资料的,一万多张图片,640*480分辨率,那就是30多亿次,他没遍历完,我人就先走了,太慢
然后想到了:先用cv2.line绘制边框,然后图像按横遍历找里面的255值,找到两个255的话就把这两个中间的全都标记255,成功了,不过还是慢
最后解决方法:用cv2.line一条线一条线的画,从(x1,y1)->(x2,y2)一直画到(x3,y3)->(x4,y4)
Output = np.zeros((480,640,1))
#我的用单通道,三通道就把1改成3,下面的(255)改成(255,255,255)
a = (y3-y1)/(x3-x1)
#这个是用来计算斜率
for x in range(0,abs(x3-x1)):
cv2.line(Output,(int(x1+x),int(y1+a*x)),(int(x2+x),int(y2+a*x)), (255), 2)
按1pixel画,精度不够,改为0.01步伐
Output = np.zeros((480,640,1))
a = (y3-y1)/(x3-x1)
for x in range(0,100*abs(x3-x1)):
cv2.line(Output,(int(x1+x/100),int(y1+a*x/100)),(int(x2+x/100),int(y2+a*x/100)), (255), 2)