Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python OpenCV open window on top of other applications

Tags:

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

like image 819
s-low Avatar asked Jul 10 '15 21:07

s-low


People also ask

Is OpenCV better than pillow?

OpenCV is written in C and C++ whereas PIL is written using Python and C, hence just from this information, OpenCV seems faster. While dealing with 1000s of images for data extraction, the processing speed 🚀 matters. Here is a quick comparison of these two libraries.

What does .read do OpenCV?

. read() in OpenCV returns 2 things, boolean and data. If there are not 2 variables, a tuple will be assigned to one variable. The boolean is mostly used for error catching.

What is cv2 moveWindow?

Python OpenCV – moveWindow() FunctionIf we want to show image windows at a specific position moveWindow() function of OpenCV will do it. Syntax: cv2.moveWindow(window_Name,x,y) Parameters: window_name: name of the window which you want to move at particular position. x: Value of x coordinate.

Is OpenCV a Python library?

OpenCV is a Python open-source library, which is used for computer vision in Artificial intelligence, Machine Learning, face recognition, etc.


2 Answers

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.

like image 183
pradyunsg Avatar answered Sep 17 '22 14:09

pradyunsg


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)
{
like image 21
Mark Setchell Avatar answered Sep 17 '22 14:09

Mark Setchell