无法在Python中使用namedWindow绘制图像

时间:2020-05-16 16:42:25

标签: python opencv

在终端或Spyder中运行我的代码时,将打开一个图像。但是,单击鼠标左键或鼠标右键均不执行任何操作。这是我编写的代码:

import cv2
import matplotlib.pyplot as plt

def drawCircle(event,x,y,flags,param):

    if event == cv2.EVENT_LBUTTONDOWN: #(If Left button of mouse clicked)
        cv2.circle(img,(x,y),radius = 100, color =(255,0,0),thickness=-1)
    if event == cv2.EVENT_RBUTTONDOWN: #(If Right button of mouse clicked)
        cv2.circle(img,(x,y),radius = 100, color =(0,255,0),thickness=-1)

img= np.zeros(shape=(512,512,3),dtype=np.int8)
cv2.namedWindow("someimage")

cv2.setMouseCallback("someimage", drawCircle)

while True:
    cv2.imshow("someimage",img)
    if cv2.waitKey(2):
        break

cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:1)

您缺少导入内容:

declare cur cursor fast_forward for select distinct GroupId from @employee
declare @groupid int, @employeeid int

open cur
fetch next from cur into @groupid

while (@@FETCH_STATUS = 0)
begin
    select @employeeid = EmployeeId from @employee where GroupId = @groupid and IsDuplicate = 0
    select e.EmployeeId, IsDuplicate, c.*
    into #temp
    from @employee e
    join @employeecontacts ec on ec.EmployeeId = e.EmployeeId
    join @contacts c on c.ContactId = ec.ContactId
    where GroupId = @groupid

    ;with cte as
    (
        select @employeeid employeeid, t.ContactId, t.ContactEmail, ROW_NUMBER() over (partition by t.ContactEmail order by  t.ContactId) rn--, c.ContactEmail as ce2
        from #temp t 
        left join (select EmployeeId, x.ContactId, ContactEmail from @employeecontacts x join @contacts c on c.ContactId = x.ContactId)ec 
        on ec.EmployeeId = @employeeid and ec.ContactEmail = t.ContactEmail
        where ec.ContactId is null
    )
    insert into @employeecontacts
    select employeeid, ContactId from cte where rn = 1

    drop table if exists #temp
    fetch next from cur into @groupid
end

close cur
deallocate cur

此外,如果在指定的延迟时间内未按任何键(在您的情况下为2ms),则waitkey返回-1。因此,import numpy as np 调用的结果为真实值,并且执行了cv2.waitKey(2)。更改为

break

仅当您按下键if cv2.waitKey(2) == ord("q"): break

时,这将停止循环

我还注意到右键单击将首先打开一个上下文菜单。这是解决方案的链接:
Why does a right click open a drop down menu in my OpenCV imshow() window?