由于在python中,不具有直接画旋转矩阵的能力,于是写了一个画旋转矩阵的方法,大体思路就是计算出来这矩形四点的位置坐标,然后从1点开始逐点画线。写的不好(我本来预设逆时针为正值,写完测试发现顺时针为正)。
这是抓取点检测里面的图片,以它为例,进行的计算。水平为x轴,竖直为y轴。
ptrhon代码:
import cv2
import math
img = cv2.imread("C:\\Users\\Dell\\Desktop\\grasp_rep.png")
w = 100
h = 80
center = (220,140)
x = center[0]
y = center[1]
angel = 30
angelPi = (angel/180)*math.pi
x1 = x + (w/2)*math.cos(angelPi) - (h/2)*math.sin(angelPi)
y1 = y + (w/2)*math.sin(angelPi) + (h/2)*math.cos(angelPi)
x2 = x + (w/2)*math.cos(angelPi) + (h/2)*math.sin(angelPi)
y2 = y + (w/2)*math.sin(angelPi) - (h/2)*math.cos(angelPi)
x3 = x - (w/2)*math.cos(angelPi) + (h/2)*math.sin(angelPi)
y3 = y - (w/2)*math.sin(angelPi) - (h/2)*math.cos(angelPi)
x4 = x - (w/2)*math.cos(angelPi) - (h/2)*math.sin(angelPi)
y4 = y - (w/2)*math.sin(angelPi) + (h/2)*math.cos(angelPi)
cv2.circle(img,center,2,(0,0,0),thickness=2)
cv2.line(img,(int(x1),int(y1)),(int(x2),int(y2)),(250,0,0),thickness=2,)
cv2.line(img,(int(x2),int(y2)),(int(x3),int(y3)),(0,0,250),thickness=2)
cv2.line(img,(int(x3),int(y3)),(int(x4),int(y4)),(250,0,0),thickness=2)
cv2.line(img,(int(x4),int(y4)),(int(x1),int(y1)),(0,0,250),thickness=2)
cv2.imshow('grasp_box',img)
cv2.waitKey(-1)
运行结果如下图所示: