OpenCV & Python - Image is too large to display

I have an image with a size of 6400 ร— 3200, while my screen is 1280 x 800. Therefore, the image needs to be changed only for display. I am using Python and OpenCV 2.4.9. According to the OpenCV Documentation ,

If you need to show an image that is larger than the screen resolution, you will need to call namedWindow ("", WINDOW_NORMAL) before your show.

This is what I do, but the image is not tied to the screen, only a part is shown, because it is too large. I also tried cv2.resizeWindow, but that doesn't make any difference.

import cv2 cv2.namedWindow("output", cv2.WINDOW_NORMAL) # Create window with freedom of dimensions # cv2.resizeWindow("output", 400, 300) # Resize window to specified dimensions im = cv2.imread("earth.jpg") # Read image cv2.imshow("output", im) # Show image cv2.waitKey(0) # Display the image infinitely until any keypress 
+29
source share
4 answers

Although I was expecting an automatic solution (automatically setting the screen), resizing also solves the problem.

 import cv2 cv2.namedWindow("output", cv2.WINDOW_NORMAL) # Create window with freedom of dimensions im = cv2.imread("earth.jpg") # Read image imS = cv2.resize(im, (960, 540)) # Resize image cv2.imshow("output", imS) # Show image cv2.waitKey(0) # Display the image infinitely until any keypress 
+40
source

Try it:

 image = cv2.imread("img/Demo.jpg") image = cv2.resize(image,(240,240)) 

The image size is now resized. The display will be displayed in 240x240.

+3
source

In opencv, cv.namedWindow () simply creates a window object of your choice, but does not resize the original image. You can use cv2.resize (img, resolution) to solve the problem.

This is what it displays, an image with a resolution of 740 * 411. The original image

 image = cv2.imread("740*411.jpg") cv2.imshow("image", image) cv2.waitKey(0) cv2.destroyAllWindows() 

An image with a resolution of 100 * 200 after resizing is displayed here. Remember that the column for using the permission parameter is the row first.

Image after resizing

 image = cv2.imread("740*411.jpg") image = cv2.resize(image, (200, 100)) cv2.imshow("image", image) cv2.waitKey(0) cv2.destroyAllWindows() 
+1
source

Other answers perform a fixed size (width, height) . If you want to resize to a specific size while maintaining aspect ratio, use this

 def ResizeWithAspectRatio(image, width=None, height=None, inter=cv2.INTER_AREA): dim = None (h, w) = image.shape[:2] if width is None and height is None: return image if width is None: r = height / float(h) dim = (int(w * r), height) else: r = width / float(w) dim = (width, int(h * r)) return cv2.resize(image, dim, interpolation=inter) 
Example

Example

 image = cv2.imread('img.png') resize = ResizeWithAspectRatio(image, width=1280) # Resize by width OR # resize = ResizeWithAspectRatio(image, height=1280) # Resize by height cv2.imshow('resize', resize) cv2.waitKey() 
+1
source

Source: https://habr.com/ru/post/1242211/


All Articles