Opencv-python 计算点的延长线
import numpy as np
import cv2 as cv
def yc_line(center, yc_point, dist):
c_x, c_y = center
t_x, t_y = yc_point
x_dst = np.sqrt((center[0] - yc_point[0]) ** 2)
y_dst = np.sqrt((center[1] - yc_point[1]) ** 2)
if t_y == c_y:
if t_x < c_x:
new_point = (t_x - dist, t_y)
else:
new_point = (t_x + dist, t_y)
return new_point
if t_x == c_x:
if t_y < c_y:
new_point = (t_x, t_y - dist)
else:
new_point = (t_x, t_y + dist)
return new_point
angle = np.arctan(y_dst / x_dst)
angle_dg = angle * 180 / np.pi
x_diff = np.cos(angle) * dist
y_diff = np.sin(angle) * dist
if t_x < c_x and t_y < c_y:
new_point = (t_x - x_diff, t_y - y_diff)
elif t_x < c_x and t_y > c_y:
new_point = (t_x - x_diff, t_y + y_diff)
elif t_x > c_x and t_y < c_y:
new_point = (t_x + x_diff, t_y - y_diff)
else:
new_point = (t_x + x_diff, t_y + y_diff)
new_point = (int(new_point[0]), int(new_point[1]))
return new_point
def main():
center = (200, 200)
left_top = (100, 150)
left_bottom = (100, 300)
right_top = (300, 100)
right_bottom = (300, 300)
img_org = np.zeros((400, 400, 3), np.uint8)
put_point = yc_line(center, left_top, 20)
cv.line(img_org, center, put_point, color=(255, 255, 255), thickness=2)
cv.circle(img_org, put_point, 5, (255, 255, 0), -1)
put_point = yc_line(center, left_bottom, 20)
cv.line(img_org, center, put_point, color=(255, 255, 255), thickness=2)
cv.circle(img_org, put_point, 5, (255, 255, 0), -1)
put_point = yc_line(center, right_top, 20)
cv.line(img_org, center, put_point, color=(255, 255, 255), thickness=2)
cv.circle(img_org, put_point, 5, (255, 255, 0), -1)
put_point = yc_line(center, right_bottom, 20)
cv.line(img_org, center, put_point, color=(255, 255, 255), thickness=2)
cv.circle(img_org, put_point, 5, (255, 255, 0), -1)
cv.circle(img_org, center, 5, (0, 0, 255), -1)
cv.circle(img_org, left_top, 5, (255, 0, 0), -1)
cv.circle(img_org, left_bottom, 5, (255, 0, 0), -1)
cv.circle(img_org, right_top, 5, (255, 0, 0), -1)
cv.circle(img_org, right_bottom, 5, (255, 0, 0), -1)
cv.imshow('img_org', img_org)
cv.waitKey()
cv.destroyAllWindows()
if __name__ == '__main__':
main()
![在这里插入图片描述](https://img-blog.csdnimg.cn/6828923559ce442495fb324863988a65.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bu355uKLS3po57puJ8=,size_12,color_FFFFFF,t_70,g_se,x_16#pic_center)