python 一条直线坐标_在opencv Python中拖动鼠标绘制一条直线,得到直线端点的坐标...

这段代码演示了如何在OpenCV中通过鼠标事件绘制一条直线,并记录其端点坐标。当鼠标左键按下并在图像上拖动时,会绘制一条直线,并在释放鼠标时显示直线的终点。最终,程序会输出直线端点的坐标。
摘要由CSDN通过智能技术生成

也可以用其他的活动。在

这里有一个快速肮脏的解决方案:import numpy as np

import cv2

btn_down = False

def get_points(im):

# Set up data to send to mouse handler

data = {}

data['im'] = im.copy()

data['lines'] = []

# Set the callback function for any mouse event

cv2.imshow("Image", im)

cv2.setMouseCallback("Image", mouse_handler, data)

cv2.waitKey(0)

# Convert array to np.array in shape n,2,2

points = np.uint16(data['lines'])

return points, data['im']

def mouse_handler(event, x, y, flags, data):

global btn_down

if event == cv2.EVENT_LBUTTONUP and btn_down:

#if you release the button, finish the line

btn_down = False

data['lines'][0].append((x, y)) #append the seconf point

cv2.circle(data['im'], (x, y), 3, (0, 0, 255),5)

cv2.line(data['im'], data['lines'][0][0], data['lines'][0][1], (0,0,255), 2)

cv2.imshow("Image", data['im'])

elif event == cv2.EVENT_MOUSEMOVE and btn_down:

#thi is just for a ine visualization

image = data['im'].copy()

cv2.line(image, data['lines'][0][0], (x, y), (0,0,0), 1)

cv2.imshow("Image", image)

elif event == cv2.EVENT_LBUTTONDOWN and len(data['lines']) < 2:

btn_down = True

data['lines'].insert(0,[(x, y)]) #prepend the point

cv2.circle(data['im'], (x, y), 3, (0, 0, 255), 5, 16)

cv2.imshow("Image", data['im'])

# Running the code

img = cv2.imread('C://image.jpg', 1)

pts, final_image = get_points(img)

cv2.imshow('Image', final_image)

print pts

cv2.waitKey(0)

告诉我这是否是你想要的。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值