5

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.

Andrei Bularca
  • 914
  • 2
  • 11
  • 29
  • 1
    As of OpenCV 3.4.8/4.1.2, there is a way to do this, not mentioned in any of the answers on this question. See [OpenCV: how to force the image window to appear on top of other windows?](https://stackoverflow.com/questions/8417531/opencv-how-to-force-the-image-window-to-appear-on-top-of-other-windows) – Dan Mašek Jun 14 '21 at 09:27

7 Answers7

4

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

DanielHsH
  • 4,287
  • 3
  • 30
  • 36
1

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))
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)
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Oct 22 '21 at 08:22
  • thank you for suggestion, but no ... this 4 lines are self understandable for people who knows "OpenCV" word – Alexandr Kil Oct 23 '21 at 11:22
1

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

Guglie
  • 2,121
  • 1
  • 24
  • 43
0

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)
Noah Spurrier
  • 508
  • 5
  • 8
0

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

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
  • this doesn't ensure that the window will remain "always on top". it is an abuse of the GUI, actually. it might even prevent focus from working properly. – Christoph Rackwitz Apr 07 '21 at 19:56