【问题标题】:OpenCV window always on topOpenCV 窗口始终位于顶部
【发布时间】:2011-08-23 02:09:05
【问题描述】:

有没有办法将 OpenCV 窗口设置为始终位于顶部? 我可以从我的窗口中删除最小化和关闭按钮吗? 谢谢。

【问题讨论】:

标签: opencv window


【解决方案1】:
cv2.namedWindow('CCPDA')
cv2.resizeWindow('CCPDA', 200, 200)
hWnd = win32gui.FindWindow(None, 'CCPDA')
win32gui.SetWindowPos(hWnd, win32con.HWND_TOPMOST, 0, 0, 0, 0,
                      win32con.SWP_SHOWWINDOW | win32con.SWP_NOSIZE | win32con.SWP_NOMOVE)

【讨论】:

  • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
  • 谢谢你的建议,但是不......这4行对于知道“OpenCV”这个词的人来说是可以理解的
【解决方案2】:

对我来说,这在 macOS 中可以正常工作,我认为它在其他操作系统中也可以正常工作

destroyWindow( "windowName" );
namedWindow( "windowName", WINDOW_NORMAL );
moveWindow( "windowName", posx, posy );
imshow( "windowName", frame );

这个循环重复,顺序是:

  1. 销毁窗口,强制创建新窗口
  2. 声明新窗口
  3. 将窗口移动到您想要的位置,例如最后一个 位置
  4. 显示窗口

【讨论】:

  • 这并不能确保窗口将保持“始终在顶部”。实际上,这是对 GUI 的滥用。它甚至可能会阻止焦点正常工作。
【解决方案3】:

我发现我需要做的就是将主窗口设置为全屏,然后恢复正常。

    #!/usr/bin/env python
    import cv2
    import numpy

    WindowName="Main View"
    view_window = cv2.namedWindow(WindowName,cv2.WINDOW_NORMAL)

    # These two lines will force your "Main View" window to be on top with focus.
    cv2.setWindowProperty(WindowName,cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
    cv2.setWindowProperty(WindowName,cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_NORMAL)

    # The rest of this does not matter. This would be the rest of your program.
    # This just shows an image so that you can see that this example works.
    img = numpy.zeros((400,400,3), numpy.uint8)
    for x in range(0,401,100):
        for y in range(0,401,100):
            cv2.line(img,(x,0),(0,y),(128,128,254),1)
            cv2.line(img,(x,399),(0,y),(254,128,128),1)
            cv2.line(img,(399,y),(x,399),(128,254,128),1)
            cv2.line(img,(399,y),(x,0),(254,254,254),1)
    cv2.imshow(WindowName, img)
    cv2.waitKey(0)
    cv2.destroyWindow(WindowName)

【讨论】:

  • 您需要在 FULLSCREEN 和 NORMAL 行之间放置一个cv2.waitKey(1),至少在 Windows 10 上
  • 这也不会让窗口保持在顶部,只聚焦一次。所以没有按要求工作。
【解决方案4】:

我在这里找到了 cmets 中发布的最佳解决方案:Python OpenCV open window on top of other applications

打开一个窗口后只需添加下面的命令,例如

cv2.namedWindow('img_file_name', cv2.WINDOW_NORMAL) # Creates a window
os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "python" to true' ''') # To make window active

使用小写的“python”。正如我在一些答案中发现的那样,使用“Python”给了我一个错误:

21:62: execution error: Finder got an error: Can’t set process "Python" to true. (-10006))

【讨论】:

  • 仅适用于 Mac
【解决方案5】:

您可以使用: cvGetWindowHandle() 获取寡妇处理程序。然后使用常规的 windows API 你可以做任何你喜欢的事情

【讨论】:

  • 投了反对票,因为它看起来更像是一个提示而不是一个答案。
猜你喜欢
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多