Thursday, July 6, 2017

Python OpenCV open window on top of other applications

Leave a Comment

When executing an OpenCV python script containing: cv2.imshow(img) the resulting window opens behind my terminal window. This is a mild irritation - is there any way to get it to initially open in front / on top?

A number of people have asked questions (here and here) about forcing persistent behaviour but this is, I think, simpler.

Platform OS X and OpenCV 2.4.11

3 Answers

Answers 1

Open a namedWindow before imshow, for instance:

cv2.namedWindow('ImageWindowName', cv2.WINDOW_NORMAL) cv2.imshow('ImageWindowName',img) cv2.waitKey(0) cv2.destroyAllWindows() 

Comment if this makes any different.

Answers 2

I can do what I think you want by adding a single line to the following file in the OpenCV distribution:

modules/highgui/src/window_cocoa.mm 

It is around line 568, and is the single line after the word SETCHELL in the code below:

    [window setFrameTopLeftPoint:initContentRect.origin];      [window setFirstContent:YES];      [window setContentView:[[CVView alloc] init]];      [window setHasShadow:YES];     [window setAcceptsMouseMovedEvents:YES];     [window useOptimizedDrawing:YES];     [window setTitle:windowName];     [window makeKeyAndOrderFront:nil];  // SETCHELL - raise window to top of stacking order [window setLevel:CGWindowLevelForKey(kCGMaximumWindowLevelKey)];      [window setAutosize:(flags == CV_WINDOW_AUTOSIZE)];      [windows setValue:window forKey:windowName];      [localpool drain];     return [windows count]-1; }  CV_IMPL int cvWaitKey (int maxWait) { 

Answers 3

One way to work around this would be to set all windows from OpenCV "frontmost" using AppleScript.

subprocess.call(["/usr/bin/osascript", "-e", 'tell app "Finder" to set frontmost of process "Python" to true']) 

This way, all windows should be brought to the front.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment