gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:28:35 25 4
gpt4 key购买 nike

我希望在显示图像时通过右键单击鼠标来收集像素位置(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()

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

最佳答案

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 - 使用鼠标点击/事件获取像素位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32770654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com