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.
Asked
Active
Viewed 1.2k times
4
-
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
5 Answers
5
You can use: cvGetWindowHandle() to obtain Widows handler. Then using regular windows API you can do anything you like
DanielHsH
- 4,066
- 2
- 26
- 35
2
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))
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
- 478
- 5
- 8
-
You need to put a `cv2.waitKey(1)` between the FULLSCREEN and NORMAL lines, at least on windows 10 – skjerns Dec 27 '19 at 17:03
-
this also doesn't keep the window on top, just focuses it once. So not working as asked. – skjerns Dec 27 '19 at 17:06
-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:
- destroy window, this force to create new window
- declare new window
- move window at position that you want, for example last position
- show window
indio99
- 1
-
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
-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)
Alexandr Kil
- 1
- 1
-
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