OpenCV 窗口始终位于顶部

发布于 2024-11-06 13:02:27 字数 57 浏览 0 评论 0原文

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

Is there a way to set a OpenCV window to be always on top?
And can i remove the minimize and close button from my window?
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

野生奥特曼 2024-11-13 13:02:27

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

You can use:
cvGetWindowHandle()
to obtain Widows handler. Then using regular windows API you can do anything you like

凉墨 2024-11-13 13:02:27

您可以使用 setWindowProperty
使用属性 WND_PROP_TOPMOST 在顶部设置 OpenCV 窗口。
这适用于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

You can use setWindowProperty
to set an OpenCV window on top with the property WND_PROP_TOPMOST.
This works with OpenCV 3.4.8+ and 4.1.2+.

E.g. in 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)

The above python code should create a foreground window:
OpenCV always on top window - python

明天过后 2024-11-13 13:02:27

我在此处的评论中找到了最佳解决方案: Python OpenCV在其他应用程序之上打开窗口

只需在打开窗口后添加以下命令,例如

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))

I found the best solution posted in comments here: Python OpenCV open window on top of other applications

Simply add the command below after opening a window, e.g.

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

Use "python" in lower case. Using "Python", as I found in some answers, gave me an error:

21:62: execution error: Finder got an error: Can’t set process "Python" to true. (-10006))
泛泛之交 2024-11-13 13:02:27

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

    #!/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)

I found that all I needed to do was set my main window to fullscreen then back to normal.

    #!/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)
热鲨 2024-11-13 13:02:27
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)
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)
甜味超标? 2024-11-13 13:02:27

我发现,对于任何回顾过去的人来说,解决这个问题的最简单方法就是这两行。这就像 Gugile 的另一个答案一样,但不需要先用单独的行创建命名窗口。

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

The simplest way I found to solve this for anyone looking back was just these two lines. It's like the other answer by Gugile but there's no need to have a seperate line creating the named window first.

cv2.imshow('window_name', img)
cv2.setWindowProperty('window_name', cv2.WND_PROP_TOPMOST, 1)
誰認得朕 2024-11-13 13:02:27

对我来说,这在 macOS 中工作得很好,我认为它在另一个操作系统中也能很好地

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

重复循环,顺序是:

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

for me this work fine in macOS, I think that it's will work well in another os

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

this repeat in loop, the sequence are:

  1. destroy window, this force to create new window
  2. declare new window
  3. move window at position that you want, for example last
    position
  4. show window
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文