33

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

5
  • 2
    You are using the old version (python module for the new one named cv2). Try to update it. Commented Oct 10, 2013 at 3:21
  • Sorry, I forgot to mention that I am using cv2 as cv. Commented Oct 14, 2013 at 1:06
  • This code looks fine. Can you show us a screenshot? Commented Oct 14, 2013 at 2:45
  • i.sstatic.net/ug90J.jpg i.sstatic.net/qsJuF.jpg Commented Oct 16, 2013 at 20:49
  • It wouldn't let me add the images to my question, but here are links to the original image and a screenshot of the result. Commented Oct 16, 2013 at 20:50

3 Answers 3

36

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Use floating point screen_res to get a correctly scaled starting image: screen_res = 1280., 720.
@Marc from __future__ import division look it up
I needed to add cv2.resize(img, (window_width, window_height)) before cv2.imshow('dst_rt', img)
3

This can help you

namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image );                   // Show our image inside it.

1 Comment

Isn't it WINDOW_AUTOSIZE instead of CV_WINDOW_AUTOSIZE?
1

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)

2 Comments

Does it make sense to have CV_WINDOW_AUTOSIZE if you then explicitly invoke resizeWindow() ?
@javadba your right, CV_WINDOW_AUTOSIZE is not recommended when we explicitly invoke resizeWindow() thanks for catching that!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.