单击图像1次,获取位置并销毁OpenCv窗口 [英] Click image 1 time, get position and destroy window OpenCv
本文介绍了单击图像1次,获取位置并销毁OpenCv窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种简单的方法可以使用OpenCv打开图像,并保持其打开状态直到被单击,然后返回像素坐标并销毁图像,就像使用WaitKey()一样,只是带有返回键,然后单击作为触发器?
Is there a simple way to open an image using OpenCv, and keep it open until it is clicked, then return the pixel coordinate and destroy the image, almost like using WaitKey() just with a return, and click as trigger?
推荐答案
这应该做您想要的:
#!/usr/bin/env python3
import cv2
import numpy as np
def onClick(event,x,y,flags,param):
"""Called whenever user left clicks"""
global Running
if event == cv2.EVENT_LBUTTONDOWN:
print(f'I saw you click at {x},{y}')
Running = False
# Create window
wname = "Funky Image"
cv2.namedWindow(winname=wname)
cv2.setMouseCallback(wname, onClick)
# Load an image
img = cv2.imread('image.jpg')
Running = True
while Running:
cv2.imshow(wname,img)
cv2.waitKey(1)
cv2.destroyAllWindows
这篇关于单击图像1次,获取位置并销毁OpenCv窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文