OpenCV 和 Python - 图像太大而无法显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35180764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:06:09  来源:igfitidea点击:

OpenCV & Python - Image too big to display

pythonimageopencvimage-processingimshow

提问by Zynk

I have an image that is 6400?×?3200, while my screen is 1280 x 800. Therefore, the image needs to be resized for display only. I am using Python and OpenCV 2.4.9. According to OpenCV Documentation,

我有一个 6400?×?3200 的图像,而我的屏幕是 1280 x 800。因此,图像需要调整大小以仅显示。我正在使用 Python 和 OpenCV 2.4.9。根据OpenCV 文档

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

如果需要显示大于屏幕分辨率的图像,则需要在 imshow 之前调用 namedWindow("", WINDOW_NORMAL)。

That is what I am doing, but the image is not fitted to the screen, only a portion is shown because it's too big. I've also tried with cv2.resizeWindow, but it doesn't make any difference.

这就是我正在做的,但图像不适合屏幕,只显示了一部分,因为它太大了。我也尝试过 cv2.resizeWindow,但它没有任何区别。

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

采纳答案by Zynk

Although I was expecting an automatic solution (fitting to the screen automatically), resizing solves the problem as well.

虽然我期待一个自动解决方案(自动适应屏幕),但调整大小也解决了这个问题。

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

回答by FrancisYL

In opencv, cv.namedWindow() just creates a window object as you determine, but not resizing the original image. You can use cv2.resize(img, resolution) to solve the problem.

在 opencv 中, cv.namedWindow() 只是根据您的决定创建一个窗口对象,而不是调整原始图像的大小。您可以使用 cv2.resize(img,resolution) 来解决问题。

Here's what it displays, a 740 * 411 resolution image. The original image

这是它显示的内容,一个 740 * 411 分辨率的图像。 原图

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

Here, it displays a 100 * 200 resolution image after resizing. Remember the resolution parameter use column first then is row.

在这里,它在调整大小后显示 100 * 200 分辨率的图像。记住分辨率参数首先使用列然后是行。

Image after resizing

调整大小后的图像

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

回答by MARTIN ALEXIS SAMAN ARATA

Try this:

尝试这个:

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

The imageis now resized. Displaying it will render in 240x240.

image现在的调整。显示它将以 240x240 呈现。

回答by nathancy

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

其他答案执行固定(width, height)调整大小。如果您想在保持纵横比的同时调整到特定大小,请使用此

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

例子

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()

回答by Madhu C

Try with this code:

尝试使用此代码:

from PIL import Image

Image.fromarray(image).show()