I have the center coordinate, rotation angle, width and height. With this I want to draw a rotated box on the original image using the rotation angle. Is there any method in Python2.7 to achieve my requirement?
It should looks like this image as red box. I don't want to extract contour because I already have any co-ordinate you want.
I add my code here to finish this question. I use rotation matrix to find four corner points then draw lines.
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
The result shows as following: Result