Opencv:检测鼠标在图片上的点击位置

28

我有一段代码,其中我使用OpenCV简单地显示一张图片:

    import numpy as np 
    import cv2

    class LoadImage:
        def loadImage(self):
            self.img=cv2.imread('photo.png')
            cv2.imshow('Test',self.img)

            self.pressedkey=cv2.waitKey(0)

            # Wait for ESC key to exit
            if self.pressedkey==27:
                cv2.destroyAllWindows()

    # Start of the main program here        
    if __name__=="__main__":
        LI=LoadImage()
        LI.loadImage()

当照片显示在窗口中时,我想在控制台(终端)上显示鼠标在图片上单击时的位置。我不知道怎样实现这个功能,请帮忙一下吗?


@berak 这不是同一个问题,我是Python和OpenCV的初学者。 - user4519127
1
只需再次前往那里,打印出所有值并查看即可。 - berak
8个回答

43

这是一个示例鼠标回调函数,它捕获左键双击事件。

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

然后您需要将该函数绑定到一个窗口,以捕获鼠标点击事件。

img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)

然后,在一个无限处理循环中(或者您想要的任何其他方式)

while(1):
    cv2.imshow('image',img)
    k = cv2.waitKey(20) & 0xFF
    if k == 27:
        break
    elif k == ord('a'):
        print mouseX,mouseY

这段代码是用来做什么的?

它会在你双击黑色窗口并按下a键时,将鼠标位置存储在全局变量mouseXmouseY中。

elif k == ord('a'):
    print mouseX,mouseY

每次按下a键时,将打印出当前存储的鼠标点击位置。


代码“借用”自这里。


@MasoudPourbozorg 按下 Escape 键。 - GPPK
同时,@MasoudPourbozorg,我已经提供了代码来源的链接,因为它提供了更多关于我的答案的详细信息,包括对原始代码作者的归属以及任何潜在的许可问题。 - GPPK
1
@GPPK 感谢您提供链接,但我无法打开它。因此,我建议提供更新的链接或将其完全删除。请检查源链接。 - mpour
1
在第一个代码块中,在global mouseX,mouseY这一行,你可能也想添加img。否则,你将无法将它传递到 cv2.circle(img, (x,y), 100, (255,0,0), -1) - Alexey Antonenko
1
这真的很有用。建议编辑:如果您在图像中双击并按字母 a,系统将打印鼠标坐标。 - mherzog
@GPPK 是的,我现在看到了。也许可以使用高亮或引用来强调它是“a”按钮,而不是打字错误。 - mherzog

