<>题目:给出旋转矩形的四个顶点 p1(x1, y1), p2(x2, y2), p3(x3, y3), p4(x4, y4) 或者 中心点
(cx,cy),(w,h),θ 画一个旋转的实心矩阵:输出numpy数组,矩形内的值全部为255

<>一、通过中心点、宽高、角度得到四个顶点的公式网上都有,最后输出四个顶点的x和y坐标。
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)
画出来了

技术
下载桌面版
GitHub
Gitee
SourceForge
百度网盘(提取码:draw)
云服务器优惠
华为云优惠券
腾讯云优惠券
阿里云优惠券
Vultr优惠券
站点信息
问题反馈
邮箱:[email protected]
吐槽一下
QQ群:766591547
关注微信