我希望在显示图像时通过右键单击鼠标来收集像素位置(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
中收集和存储像素位置的最佳方法。
答案 0 :(得分:2)
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()
答案 1 :(得分:1)
您可以使用数组或列表在其中存储像素位置,也可以存储像素值。
在这里,我正在使用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 输入相同的名称。应该是一样的。在我的代码中,所有窗口都使用相同的名称“图像” 。
您也可以对多个图像执行相同的操作。您只需要传递一个列表,而不是单个图像。
如果要将像素位置存储在某些文本文件中,则还可以按照以下步骤进行操作:
给出存储像素位置值的变量的名称。我使用 refPt 来存储值。所以,我在这里使用它如下:
import csv
with open("D:/pixelLocation.txt", 'w', newline='') as f:
w = csv.writer(f)
w.writerows(refPt)