我有以下代码正常工作,除了提供的图片上的绘制圆圈。我在here上尝试了setMouseCallback示例,它正在运行,但我担心我在自己的代码中实现了它。
import numpy as np
import urllib.request as ur
import cv2
params = np.zeros([1, 2]) # init params
s_img = np.zeros((512, 512, 3), np.uint8)
def clicked_corners(camera, user, pw):
"""
Function to write the clicked corners (ij) to a camera object.
Image is provided by a valid url to a snapshot of camera,
usually in the form of http://xxx.xxx.x.x/snapshot/view0.jpg
:param camera: Camera object
:param user: username to authorize access to URL if set
:param pw: password to authorize access to URL if set
"""
window_name = 'img'
... # define url, not important to the question
global params, s_img
... # handle authentication, not important to the question
... #open url
img = cv2.imdecode(arr, -1) # decode array to an image
s_img = cv2.resize(img, (0, 0), fx=0.5, fy=0.5)
cv2.startWindowThread()
cv2.imshow(window_name, s_img)
print('Click corners to determine the field')
cv2.setMouseCallback(window_name, on_mouse, params) # <-- setting callback for mouseclicks
k = cv2.waitKey(0) & 0xFF # adding a keylistener
if k == 27:
... # destroy window
clicked_points = params
return clicked_points
def on_mouse(event, x, y, flag, param):
global params
if event == cv2.EVENT_LBUTTONDOWN:
... do some operation
cv2.circle(s_img, (x, y), 100, (255, 0, 0), -1) # **draw circle this part is not working properly**
... return something
我编辑了代码以突出显示问题的重要部分,以及我认为它不应该如此。
答案 0 :(得分:2)
固定。基本上我所做的是添加一个imshow()调用来显示带有圆圈的图像的更新版本。这个修正版本没有任何故障,window_name确保使用现有窗口显示更新的图像:
<String, FileName>