如何在opencv-python中绘制短cv2.line?

bihw5rsg  于 2022-11-24  发布在  Python
关注(0)|答案(1)|浏览(185)

如果要画直线的两个点是(100,100)和(200,200),有没有办法只画中间的点?

oprakyz7

oprakyz71#

如果我没理解错你的问题,你只想要中间点,所以这个片段应该解决你的问题

import cv2

def midpoint(point1, point2):
    # values need to be rounded to an integer to avoid an error when calling cv2.circle() later
    midpoint_x = round((point2[0] + point1[0])/2)
    midpoint_y = round((point2[1] + point1[1])/2)
    midpoint = (midpoint_x, midpoint_y)
    return midpoint

img = cv2.imread('image.jpg')
    
point1 = (352, 92)
point2 = (-2.5121140e+06, 4.8845758e+02)
    
point3 = midpoint(point1, point2)

# starting with radius 10 to make the point initially more visible
img = cv2.circle(img, point3, radius=10, color=(255, 255, 255), thickness=-1)
cv2.imshow("Midpoint position", img)
cv2.waitKey()
cv2.destroyWindow("Midpoint position")

相关问题