目录
python 任意两个顶点画正方形,两个顶点是正方形的对角点
python 任意两个顶点画正方形,两个顶点是正方形的对角点
import cv2
import numpy as np
def draw_square(image, point1, point2):
x1, y1 = point1
x2, y2 = point2
center_x, center_y = (x1 + x2) / 2, (y1 + y2) / 2
side_length = np.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) / np.sqrt(2)
angle =- np.arctan2(y2 - y1, x2 - x1) * 180 / np.pi - 45
rect_center = (center_x, center_y)
rect_size = (side_length, side_length)
square_points = get_rotated_box_points(rect_center, rect_size, angle)
cv2.drawContours(image, [square_points], 0, (0, 255, 0), 2)
def get_rotated_box_points(center, size, angle):
angle = np.deg2rad(angle)