使用鼠标获取像素位置 click/events
Get pixel location using mouse click/events
我想在显示图像时通过鼠标右键来收集像素位置(row-i, col-i)。
这是一个关于从互联网上下载的图片的简单示例:
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()
至此,我希望了解在 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()
您可以使用数组或列表来存储像素位置,也可以存储像素值。
在这里,我使用 python 3.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()
注意:您需要记住的一件事是,您必须为 namedWindow 输入相同的名称。应该是一样的。在我的代码中,我对所有 window.
使用相同的名称 "image"
您也可以对多张图片执行相同的操作。您只需要传递一个列表而不是单个图像。
如果你想将像素位置存储在某个文本文件中,那么你也可以这样做:
给出存储像素位置值的变量的名称。我使用 refPt 来存储值。所以,我在这里使用它如下:
import csv
with open("D:/pixelLocation.txt", 'w', newline='') as f:
w = csv.writer(f)
w.writerows(refPt)
我想在显示图像时通过鼠标右键来收集像素位置(row-i, col-i)。
这是一个关于从互联网上下载的图片的简单示例:
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()
至此,我希望了解在 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()
您可以使用数组或列表来存储像素位置,也可以存储像素值。
在这里,我使用 python 3.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()
注意:您需要记住的一件事是,您必须为 namedWindow 输入相同的名称。应该是一样的。在我的代码中,我对所有 window.
使用相同的名称 "image"您也可以对多张图片执行相同的操作。您只需要传递一个列表而不是单个图像。
如果你想将像素位置存储在某个文本文件中,那么你也可以这样做:
给出存储像素位置值的变量的名称。我使用 refPt 来存储值。所以,我在这里使用它如下:
import csv
with open("D:/pixelLocation.txt", 'w', newline='') as f:
w = csv.writer(f)
w.writerows(refPt)