Python cv2.圆圈()未显示
我有下面的代码正在工作,除了提供的图像上的画圈。我在上尝试了setMouseCallback示例,该示例正在运行,但恐怕我在自己的代码中实现了错误Python cv2.圆圈()未显示,python,opencv3.0,Python,Opencv3.0,我有下面的代码正在工作,除了提供的图像上的画圈。我在上尝试了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
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
将numpy导入为np
将urllib.request作为ur导入
进口cv2
params=np.zeros([1,2])#init params
s_img=np.zero((512,512,3),np.uint8)
def点击角(摄像头、用户、pw):
"""
函数将单击的角点(ij)写入摄影机对象。
图像由相机快照的有效url提供,
通常以http://xxx.xxx.x.x/snapshot/view0.jpg
:param摄影机:摄影机对象
:param user:用于授权访问URL(如果已设置)的用户名
:param pw:授权访问URL的密码(如果已设置)
"""
窗口名称='img'
... # 定义url,对问题不重要
全局参数,s_img
... # 处理身份验证,这对问题不重要
... #打开url
img=cv2.imdecode(arr,-1)#将数组解码为图像
s_img=cv2。调整大小(img,(0,0),fx=0.5,fy=0.5)
cv2.startWindowThread()
cv2.imshow(窗口名称,s\U img)
打印('单击角点以确定字段')
cv2.setMouseCallback(窗口名称、鼠标上的参数)#已修复。基本上,我所做的是添加对imshow()的调用,以显示绘制了圆的图像的更新版本。此修正版本无任何故障,窗口名称确保现有窗口用于显示更新的图像:
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()
固定的。基本上,我所做的是添加对imshow()的调用,以显示绘制了圆的图像的更新版本。此修正版本无任何故障,窗口名称确保现有窗口用于显示更新的图像:
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()
也许在imshow工作之前设置callback cv2.setMouseCallback(窗口名、鼠标上的参数)?粘贴的代码中包含许多与问题无关的内容。为了让别人有可能重现你的问题,你应该在发帖前更加努力地找出问题所在。阅读本指南@Organ试过了,没用。@HåkenLid谢谢你的建议。我将对其进行适当编辑。在绘制圆圈后尝试添加一个cv2.waitKey(1)
。在imshow工作之前,可能会设置回调cv2.setMouseCallback(窗口名称、鼠标上的参数)?粘贴的代码包含许多与问题无关的内容。为了让别人有可能重现你的问题,你应该在发帖前更加努力地找出问题所在。阅读本指南@Organ试过了,没用。@HåkenLid谢谢你的建议。我会对它进行适当的编辑。尝试添加一个cv2.waitKey(1)
在您绘制圆圈hi@ALGOholic之后,这似乎对我有效。非常感谢你。特别是,我看到您在“on_mouse()”中的圆圈绘制之后放了一个“cv2.imshow()”,这似乎就是我遇到这个问题的原因。你怎么找到的?无论如何,欢迎来到SO,谢谢你的有用回答!谢谢我只是觉得图像更新和窗口更新之间的耦合完全缺失了。我没想到窗口会智能地跟踪图像的状态——在UI框架中很少出现这种情况。非常感谢你。特别是,我看到您在“on_mouse()”中的圆圈绘制之后放了一个“cv2.imshow()”,这似乎就是我遇到这个问题的原因。你怎么找到的?无论如何,欢迎来到SO,谢谢你的有用回答!谢谢我只是觉得图像更新和窗口更新之间的耦合完全缺失了。我没想到窗口会智能地跟踪图像的状态——在UI框架中很少出现这种情况。