40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
|
|
import cv2
|
||
|
|
import numpy as np
|
||
|
|
|
||
|
|
# 全局变量
|
||
|
|
points = []
|
||
|
|
drawing = False # 是否开始绘图
|
||
|
|
|
||
|
|
|
||
|
|
def select_point(event, x, y, flags, param):
|
||
|
|
global drawing, points
|
||
|
|
|
||
|
|
if event == cv2.EVENT_LBUTTONDOWN:
|
||
|
|
# 当鼠标左键按下时记录第一个点
|
||
|
|
drawing = True
|
||
|
|
points = [(x, y)]
|
||
|
|
elif event == cv2.EVENT_LBUTTONUP:
|
||
|
|
# 当鼠标左键释放时记录第二个点,并完成线段的选择
|
||
|
|
drawing = False
|
||
|
|
points.append((x, y))
|
||
|
|
# 绘制线段
|
||
|
|
cv2.line(img, points[0], points[1], (0, 255, 0), 2)
|
||
|
|
# 计算两点间的距离
|
||
|
|
distance = np.sqrt((points[1][0] - points[0][0]) ** 2 + (points[1][1] - points[0][1]) ** 2)
|
||
|
|
print(f"线段的长度为: {distance:.2f} 像素")
|
||
|
|
# 显示更新后的图像
|
||
|
|
cv2.imshow('image', img)
|
||
|
|
|
||
|
|
|
||
|
|
img_path = '/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/point1/val/192.168.0.234_01_20251014154410130.jpg' # 替换为你的图片路径
|
||
|
|
img = cv2.imread(img_path)
|
||
|
|
cv2.namedWindow('image')
|
||
|
|
cv2.setMouseCallback('image', select_point)
|
||
|
|
|
||
|
|
while(1):
|
||
|
|
cv2.imshow('image', img)
|
||
|
|
k = cv2.waitKey(1) & 0xFF
|
||
|
|
if k == 27: # 按下ESC退出
|
||
|
|
break
|
||
|
|
|
||
|
|
cv2.destroyAllWindows()
|