12
以下是我的实现:
不需要存储点击位置,仅显示它:
def onMouse(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
       # draw circle here (etc...)
       print('x = %d, y = %d'%(x, y))
cv2.setMouseCallback('WindowName', onMouse)

如果您想在代码的其他位置使用这些位置,可以使用以下方法获取坐标:

posList = []
def onMouse(event, x, y, flags, param):
   global posList
   if event == cv2.EVENT_LBUTTONDOWN:
        posList.append((x, y))
cv2.setMouseCallback('WindowName', onMouse)
posNp = np.array(posList)     # convert to NumPy for later use

5
import cv2

cv2.imshow("image", img)
cv2.namedWindow('image')
cv2.setMouseCallback('image', on_click)

def on_click(event, x, y, p1, p2):
    if event == cv2.EVENT_LBUTTONDOWN:
        cv2.circle(lastImage, (x, y), 3, (255, 0, 0), -1)

1
欢迎来到SO。请问您能否解释一下为什么这是答案,除了分享的代码之外? - Mike Poole

1
这是基于类的OpenCV鼠标回调函数的实现,用于获取图像上的点。
import cv2
import numpy as np
#events = [i for i in dir(cv2) if 'EVENT' in i]
#print (events)

class MousePts:
    def __init__(self,windowname,img):
        self.windowname = windowname
        self.img1 = img.copy()
        self.img = self.img1.copy()
        cv2.namedWindow(windowname,cv2.WINDOW_NORMAL)
        cv2.imshow(windowname,img)
        self.curr_pt = []
        self.point   = []

    def select_point(self,event,x,y,flags,param):
        if event == cv2.EVENT_LBUTTONDOWN:
            self.point.append([x,y])
            #print(self.point)
            cv2.circle(self.img,(x,y),5,(0,255,0),-1)
        elif event == cv2.EVENT_MOUSEMOVE:
            self.curr_pt = [x,y]
            #print(self.point)

    def getpt(self,count=1,img=None):
        if img is not None:
            self.img = img
        else:
            self.img = self.img1.copy()
        cv2.namedWindow(self.windowname,cv2.WINDOW_NORMAL)
        cv2.imshow(self.windowname,self.img)
        cv2.setMouseCallback(self.windowname,self.select_point)
        self.point = []
        while(1):
            cv2.imshow(self.windowname,self.img)
            k = cv2.waitKey(20) & 0xFF
            if k == 27 or len(self.point)>=count:
                break
            #print(self.point)
        cv2.setMouseCallback(self.windowname, lambda *args : None)
        #cv2.destroyAllWindows()
        return self.point, self.img

if __name__=='__main__':
    img = np.zeros((512,512,3), np.uint8)
    windowname = 'image'
    coordinateStore = MousePts(windowname,img)

    pts,img = coordinateStore.getpt(3)
    print(pts)

    pts,img = coordinateStore.getpt(3,img)
    print(pts)

    cv2.imshow(windowname,img)
    cv2.waitKey(0)

1
您可以通过执行各种鼠标单击事件来检测点击图片时的鼠标位置。
在执行鼠标单击事件时,您需要记住一件事情,即无论您在何处使用cv2.imshowcv2.namedWindow,都应该在所有地方使用相同的窗口名称。
我在以下stackoverflow帖子中提供了使用Python 3.x和OpenCV的工作代码: https://dev59.com/J4_ea4cB1Zd3GeqPOnYe#60445099 您可以参考上面的链接以获得更好的解释。 代码:
import cv2
import numpy as np

#This will display all the available mouse click events  
events = [i for i in dir(cv2) if 'EVENT' in i]
print(events)

#This variable we use to store the pixel location
refPt = []

#click event function
def click_event(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        print(x,",",y)
        refPt.append([x,y])
        font = cv2.FONT_HERSHEY_SIMPLEX
        strXY = str(x)+", "+str(y)
        cv2.putText(img, strXY, (x,y), font, 0.5, (255,255,0), 2)
        cv2.imshow("image", img)

    if event == cv2.EVENT_RBUTTONDOWN:
        blue = img[y, x, 0]
        green = img[y, x, 1]
        red = img[y, x, 2]
        font = cv2.FONT_HERSHEY_SIMPLEX
        strBGR = str(blue)+", "+str(green)+","+str(red)
        cv2.putText(img, strBGR, (x,y), font, 0.5, (0,255,255), 2)
        cv2.imshow("image", img)


#Here, you need to change the image name and it's path according to your directory
img = cv2.imread("D:/pictures/abc.jpg")
cv2.imshow("image", img)

#calling the mouse click event
cv2.setMouseCallback("image", click_event)

cv2.waitKey(0)
cv2.destroyAllWindows()

0

0

如果有人想要一个基于多进程的GUI来绘制点并拖动它们,这里是一个 单文件脚本


0

如果你想在Python 3中通过悬停在图像上来获取坐标,可以尝试以下方法:

import numpy as np
import cv2 as cv
import os
import sys

# Reduce the size of image by this number to show completely in screen
descalingFactor = 2

# mouse callback function, which will print the coordinates in console
def print_coord(event,x,y,flags,param):
    if event == cv.EVENT_MOUSEMOVE:
        print(f'{x*descalingFactor, y*descalingFactor}\r', end="")

img = cv.imread(cv.samples.findFile('TestImage.png'))
imgheight, imgwidth = img.shape[:2]
resizedImg = cv.resize(img,(int(imgwidth/descalingFactor), int(imgheight/descalingFactor)), interpolation = cv.INTER_AREA)
cv.namedWindow('Get Coordinates')
cv.setMouseCallback('Get Coordinates',print_coord)
cv.imshow('Get Coordinates',resizedImg)
cv.waitKey(0)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接