获取给定图像位置的 numpy 索引
get the numpy index for a given image location
给定一张图片,是否有任何方法可以让用户通过指向特定图片位置以交互方式知道其位置。比如我用angle指向位置时,用来表示这张图片的numpy数组对应的index是多少
您可以设置一个 MouseCallback
function to any window generated with OpenCV. Following is a short example code derived from this 博客 post。在一些window中显示图像,单击鼠标左键时,打印x, y
坐标。 (这就是我理解你的问题的方式。)
import cv2
# Actual mouse callback function
def print_coords(event, x, y, flags, param):
# If left mouse button is clicked, print image coordinates
if (event == cv2.EVENT_LBUTTONDOWN):
print([x, y])
# Read an image
image = cv2.imread('path/to/your/image.png', cv2.IMREAD_COLOR)
# Set up window and mouse callback function
cv2.namedWindow("image")
cv2.setMouseCallback("image", print_coords)
# Loop until the 'c' key is pressed
while True:
# Display image; wait for keypress
cv2.imshow("image", image)
key = cv2.waitKey(1) & 0xFF
# If 'c' key is pressed, break from loop
if key == ord("c"):
break
cv2.destroyAllWindows()
在 window 中点击一些鼠标然后给出这样的输出:
[158, 239]
[63, 6]
[9, 18]
[59, 101]
[128, 279]
[298, 249]
现在,当然,您可以在鼠标回调函数中做任何您想做的事情。注意,如果你最终需要全局变量。
希望对您有所帮助!
给定一张图片,是否有任何方法可以让用户通过指向特定图片位置以交互方式知道其位置。比如我用angle指向位置时,用来表示这张图片的numpy数组对应的index是多少
您可以设置一个 MouseCallback
function to any window generated with OpenCV. Following is a short example code derived from this 博客 post。在一些window中显示图像,单击鼠标左键时,打印x, y
坐标。 (这就是我理解你的问题的方式。)
import cv2
# Actual mouse callback function
def print_coords(event, x, y, flags, param):
# If left mouse button is clicked, print image coordinates
if (event == cv2.EVENT_LBUTTONDOWN):
print([x, y])
# Read an image
image = cv2.imread('path/to/your/image.png', cv2.IMREAD_COLOR)
# Set up window and mouse callback function
cv2.namedWindow("image")
cv2.setMouseCallback("image", print_coords)
# Loop until the 'c' key is pressed
while True:
# Display image; wait for keypress
cv2.imshow("image", image)
key = cv2.waitKey(1) & 0xFF
# If 'c' key is pressed, break from loop
if key == ord("c"):
break
cv2.destroyAllWindows()
在 window 中点击一些鼠标然后给出这样的输出:
[158, 239]
[63, 6]
[9, 18]
[59, 101]
[128, 279]
[298, 249]
现在,当然,您可以在鼠标回调函数中做任何您想做的事情。注意,如果你最终需要全局变量。
希望对您有所帮助!