简体   繁体   English

用opencv在图像上实现鼠标输入

[英]Implement mouse input with opencv on an image

I found some code online and I changed it a little.我在网上找到了一些代码,并对其进行了一些更改。 I am doing a navigation project and want user to input a start and end point by clicking on the map image.我正在做一个导航项目,并希望用户通过单击地图图像来输入起点和终点。 Here is what I have:这是我所拥有的:

import cv2 as cv2

def draw_circle(event,x,y,flags,param):
    global mouseX,mouseY
    if event == cv2.EVENT_LBUTTONDBLCLK:
        cv2.circle(img,(x,y),2,(0,0,0),-1)
        mouseX,mouseY = x,y

img = cv2.imread("1.png")
img = cv2.resize(g1,(800,600))
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
cv2.imshow('image',img)

Normally we need a cv2.waitKey() for this image to be shown, but in this case, I really don't know what to do next.通常我们需要一个cv2.waitKey()来显示这个图像,但在这种情况下,我真的不知道下一步该怎么做。

I want to show this map view, and when user double-click on it, it will show a black dot(this is implemented).我想显示这个地图视图,当用户双击它时,它会显示一个黑点(这是实现的)。 After user input two points, the image will close and the function will return two sets of pixel wise coordinate indicate where did the user click.用户输入两个点后,图像将关闭,函数将返回两组像素坐标,指示用户点击的位置。

I saw people putting cv2.imshow() and cv2.waitKey() in a while loop, this will show the image and the click function is working fine as well.我看到人们将cv2.imshow()cv2.waitKey()放在一个 while 循环中,这将显示图像并且点击功能也工作正常。 But I don't know how to return the pixel coordinate in that way.但我不知道如何以这种方式返回像素坐标。

import cv2 as cv2

def draw_circle(event,x,y,flags,param):
    global mouseX,mouseY
    if event == cv2.EVENT_LBUTTONDBLCLK:
        cv2.circle(img,(x,y),2,(0,0,0),-1)
        mouseX,mouseY = x,y
        cv2.destroyWindow('image')
        print('last position: ', mouseX, mouseY)

img = cv2.imread("1.jpg")
img = cv2.resize(img, (800,600))
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
cv2.imshow('image',img)
cv2.waitKey()

If you run, this code, it will register the last position the mouse, destory the window and print the result the console.如果您运行此代码,它将注册鼠标的最后一个位置,销毁窗口并将结果打印到控制台。 The trick you, should use the x and y provided by the callback funtion.你的技巧,应该使用回调函数提供的xy Hope this helps.希望这可以帮助。

import cv2 import numpy as np def mouse_callback(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: global mouseX, mouseY, last_mouseX, last_mouseY last_mouseX, last_mouseY = mouseX, mouseY mouseX, mouseY = x, y print("x =", mouseX, "\ty =", mouseY) print("last_x =", last_mouseX, "last_y =", last_mouseY) global disp_img disp_img = np.copy(img) cv2.circle(disp_img, (mouseX,mouseY), 2, (0,0,255), -1) cv2.circle(disp_img, (last_mouseX,last_mouseY), 2, (255,0,0), -1) mouseX, mouseY, last_mouseX, last_mouseY = 0, 0, 0, 0 img = cv2.imread('ip.jpg') disp_img = np.copy(img) winname = 'image' cv2.namedWindow(winname) cv2.setMouseCallback(winname, mouse_callback) # we need to loop imshow in order to draw the circle ch = 0 while ch != 27: # press esc to exit cv2.imshow('image', disp_img) ch = cv2.waitKey(1) cv2.destroyAllWindows()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM