OpenCV: how to force the image window to appear on top of other windows?

Beginning with OpenCV releases 3.4.8 and 4.1.2, setWindowProperty has a WND_PROP_TOPMOST property that you can set. Example Python code:

import cv2
import numpy as np

window_name = "image"
img = np.zeros([480, 640, 1])
cv2.imshow(window_name, img)
cv2.setWindowProperty(window_name, cv2.WND_PROP_TOPMOST, 1)
cv2.waitKey(0)

OK, I figured it out which works for both OSX and Windows. You just need to create a full-screen window and show it for a very short time, then your next window from OpenCV will be in front. So, first to open a full-screen window:

cv::namedWindow("GetFocus", CV_WINDOW_NORMAL);
cv::Mat img = cv::Mat::zeros(100, 100, CV_8UC3);
cv::imshow("GetFocus", img);
cv::setWindowProperty("GetFocus", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
waitKey(1);
cv::setWindowProperty("GetFocus", CV_WND_PROP_FULLSCREEN, CV_WINDOW_NORMAL);
destroyWindow("GetFocus");

And then you can open up anther window that actually show the image:

Mat your_image = ...;
cv::namedWindow("ShowImg");
cv::imshow("ShowImg", your_image);

It works for me.


OpenCV has no native way to do this (that I'm aware of).

The answer is platform dependent. If your target is Windows, check this answer and then this and this will certainly be useful.

If you are on Linux, you need to take a look at how OpenCV was compiled and check what system its built on (probably GTK+ 2.x). Then, do some research of your own.


On MAC-OSX (El Capitan) OpenCV 3.1.0, calling moveWindow seems to bring the window just moved to the top.

Tags:

C

Opencv