Is there a way to force the window displayed by OpenCV (cv2.imshow())when displaying an image to fit to the width and height of the image without the need to resize by the mouse it for that ?
You need to pass CV_WINDOW_AUTOSIZE when creating the namedWindow (or WINDOW_AUTOSIZE if you import cv2 instead of cv)
Here is an example:
cv2.namedWindow("window", cv2.WINDOW_AUTOSIZE)
# or cv.namedWindow("window",cv.CV_WINDOW_AUTOSIZE)
cv2.imshow("window", yourimage)
In opencv 4.0.0 the below solution works:
import cv2
cv2.namedWindow("myImage", cv2.WINDOW_NORMAL)
image = cv2.imread("./image.jpg")
cv2.imshow("myImage", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Related
I am using opencv in Python to play a video file full screen. The video is 4:3 aspect ratio, and imshow is by default maintaining the aspect ratio by creating a gray pillarbox along the right side of the screen.
I have not been succesfull in finding any documentation about how to manipulate this – i.e. how to change the color of the pillarbox / what I would presume is the default color of the empty window at the OS level? If changing the color is not explicitly supported by opencv is there a workaround? i.e. draw a black rectangle underneith the video frame?
#!/usr/bin/env python3
import cv2 as cv
cap = cv.VideoCapture("video.mp4")
while(cap.isOpened()):
ret,frame = cap.read()
frame = cv.resize(frame,(720,480))
cv.imshow("video", frame)
cv.namedWindow("video", cv.WND_PROP_FULLSCREEN)
cv.setWindowProperty("video", cv.WND_PROP_FULLSCREEN, cv.WINDOW_FULLSCREEN)
if cv.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()
cap = cv2.VideoCapture(0)
and
img = cv2.imread(/users/..../jumpingjacks.jpeg, 1)
cv2.imshow('Jumping Jacks', img)
when my prog run these two codes, the webcam feed and image doesn't pop out, but instead, it shows as minimized version, which requires me to press the icon at the bottom of the screen to bring it forward. anyidea what caused this ?
Found the answer by trying out this code:
img = cv2.imread('/users/..../jumpingjacks.jpeg', 1)
cv2.imshow('Jumping Jacks', img)
cv2.setWindowProperty("Jumping Jacks", cv2.WND_PROP_TOPMOST, 1)
cv2.waitKey(5000)
cv2.destroyAllWindows()
I am running Anaconda install of python35 with cv2 install from menpo.
I am having trouble with cv2.imshow() inconsistently placing the image window outside of the viewable screen when running code similar to the one below both as a standalone script and line by line in the console (cmd, spyder, ipython)...
import cv2
img = cv2.imread('Image71.jpg',0)
cv2.startWindowThread()
cv2.namedWindow('image')
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
I have also tried the above without cv2.starWindowThread() and cv2.namedWindow() with the same result. The window appears on my taskbar but is not in view, cv2.waitKey(0) responds to the keystroke, and I am not able to bring the window into view using any of the window arrangement shortcut keys for Windows 10 (e.g. alt+tab, Winkey + left, etc).
My OS is Win10 version 1709.
Any help is much appreciated, thx!
img = cv2.imread("test.png")
winname = "Test"
cv2.namedWindow(winname) # Create a named window
cv2.moveWindow(winname, 40,30) # Move it to (40,30)
cv2.imshow(winname, img)
cv2.waitKey()
cv2.destroyAllWindows()
wrapped answer by Kinght in a function for easy calling
def showInMovedWindow(winname, img, x, y):
cv2.namedWindow(winname) # Create a named window
cv2.moveWindow(winname, x, y) # Move it to (x,y)
cv2.imshow(winname,img)
img = cv2.imread('path.png')
showInMovedWindow('named_window',img, 0, 200)
I installed openCV and numpy libraries in python 2.7.
I've tested them using commands import cv2 and import numpy and it compiled.
But when I use the cv2.imshow('frame', ----) function it displays a window but not displaying the image. And it's showing " frame is Not Responding".
So, I tried with matplotlib functions for displaying image and it worked.
I inserted cv2.imshow function in the 2nd case and it worked.
Versions [Python-2.7.10, OpenCV-2.4.11]
Below is the code,
Case 1: Not Working,displaying window but not image (showing FRAME IS NOT RESPONDING)
import cv2
import numpy
img = cv2.imread('a.jpg')
cv2.imshow('FRAME',img)
Case 2: Working
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2
img = mpimg.imread('a.jpg')
img2 = cv2.imread('b.jpg')
cv2.imshow('FRAME',img2)
plt.imshow(img)
plt.show()
imshow should be followed by waitKey function which displays the image for specified milliseconds. Otherwise, it won’t display the image. For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). waitKey(25) will display a frame for 25 ms, after which display will be automatically closed. (If you put it in a loop to read videos, it will display the video frame-by-frame). Here's a working example:
import cv2
img = cv2.imread('a.jpg')
cv2.imshow('FRAME', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Try using imread like this
img = cv2.imread('a.jpg',0)#grayscale
img = cv2.imread('a.jpg',1)#rgb
I'm trying to show images with cv2 library in my Jupiter Notebook with cv2.imshow(img) and it shows as expected, but I can not use or don't know how to use cv2.waitKey(0), hence the cell will not stop executing.
cv2.waitKey(0) works in script, but not in Notebook.
Here's a snippet:
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
How do I stop executing cell without restarting the whole kernel?
So, thanks to #Micka, here's the solution:
You must write cv2.startWindowThread() first, explained here.
I found the answer from primoz very useful. Here is a code for a function that reads an image from specified path, draws the image, waits for any input to close a window and returns the image object.
import cv2
def cv2_imshow(path, title):
"""
function:
- reads image from `path`,
- shows image in a separate window,
- waits for any key to close the window.
return: image object
"""
img = cv2.imread(path)
cv2.startWindowThread()
cv2.imshow(title, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
return img
Call the function with image path and title:
img_raw = cv2_imshow(path = r'img\example\test.png', title = "raw image")
I have just developed a library to facilitate the opencv functionality in Jupyter.
I used buttons in jupyter for simulating waitKey
It shows the image in the jupyer.
Document
Installation
pip install opencv_jupyter_ui
Usage
You need to only change cv2 to jcv2.
import opencv_jupyter_ui as jcv2
...
jcv2.imshow(img,title)
if jcv2.waitKey(1000)==ord('q'):
break
jcv2.destroyAllWindows()