Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

Using cvShowImage, one can easily show an image in OpenCV. However, how do you tell OpenCV to show the window on top of every other window?

I run a full screen OpenGL application while showing images. The first time the OpenCV window pops up on top of my application window, but if I click on the window of my application (i.e. give focus back to it), then I can't manage to have the OpenCV come back on top of the OpenGL window, even when destroying and recreating the window.

I thought of renaming the window each time, but is there another way to do it?

like image 366
seb Avatar asked Dec 07 '11 15:12

seb


2 Answers

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) 
like image 79
Bruce Christensen Avatar answered Oct 06 '22 13:10

Bruce Christensen


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.

like image 22
xczhang Avatar answered Oct 06 '22 11:10

xczhang