简体   繁体   English

使用鼠标点击/事件获取像素位置

[英]Get pixel location using mouse click/events

I wish to collect the pixel location (row-i, col-i) by right-clicking the mouse when the image is displayed.我希望在显示图像时通过右键单击鼠标来收集像素位置(row-i,col-i)。

This is a simple example about a picture downloaded from the internet:这是一个关于从互联网下载的图片的简单示例:

import urllib
import cv2
from win32api import GetSystemMetrics

path_image = urllib.urlretrieve("http://www.bellazon.com/main/uploads/monthly_06_2013/post-37737-0-06086500-1371727837.jpg", "local-filename.jpg")[0]
img = cv2.imread(path_image,0)
width = GetSystemMetrics(0)
height = GetSystemMetrics(1)
scale_width = width / img.shape[1]
scale_height = height / img.shape[0]
scale = min(scale_width, scale_height)
window_width = int(img.shape[1] * scale)
window_height = int(img.shape[0] * scale)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.resizeWindow('image', window_width, window_height)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

At this point, I wish to understand the best way to collect and store the pixel locations in a list .在这一点上,我希望了解在list收集和存储像素位置的最佳方法。

import urllib
import cv2
from win32api import GetSystemMetrics

#the [x, y] for each right-click event will be stored here
right_clicks = list()

#this function will be called whenever the mouse is right-clicked
def mouse_callback(event, x, y, flags, params):

    #right-click event value is 2
    if event == 2:
        global right_clicks

        #store the coordinates of the right-click event
        right_clicks.append([x, y])

        #this just verifies that the mouse data is being collected
        #you probably want to remove this later
        print right_clicks

path_image = urllib.urlretrieve("http://www.bellazon.com/main/uploads/monthly_06_2013/post-37737-0-06086500-1371727837.jpg", "local-filename.jpg")[0]
img = cv2.imread(path_image,0)
scale_width = 640 / img.shape[1]
scale_height = 480 / img.shape[0]
scale = min(scale_width, scale_height)
window_width = int(img.shape[1] * scale)
window_height = int(img.shape[0] * scale)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.resizeWindow('image', window_width, window_height)

#set mouse callback function for window
cv2.setMouseCallback('image', mouse_callback)

cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

you can use an array or list to store the pixels location in it and also you can store the pixel value as well.您可以使用数组或列表来存储像素位置,也可以存储像素值。

Here, I'm using python 3.x You can follow the below code.在这里,我使用的是 python 3.x 你可以按照下面的代码。 In that code I'm performing the two mouse click events.在该代码中,我正在执行两个鼠标单击事件。 One is for getting the pixel location using left mouse click, and second is for getting the specific pixel value at particular location in RGB image.一是使用鼠标左键获取像素位置,二是获取RGB图像中特定位置的特定像素值。

I'm also storing the pixel location value in refPt variable.我还将像素位置值存储在refPt变量中。 See below is the code.下面是代码。

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()

Note: One thing you need to remember that you have to put same name for the namedWindow .注意:您需要记住的一件事是您必须为namedWindow设置相同的名称。 It should be same.应该是一样的。 In my code, I'm using the same name "image" for all the window.在我的代码中,我对所有窗口使用相同的名称“图像”

You can do the same thing for multiple images as well.您也可以对多个图像执行相同的操作。 You just need to pass an list instead of single image.您只需要传递一个列表而不是单个图像。

If you want to store the pixel location in some text file, then you can also do it as follow:如果你想将像素位置存储在某个文本文件中,那么你也可以这样做:

Give the name of the variable where you are storing the pixel location value.给出存储像素位置值的变量的名称。 I used refPt for storing the value.我使用refPt来存储值。 so, I used it here as follow:所以,我在这里使用它如下:

import csv
with open("D:/pixelLocation.txt", 'w', newline='') as f:
    w = csv.writer(f)
    w.writerows(refPt) 

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

相关问题 通过在TKinter中单击鼠标获取像素位置 - Get pixel location from mouse click in TKinter 在 Kivy 中点击获取像素位置 - Get location of pixel upon click in Kivy 当鼠标点击图像时,Pyqt获取像素位置和值 - Pyqt get pixel position and value when mouse click on the image 使用Tkinter在Matplotlib中获取鼠标单击的位置 - Getting the location of a mouse click in Matplotlib using Tkinter 程序使用python Tkinter识别鼠标单击时的像素颜色 - Program to identify pixel color on mouse click using python Tkinter 如何使用 PyQt5 获取像素位置并在该位置绘制一个点? - How to get pixel location and draw a dot on that location using PyQt5? 将鼠标移至特定颜色像素? - Get mouse to specific color pixel? 使用 pynput 在随机位置自动单击鼠标(对于 web 浏览器游戏) - Automatically Click Mouse at Random location using pynput (for a web browser game) 使用 Opencv 和 python 实时流上的鼠标单击事件 - Mouse click events on a live stream with Opencv and python 如何使用python侦听Windows中当前正在运行的应用程序的任务栏图标上的鼠标单击事件? - How to listen for mouse click events on task bar icon for a currently running application in windows using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM