Xia*_*ENG 5 python opencv rotation computer-vision drawrectangle
我有中心坐标,旋转角度,宽度和高度。有了这个,我想使用旋转角度在原始图像上绘制一个旋转的框。Python2.7 中是否有任何方法可以实现我的要求?
它应该看起来像这个图像作为红色框。我不想提取轮廓,因为我已经有了你想要的任何坐标。
我在这里添加我的代码来完成这个问题。我使用旋转矩阵找到四个角点然后画线。
def draw_rectangle(image, centre, theta, width, height):
theta = np.radians(theta)
c, s = np.cos(theta), np.sin(theta)
R = np.matrix('{} {}; {} {}'.format(c, -s, s, c))
# print(R)
print centre[0]
p1 = [ + width / 2, + height / 2]
p2 = [- width / 2, + height / 2]
p3 = [ - width / 2, - height / 2]
p4 = [ + width / 2, - height / 2]
p1_new = np.dot(p1, R)+ centre
p2_new = np.dot(p2, R)+ centre
p3_new = np.dot(p3, R)+ centre
p4_new = np.dot(p4, R)+ centre
print p1_new
img = cv2.line(image, (int(p1_new[0, 0]), int(p1_new[0, 1])), (int(p2_new[0, 0]), int(p2_new[0, 1])), (255, 0, 0), 1)
img = cv2.line(img, (int(p2_new[0, 0]), int(p2_new[0, 1])), (int(p3_new[0, 0]), int(p3_new[0, 1])), (255, 0, 0), 1)
img = cv2.line(img, (int(p3_new[0, 0]), int(p3_new[0, 1])), (int(p4_new[0, 0]), int(p4_new[0, 1])), (255, 0, 0), 1)
img = cv2.line(img, (int(p4_new[0, 0]), int(p4_new[0, 1])), (int(p1_new[0, 0]), int(p1_new[0, 1])), (255, 0, 0), 1)
img = cv2.line(img, (int(p2_new[0, 0]), int(p2_new[0, 1])), (int(p4_new[0, 0]), int(p4_new[0, 1])), (255, 0, 0), 1)
img = cv2.line(img, (int(p1_new[0, 0]), int(p1_new[0, 1])), (int(p3_new[0, 0]), int(p3_new[0, 1])), (255, 0, 0), 1)
return img
Run Code Online (Sandbox Code Playgroud)
结果显示如下: 结果
| 归档时间: |
|
| 查看次数: |
5955 次 |
| 最近记录: |