有没有办法让OpenCV窗口始终保持在最前面?
并且我能否从我的窗口中去掉最小化和关闭按钮?
谢谢。
WND_PROP_TOPMOST
。例如,在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)
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('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
21:62: execution error: Finder got an error: Can’t set process "Python" to true. (-10006))
我发现我所需要做的就是将我的主窗口设置为全屏,然后再改回正常模式。
#!/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)
cv2.waitKey(1)
,至少在Windows 10上这样做。 - skjerns我发现的最简单的方法,对于任何回顾的人来说,只需要这两行代码。就像Gugile给出的其他答案一样,没有必要先创建一个单独的命名窗口。
cv2.imshow('window_name', img)
cv2.setWindowProperty('window_name', cv2.WND_PROP_TOPMOST, 1)
对我来说,在 macOS 上这个工作很好,我认为它在其他操作系统上也会很好地运行。
destroyWindow( "windowName" );
namedWindow( "windowName", WINDOW_NORMAL );
moveWindow( "windowName", posx, posy );
imshow( "windowName", frame );
这是一个循环,其序列如下: