Home > database >  Draw a Line in OpenCV and Python beyond given points
Draw a Line in OpenCV and Python beyond given points

Time:02-28

so my goal is it to draw a line in opencv that continous after the second given point.

I drew an image which should explain it better. The green drawing is the line i want. It starts at point 1 and doesnt end in point 2.

My Paint drawing

Thank you very much for your help

CodePudding user response:

Drawing a line between two points is like drawing a triangle. I got a proportion by using the greater of the distances on the x and y axis, and by enlarging my triangle to the screen dimensions, I reached the line you want.

import cv2
import numpy as np

sizeofscr = 500

blank_image = np.zeros((sizeofscr,sizeofscr,3), np.uint8)

p1 = (290,230)
p2 = (100,100)

blank_image = cv2.circle(blank_image, p1, 10, (255, 0, 0), 2)
blank_image = cv2.circle(blank_image, p2, 10, (255, 0, 0), 2)

fx = abs(p1[0] - p2[0])
fy = abs(p1[1] - p2[1])

if (p1[0] p1[1]) > (p2[0] p2[1]):
    if fx < fy:
        temp = abs(sizeofscr - p1[1]) / fy

        fx = (temp * fx)   p1[0]
        fy = (temp * fy)   p1[1]

        blank_image = cv2.line(blank_image,(int(fx),int(fy)),p2,(0,0,255),3)

    else:
        temp = abs(sizeofscr - p1[0]) / fx
    
        fx = (temp * fx)   p1[0]
        fy = (temp * fy)   p1[1]
    
        blank_image = cv2.line(blank_image,(int(fx),int(fy)),p2,(0,0,255),3)    
else:
    if fx < fy:
        temp = abs(sizeofscr - p1[1]) / fy

        fx = (temp * fx)   p1[0]
        fy = (temp * fy)   p1[1]

        blank_image = cv2.line(blank_image,(int(fx),int(fy)),p1,(0,0,255),3)
    else:
        temp = abs(sizeofscr - p1[0]) / fx
    
        fx = (temp * fx)   p1[0]
        fy = (temp * fy)   p1[1]
    
    
        blank_image = cv2.line(blank_image,(int(fx),int(fy)),p1,(0,0,255),3)    

cv2.imshow("test",blank_image)
cv2.waitKey(0)

result:

enter image description here

  • Related