在 OpenCV 中画一条线并 Python 超出给定点
Draw a Line in OpenCV and Python beyond given points
所以我的目标是在 opencv 中画一条在第二个给定点之后连续的线。
我画了一张图应该能更好地解释它。绿色绘图是我想要的线。它从第 1 点开始,到第 2 点不结束。
非常感谢您的帮助
在两点之间画一条线就像画一个三角形。
我通过使用 x 轴和 y 轴上较大的距离得到一个比例,并通过将我的三角形放大到屏幕尺寸,我达到了你想要的线。
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)
结果:
所以我的目标是在 opencv 中画一条在第二个给定点之后连续的线。
我画了一张图应该能更好地解释它。绿色绘图是我想要的线。它从第 1 点开始,到第 2 点不结束。
非常感谢您的帮助
在两点之间画一条线就像画一个三角形。 我通过使用 x 轴和 y 轴上较大的距离得到一个比例,并通过将我的三角形放大到屏幕尺寸,我达到了你想要的线。
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)
结果: