Click image 1 time, get position and destroy window OpenCv

穿精又带淫゛_ 提交于 2021-01-28 09:50:57

问题


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?


回答1:


This should do what you want:

#!/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


来源:https://stackoverflow.com/questions/61275955/click-image-1-time-get-position-and-destroy-window-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!