Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用鼠标单击/事件获取像素位置_Python_Opencv_Position - Fatal编程技术网

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

Python 使用鼠标单击/事件获取像素位置,python,opencv,position,Python,Opencv,Position,我希望在显示图像时通过右键单击鼠标来收集像素位置(第一行,第一列) 这是一个关于从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",

我希望在显示图像时通过右键单击鼠标来收集像素位置(第一行,第一列)

这是一个关于从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()

此时,我希望了解在
列表中收集和存储像素位置的最佳方法

您可以使用数组或列表在其中存储像素位置,也可以存储像素值

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()
在这里,我使用的是Python3.x 您可以按照下面的代码操作。在这段代码中,我将执行两个鼠标单击事件。 一个用于使用鼠标左键单击获取像素位置,第二个用于获取RGB图像中特定位置处的特定像素值

我还在refPt变量中存储像素位置值。 下面是代码

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()
注意:您需要记住的一点是,您必须为名称添加相同的名称。应该是一样的。在我的代码中,我对所有窗口使用相同的名称“image”

您也可以对多个图像执行相同的操作。您只需要传递一个列表,而不是单个图像

如果要将像素位置存储在某个文本文件中,也可以按如下操作:

import csv
with open("D:/pixelLocation.txt", 'w', newline='') as f:
    w = csv.writer(f)
    w.writerows(refPt) 
给出存储像素位置值的变量的名称。我使用refPt存储值。因此,我在这里使用它如下:

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

您可以使用数组或列表存储其中的像素位置,也可以存储像素值

在这里,我使用的是Python3.x 您可以按照下面的代码操作。在这段代码中,我将执行两个鼠标单击事件。 一个用于使用鼠标左键单击获取像素位置,第二个用于获取RGB图像中特定位置处的特定像素值

我还在refPt变量中存储像素位置值。 下面是代码

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()
注意:您需要记住的一点是,您必须为名称添加相同的名称。应该是一样的。在我的代码中,我对所有窗口使用相同的名称“image”

您也可以对多个图像执行相同的操作。您只需要传递一个列表,而不是单个图像

如果要将像素位置存储在某个文本文件中,也可以按如下操作:

import csv
with open("D:/pixelLocation.txt", 'w', newline='') as f:
    w = csv.writer(f)
    w.writerows(refPt) 
给出存储像素位置值的变量的名称。我使用refPt存储值。因此,我在这里使用它如下:

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

您没有使用宽度和高度变量,如果没有,应该删除needed@prashant谢谢。你没有使用宽度和高度变量,如果没有,应该删除needed@prashant谢谢