OpenCV窗口始终置顶

5
有没有办法让OpenCV窗口始终保持在最前面? 并且我能否从我的窗口中去掉最小化和关闭按钮? 谢谢。

1
从OpenCV 3.4.8/4.1.2开始,有一种方法可以实现这一点,但在此问题的任何答案中都没有提到。请参见OpenCV:如何强制图像窗口显示在其他窗口之上? - Dan Mašek
7个回答

4
您可以使用cvGetWindowHandle()函数获取窗口句柄。然后使用常规的Windows API,您可以随心所欲地进行任何操作。

7
因为它更像是一个提示而不是一个答案,所以被踩了。 - eric

1
您可以使用setWindowProperty将OpenCV窗口置于顶部,并设置属性WND_PROP_TOPMOST
这适用于OpenCV 3.4.8+4.1.2+版本。

例如,在Python中:

import cv2, numpy as np
window_name = "Top Window"
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.setWindowProperty(window_name, cv2.WND_PROP_TOPMOST, 1)
cv2.imshow(window_name, np.full((480,640,3), (234,183,39), dtype=np.int8))
cv2.waitKey(0)

上述 Python 代码应该创建一个前景窗口: OpenCV 始终置顶窗口 - python

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)

请不要仅仅发布代码作为答案,还要提供解释您的代码是如何解决问题的。带有解释的答案通常更有帮助和更高质量,并且更有可能吸引赞同。 - Mark Rotteveel
谢谢您的建议,但不需要... 对于了解“OpenCV”一词的人来说,这4行很容易理解。 - Alexandr Kil

1
我在评论中找到了最佳解决方案: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))

3
仅适用于Mac操作系统。 - QZHua

0

我发现我所需要做的就是将我的主窗口设置为全屏,然后再改回正常模式。

    #!/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上这样做。 - skjerns
1
这也不能使窗口保持在最上层,只是将其聚焦。因此无法按要求工作。 - skjerns

0

我发现的最简单的方法,对于任何回顾的人来说,只需要这两行代码。就像Gugile给出的其他答案一样,没有必要先创建一个单独的命名窗口。

cv2.imshow('window_name', img)
cv2.setWindowProperty('window_name', cv2.WND_PROP_TOPMOST, 1)

-1

对我来说,在 macOS 上这个工作很好,我认为它在其他操作系统上也会很好地运行。

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

这是一个循环,其序列如下:

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

这并不保证窗口会一直处于“最顶层”。实际上,这是对GUI的滥用。它甚至可能会阻止焦点正常工作。 - Christoph Rackwitz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接