Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: image

Screenshot: screenshot

like image 302
Sinjin Forde Avatar asked Oct 10 '13 01:10

Sinjin Forde


People also ask

How do I view images in OpenCV?

In order to load an image off of disk and display it using OpenCV, you first need to call the cv2. imread function, passing in the path to your image as the sole argument. Then, a call to cv2. imshow will display your image on your screen.

What does Imread return OpenCV?

OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2. imread() method loads an image from the specified file. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format) then this method returns an empty matrix.


2 Answers

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.

like image 182
Igonato Avatar answered Sep 27 '22 18:09

Igonato


This can help you

namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display. imshow( "Display window", image );                   // Show our image inside it. 
like image 24
Tom J Muthirenthi Avatar answered Sep 27 '22 20:09

Tom J Muthirenthi