I want to draw a straight line in the angled rectangle using python and opencv.

Asked 2 years ago, Updated 2 years ago, 359 views

How do I draw a straight line with the same length as the long side of the rectangle in an angled rectangle?
The language is Python, and the library used for drawing is OpenCV.
The versions are as follows:
Python: 3.7.9
OpenCV: 4.6.0

A more detailed description of the straight line is a vertical line drawn from the short side of a rectangle to the short side facing it. The information available is as follows

I would like to draw the red straight line of the attached image using these 5 pieces of information.
The distance to the straight line of 4 shows the length of the green straight line in the attached image.
Image Diagram of the rectangle you want to draw

For more information on drawing a rectangle with an angle of rotation, refer to the How to draw a rectangle with an angle of rotation site.
The actual code is as follows

def draw_rotate_rect(img,rect,color):
    # rect contains the upper left coordinates of the rectangle before rotation
    center_x=rect[0]+rect[2]//2
    center_y=rect[1]+rect[3]//2
    w,h,ang = rect[2], rect[3], -rect[4]

    rot_rect=(center_x,center_y), (w,h),ang)
    box=cv2.boxPoints(rot_rect)
    box=np.int0(box)

    img = cv2.drawContours(img, [box], 0, color, 1)

python python3 opencv

2022-10-28 00:01

2 Answers

boxPoints to find the coordinates after rotation, so you can draw them by finding the coordinates of a rectangle with shorter ends as much as the length of the green line.

sample code

import cv2
import numpy as np

# The `len` argument is an integer equivalent to the length of a green straight line.
def draw_rotate_rect(img,rect,color,len):
    # rect contains the upper left coordinates of the rectangle before rotation
    center_x=rect[0]+rect[2]//2
    center_y=rect[1]+rect[3]//2
    w,h,ang = rect[2], rect[3], -rect[4]

    rot_rect=(center_x,center_y), (w,h),ang)
    box=cv2.boxPoints(rot_rect)
    box=np.int0(box)

    img = cv2.drawContours(img, [box], 0, color, 1)

    # find the coordinates by rotating the narrow border line by the width of len*2 (green line)
    rect2 = ((center_x, center_y), (rect[2]-len*2, rect[3]), ang)
    b2 = cv2.boxPoints (rect2)
    pt1_1=(int(b2[0][0]), int(b2[0][1]))#Lower left
    pt1_2=(int(b2[1][0]), int(b2[1][1]))#Top left
    img=cv2.line(img,pt1_1,pt1_2,(0,0,255),1)#Red Line
    pt2_1 = (int(b2[2][0]), int(b2[2][1])) # upper right
    pt2_2=(int(b2[3][0]), int(b2[3][1]))#Lower right
    img=cv2.line(img,pt2_1,pt2_2,(0,255,0),1)#green line

    # The height is as narrow as len*2 (green line) and below.
    rect3 = ((center_x, center_y), (rect[2], rect[3]-len*2), ang)
    b3 = cv2.boxPoints (rect3)
    pt3_1=(int(b3[0][0]), int(b3[0][1]))#Lower left
    pt3_2=(int(b3[3][0]), int(b3[3][1]))#Lower right
    img=cv2.line(img,pt3_1,pt3_2, (255,0,0),1)#blue line
    pt4_2=(int(b3[1][0]), int(b3[1][1]))#Top left
    pt4_1 = (int(b3[2][0]), int(b3[2][1])) # upper right
    img=cv2.line(img,pt4_1,pt4_2,(200,200,0),1)#light blue line

img=np.zeros(480,640,3), np.uint8)
draw_rotate_rect(img,(100,100,100,300,10),(255,255,255),20)
cv2.imwrite("test.png", img)

Run Results

Run Results


2022-10-28 00:01

Draw a straight line with the same length as the long side of the rectangle in an angled rectangle

Therefore, the angled rectangle is also an object of drawing, and for that purpose, box=cv2.boxPoints(rot_rect) is performed for presentation code in question. On the premise that
At this point, the coordinates of the four vertices of the rectangle are obtained, i.e., the coordinates of both ends of each side, so the coordinates of the points on the side can simply be found from the ratio.


2022-10-28 00:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.