OpenCV display image in fullscreen on Mac without white border - python

I'm running opencv version 3.1.0 and python 2.7 on Mac OSX 10.9 and want to display a black image on fullscreen. My screen's resolution is 2880x1800.
However when I attempt to do so, there is a large white border on the top of the screen.
Here's my code, note that black.jpg is a 2880x1800 image.
import cv2
img = cv2.imread("black.jpg")
cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
cv2.imshow("window", img)
while True:
key = cv2.waitKey(20)
#exit on ESC
if key == 27:
break
I've also tried to just create a black image manually, using the following code.
import cv2
import numpy as np
img = np.zeros((1800, 2880))
cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("window", cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
cv2.imshow("window",img)
cv2.waitKey(0)
I've adjusted the dimensions of the numpy array to make it larger but the border still remains.
Doing some research I've discovered that this may be a bug with opencv. However the solutions only apply to windows operating systems, see the following:
OpenCV window in fullscreen and without any borders
and
How to display an image in full screen borderless window in openCV
If anyone has an idea of how to fix the bug for Macs I can go ahead and rebuild the library. Or if I am doing something incorrectly please let me know. Thanks!

I guess the key is to adapt the image size to the REAL macbook screen resolution. 1800x2880 probably is not the one you are currently adopting.
Find out the display resolution of your macbook at System Preferences -> Display -> Scaled
Code with OpenCV
import cv2
def show_full_screen_image():
while True:
print 'loading images...'
img = cv2.imread('preferred_image.png')
# Note: 900x1440 is the resolution with my MBP
img = cv2.resize(img, (1440, 900), interpolation=cv2.INTER_CUBIC)
cv2.namedWindow("test", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("test", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("test", img)
key=cv2.waitKey(0)
if key==27: # ESC to exit
break
if __name__ == '__main__':
show_full_screen_image()

The problem in those links is not the presence of a border, but the window's background showing through for some reason. From what I understand, OpenCV's namedWindow actually creates a two windows, one inside the other. The "white lines" are actually the grey background of the parent window. You might be facing the same problem in OSX since openCV creates windows this way.
I solved it in windows by changing the background colour of the parent window through the Windows API, maybe you can try something similar in OSX.

Related

Python Image Window Not Closing Properly on Mac

I'm working with OpenCV and trying to figure this out. When I want to read and show an image:
import cv2
img = cv2.imread('baboon.jpg', 0)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyWindow('image')
I write this. But whenever the image display window pops up, I'm not able to close it and execute everything after
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyWindow('image')
because the window will not close unless I quit it.
For example, if I run this code
import cv2
img = cv2.imread('baboon.jpg', 0)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyWindow('image')
#Nothing after this will get executed
print('Hello')
I get no output in my console, but the image window does pop up.
How can I fix this?
(I'm using Pycharm if that helps)
EDIT:
It seems that whenever I press any key the window closes and it works as intended (code after the comment is executed). Not sure why this is happening.
I believe the quit is acting as an interrupt, which quits the entire python program. This is different than pressing a key, which is incorporated into the logic of the actual program and continues to completion:
cv2.waitKey(0) # waits for any key to be pressed to continue the logic of the program
# this is what actually allows you to "display" an image without it immediately disappearing.
I believe you can click on anywhere in the pic and it would return to the code window

how to display 2 images one after the another in sequence in same window using OpenCV?

I am using OpenCV in python and the aim of the code is to display one image after another in the same window without destroying it. For eg. i1.jpg is displayed and then in the same window left arrow is displayed after a pause of 5s.
In my case, i1.jpg is displayed and then the window is destroyed and then i2.jpg is displayed in another window.
Here is the code for the same -
t_screen_time = 5000
import numpy
import cv2
left_arrow = "left_arrow.jpg"
right_arrow = "right_arrow.jpg"
img = cv2.imread(left_arrow)
cv2.imshow('image',img)
cv2.waitKey(t_screen_time)
cv2.destroyAllWindows()
img = cv2.imread(right_arrow)
cv2.imshow('image',img)
cv2.waitKey(t_screen_time)
cv2.destroyAllWindows()
The argument in cv2.waitKey() is the time to wait for a keypress and not exactly the time the image is displayed for.
cv::imshow() or cv2.imshow() was not designed to be a complete GUI. It was designed to let you quickly debug and display images/videos.
You can achieve what you need using some form of sleep() in a thread or so. But this is harder to do in python. You could also just edit your code to
t_screen_time = 5000
import numpy
import cv2
left_arrow = r"first/image"
right_arrow = r"second/image"
img = cv2.imread(left_arrow)
cv2.imshow('image',img)
cv2.waitKey(t_screen_time)
img = cv2.imread(right_arrow)
cv2.imshow('image',img)
cv2.waitKey(t_screen_time)
cv2.destroyAllWindows()
by removing the cv2.destroyAllWindows() before displaying the second image. I've tested this code and it works, but if you're looking for more GUI functionality, use something like Qt.

Python OpenCV - waitKey(0) does not respond?

I'm using opencv 2.4.7 on ubuntu 12.04. I'm programming with python and I have a problem when i run this script:
import cv2
img = cv2.imread('347620923614738322_233985812.jpg')
cv2.namedWindow("window")
cv2.imshow("window", img)
cv2.waitKey(0)
The problem is that the script doesn't stop when I close the image. I searched information about waitKey and I found that using cv2.waitKey(0) is correct.
I don't understand, where is the problem?
I found that it works if i press the key whilst the window is in focus. If the command line is in focus then nothing happens
Adding a cv2.waitKey(1) after you destroy the window should work in this case.
cv2.imshow('imgae',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
This code works for me from IDLE:
# -*- coding: utf-8 -*-
# Objectif : découvrir le fonctionnement d'opencv-python
# http://opencv-python-tutroals.readthedocs.org/en/latest/index.html
import numpy as np
import cv2
# Load an color image in grayscale
img = cv2.imread('Lena.tiff',0)
WINDOW_NAME = 'Image de Lena'
cv2.namedWindow(WINDOW_NAME, cv2.CV_WINDOW_AUTOSIZE)
cv2.startWindowThread()
# Display an image
cv2.imshow(WINDOW_NAME,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Hope this helps for future readers.
click on the image window(active) and then press and key, don't write in the terminal window.
Here a minimalistic code for best performance on all platforms:
import cv2
img = cv2.imread("image.jpg")
cv2.imshow("Window", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
And now some observations:
When the user wants to close the window through the press of the 0 key, he/she has to make sure that the 0 key is pressed whilst the window is in focus. Because as stated above, if the terminal is in focus nothing happens and code execution stucks at the cv2.waitKey(0) until the 0 key is pressed properly whilst the window is in focus.
Pressing the 0 key whilst the window is in focus is the right way to close the window and make sure that, once the window is destroyed in line cv2.destroyAllWindows() and the program ends, the user can get back the control of the terminal.
If the window is exited by mouse click, the window will be destroyed yes, but the user will very much end up in the situation of not being able to get back the control of the terminal. In this kind of situation the user may want to shut down the unresponsive terminal and open a new one.
Try to execute the script directly from the Terminal works 100% for me but not from an IDE for example , i explain :
I'm using fedora 20 and got the same problem, copying the first example from official opencv python tutorial, i'm using :
Fedora 20 64bit
Spyder IDE for python
Python Version 2.7.5
Fedora 64 bit
OpenCV 2.4.7
Here is the code for test
import cv2
img = cv2.imread('/path/image1.jpeg',0)
cv2.imshow('Display',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
When running this script using F5 from Spyder, it runs it using the embedded python terminal with this line :
runfile('/home/user/Workspace/test.py', wdir=r'/home/user/Workspace')
In this instance, cv2.waitKey(0) or cv2.waitKey(-1) are not working and windows remains opened after pressing keys with the code of the example
Trying to close the windows will result in a "Not Responding , Force to Quit" Alert
But when executing the script from Terminal , it works 100%
didn't found the problem origin , will update if i find it.
Solved in Spyder under Ubuntu by following [Run]->[Configuration per file]->[Execute in an external system terminal].
Well the use cv2.waitKeyEx(0).
It runs in the same way and waits for your response until you revoke it.
I hope it helps.
Nithesh varmma
cv2.waitKey(0) means that script is in infinity loop with 0 miliseconds wait after loop
.only specified key can stop it.
you did not specified end of app condition.
Try this code:
Using other keys for the waitKey() function of opencv
There is a problem with unix based system running opencv programs from python notebooks.
Check this alternate method My suggestion is to run code in python in terminal. You will not face any kind of problem
Copy the same code and save with filename.py
import cv2
input = cv2.imread('path_to_image.png')
cv2.imshow('Hello World', input)
cv2.waitKey(0)
cv2.destroyAllWindows()
then open specific directory and then open terminal
Steps:
Open Terminal
cd path/to/filename.py
source activate YOURPROFILE
python filename.py
This will solve problem
https://youtu.be/8O-FW4Wm10s
This will work even if you close the window using the cross button on the window.
import numpy as np
import cv2
img = cv2.imread('arvid.jpg', 0)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', img)
while True:
k = cv2.waitKey(30) & 0xFF
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
cv2.imwrite('arvid2.jpg', img)
cv2.destroyAllWindows()
if cv2.getWindowProperty("image", 0) == -1:
break
This problem occurs in some earlier version of opencv.
So you just need to update opencv to the latest version. Version 4.0.0.21 worked for me.
Use the command below.
update opencv to version 4.0.0.21.

Python - OpenCV - imread - Displaying Image

I am currently working on reading an image and displaying it to a window. I have successfully done this, but upon displaying the image, the window only allows me to see a portion of the
full image. I tried saving the image after loading it, and it saved the entire image. So I am fairly certain that it is reading the entire image.
imgFile = cv.imread('1.jpg')
cv.imshow('dst_rt', imgFile)
cv.waitKey(0)
cv.destroyAllWindows()
Image:
Screenshot:
Looks like the image is too big and the window simply doesn't fit the screen.
Create window with the cv2.WINDOW_NORMAL flag, it will make it scalable. Then you can resize it to fit your screen like this:
from __future__ import division
import cv2
img = cv2.imread('1.jpg')
screen_res = 1280, 720
scale_width = screen_res[0] / img.shape[1]
scale_height = screen_res[1] / img.shape[0]
scale = min(scale_width, scale_height)
window_width = int(img.shape[1] * scale)
window_height = int(img.shape[0] * scale)
cv2.namedWindow('dst_rt', cv2.WINDOW_NORMAL)
cv2.resizeWindow('dst_rt', window_width, window_height)
cv2.imshow('dst_rt', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
According to the OpenCV documentation CV_WINDOW_KEEPRATIO flag should do the same, yet it doesn't and it's value not even presented in the python module.
This can help you
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
In openCV whenever you try to display an oversized image or image bigger than your display resolution you get the cropped display. It's a default behaviour.
In order to view the image in the window of your choice openCV encourages to use named window. Please refer to namedWindow documentation
The function namedWindow creates a window that can be used as a placeholder for images and trackbars. Created windows are referred to by their names.
cv.namedWindow(name, flags=CV_WINDOW_AUTOSIZE)
where each window is related to image container by the name arg, make sure to use same name
eg:
import cv2
frame = cv2.imread('1.jpg')
cv2.namedWindow("Display 1")
cv2.resizeWindow("Display 1", 300, 300)
cv2.imshow("Display 1", frame)

SimpleCV webcam black image

I need to take a single snapshot from webcam. I choice SimpleCV for this task.
Now i try to get a single image and show it:
from SimpleCV import Camera
cam = Camera()
img = cam.getImage()
img.show()
But i see only black image. I think camera is not ready at this moment, because if I call time.sleep(10) before cam.getImage() all works good.
What the right way for this? Thank you!
Have you installed PIL? I had similar problems, but on installing PIL everything works fine. Hope this helps.
You can download PIL from Pythonware
I ran into this same issue and came up with the following work-around. Basically it grabs an image, tests the middle pixel to see if it's black (0,0,0). If it is, then it waits 1 second and tries again.
import time
from SimpleCV import Camera
cam = Camera()
r = g = b = 0
while r + g + b < 0.01:
img = cam.getImage()
r, g, b = img[img.width/2, img.height/2]
print("r: {} g: {} b: {}".format(r,g,b))
time.sleep(1)
img.save(file_name)
Your problem may be that when the script ends the camera object doesn't seem to release the webcam for other programs. When you wait to run the script Windows frees it up (maybe?) so it will work again. If you use it will the old script still thinks it owns the webcam, it shows up as black. Anyway the solution here seems to work. Add "del cam" to the end of your script which will make the camera object go away and let you use the camera again.
try this
from SimpleCV import Camera
cam = Camera(0)
while True:
img = cam.getImage()
img.show()

Categories