opencv icon indicating copy to clipboard operation
opencv copied to clipboard

OpenCV 3.4 Python/QT binding opens 'imshow'-window in the background

Open MikeMitterer opened this issue 7 years ago • 6 comments

System information (version)
  • OpenCV => 3.4-dev
  • Operating System / Platform => Mac 10.11
  • Compiler => Apple LLVM version 8.0.0 (clang-800.0.42.1)
  • Python => 3.6.4
  • QT => 5.10.0_1
Detailed description

imshow opens QT-Window in the background

Steps to reproduce
import cv2
file = cv2.imread('input.jpg')
cv2.imshow('Hello World', file)
cv2.waitKey(0)
cv2.destroyAllWindows()

MikeMitterer avatar Jan 19 '18 17:01 MikeMitterer

imshow() doesn't work without waitkey().

Usage questions should go to Users OpenCV Q/A forum: http://answers.opencv.org This tracker is for issues and bugs that needs fix in OpenCV.

alalek avatar Jan 19 '18 18:01 alalek

I'm using waitKey. I updated my question.

MikeMitterer avatar Jan 23 '18 08:01 MikeMitterer

@alalek @MikeMitterer I would like to add to this issue. I am using OpenCV 3.4.0 with C on Mac OS 10.13.2 and I am getting windows popping up in the background and out of focus. Each time I run the program, I have to fish the window in the background.

There are other people having the same issue. https://stackoverflow.com/questions/31350240/python-opencv-open-window-on-top-of-other-applications

Would it be possible to increase the priority of this issue?

Below is the code I am using.

#include <cv.h>
#include <highgui.h>

#define COLS 300
#define ROWS 300

int main(int argc, char *argv[])
{
	CvMat* mat = cvCreateMat(ROWS, COLS, CV_8UC3);

	// create window
	cvNamedWindow("window", CV_WINDOW_NORMAL);

        // do something
	int row, col;
	for (row = 0; row < ROWS; row++)
	{
		uchar* ptr = (uchar*)(mat->data.ptr + row * mat->step);
		for (col = 0; col < COLS; col++)
		{
			*(ptr + col*3+2) = 64;
			*(ptr + col*3+1) = 128;
			*(ptr + col*3)   = 128;
		}
	}

	// show image
	cvShowImage("window", mat);
	cvWaitKey(0);

	// free resources
	cvReleaseMat(&mat);
	cvDestroyWindow("window");
	return 0;
}

I experienced this problem in 2.4.13.5 and I attempted to resolve the issue. I made a patch for modules/highgui/src/window_cocoa.mm

--- window_cocoa.mm	2018-01-10 14:24:46.000000000 -0600
+++ window_cocoa_patched.mm	2018-01-10 14:25:10.000000000 -0600
@@ -520,6 +520,8 @@
     [window setAcceptsMouseMovedEvents:YES];
     [window useOptimizedDrawing:YES];
     [window setTitle:windowName];
+    [window setLevel:CGWindowLevelForKey(kCGMaximumWindowLevelKey)];
+    [NSApp activateIgnoringOtherApps:YES];
     [window makeKeyAndOrderFront:nil];
 
     [window setAutosize:(flags == CV_WINDOW_AUTOSIZE)];

This patch makes the window always on top and focused. The only issue is 'always'. It stays always on top and I do not know how to resolve this issue since I am not a Cocoa objc programmer.

jakeh12 avatar Jan 26 '18 20:01 jakeh12

I think that it would be a nice addition having a cv::activateWindow(const String &winname) on the highgui module that call SetForegroundWindow on win32, activateIgnoringOtherApps on macOS, etc. I can send a pull request.

zaerl avatar Sep 14 '18 08:09 zaerl

No need to introduce separate API call, just update the current windows creation code in cv::imshow() (or probably cv::namedWindow()) implementations.

Feel free to prepare PR onto 3.4 branch.

alalek avatar Sep 14 '18 09:09 alalek

This would be super helpful! I haven't found a solution to the window opening in the background. It makes working with opencv really tedious.

cronin4392 avatar Sep 17 '22 07:09 cronin4392

Could someone review/validate this patch on OSX: #22737 ?

alalek avatar Nov 03 '22 04:11 alalek

3.4 version: https://github.com/opencv/opencv/pull/22842

asmorkalov avatar Nov 21 '22 09:11 asmorkalov