23

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

Community
  • 1
  • 1
s-low
  • 706
  • 2
  • 8
  • 21
  • I am using "keyboard mastro " mac app to do this . First set up a hot key in keyboard mastro as active "python" application in the front with string key "opencvwindowinthefront" . Then in python script , import os cmd = """ osascript -e 'tell application "System Events" to keystroke "openvwindowinthefront" ' """ os.system(cmd) – Richard Mao May 25 '16 at 13:39

3 Answers3

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.

pradyunsg
  • 18,287
  • 11
  • 43
  • 96
  • Works for me. If encounter error: ```21:62: execution error: Finder got an error: Can’t set process "Python" to true. (-10006)```, try to change ```Python``` to ```python``` – Mingjiang Shi Jan 03 '19 at 02:57
  • Doesn't work for me even with @MingjiangShi's tip. I get `21:62: execution error: Finder got an error: Can’t set process "python" to true. (-10006)` – Tom Jun 06 '19 at 15:47
  • @Tom I suspect the the python process name is neither Python nor python. Maybe something else like python2 or python27, etc. Try to find out the correct process name in activity monitor and change the script accordingly. – Mingjiang Shi Jun 08 '19 at 14:41
  • Works for me after I change `python` to `python3.7`. Find out the process name in "Activity Monitor" app > "Memory" tab. – Eran W Jun 22 '19 at 13:19
  • another way to find process name in code: (need to install `psutil`): `pid = os.getpid(); process = psutil.Process(pid); process_name = process.name()` – Eran W Jun 22 '19 at 13:20
  • Note this does not work on macOS Big Sur 11.1. – Rob Feb 18 '21 at 14:15
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)
{
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
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.

stovfl
  • 14,998
  • 7
  • 24
  • 51
  • 3
    This has no effect for me. Window still opens below as before. Anaconda Python 3.5, opencv 3.2.0, Mac OS X Sierra 10.12.5. Instead, see this thread https://stackoverflow.com/a/8775078/4259243. After opening the window, running `os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "python" to true' ''')` works. – sh37211 Jul 04 '17 at 16:47
  • Note this does not work on macOS Big Sur 11.1. – Rob Feb 18 '21 at 14:15
  • Not working for me, macOS 12.3.1 – Kao-Yuan Lin May 12 '22 at 03:48