cv2.circle() 未显示

cv2.circle() not showing

我有以下有效的代码,除了提供的图像上的绘制圆圈。我在 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

我已经编辑了代码以突出问题的重要部分,以及我认为它没有按预期进行的地方。

已修复。本质上,我所做的是添加对 imshow() 的调用,以显示带有圆圈的图像的更新版本。这个更正后的版本没有任何故障,window_name 确保现有的 window 用于显示更新后的图像:

import numpy as np
import cv2

params = np.zeros([1, 2])  # init params
s_img = np.zeros((512, 512, 3), np.uint8)
window_name = 'img'

def main():
    global s_img
    img = np.ones([512, 512, 3]) * 255
    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, None)

    while True:
        k = cv2.waitKey(0) & 0xFF

        if k == 27:
            break # destroy window


def on_mouse(event, x, y, flag, param):
    global s_img
    if event == cv2.EVENT_LBUTTONDOWN:
        print ('here')
        cv2.circle(s_img, (x, y), 100, (255, 0, 0), -1)
        cv2.imshow(window_name, s_img)


if __name__ == '__main__':
    main()