OpenCV & Python - 图片太大无法显示

OpenCV & Python - Image too big to display

我有一张 6400∆×∆3200 的图片,而我的屏幕是 1280 x 800。因此,图片需要调整大小才能显示。我正在使用 Python 和 OpenCV 2.4.9。 根据OpenCV Documentation

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.

这就是我正在做的,但是图像不适合屏幕,只显示了一部分,因为它太大了。我也尝试过 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

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

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

在 OpenCV 中,cv2.namedWindow() 只是创建一个 window 对象,但不会调整原始图像的大小。您可以使用cv2.resize(img, resolution)来解决问题。

这是它显示的内容,一张 740 * 411 分辨率的图片。

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

这里显示的是一张100*200分辨率的图片,调整大小后。记住分辨率参数先用列再用行。

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

试试这个:

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

image 现已调整大小。显示它将以 240x240 呈现。

其他答案执行固定的 (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)

示例

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

尝试使用此代码:

from PIL import Image

Image.fromarray(image).show()

例如:

cv2.namedWindow('finalImg', cv2.WINDOW_NORMAL)
cv2.imshow("finalImg",finalImg)

看起来 opencv 库对传递给方法的参数非常敏感。以下代码使用 opencv 4.3.0 对我有用:

win_name = "visualization"  #  1. use var to specify window name everywhere
cv2.namedWindow(win_name, cv2.WINDOW_NORMAL)  #  2. use 'normal' flag
img = cv2.imread(filename)
h,w = img.shape[:2]  #  suits for image containing any amount of channels
h = int(h / resize_factor)  #  one must compute beforehand
w = int(w / resize_factor)  #  and convert to INT
cv2.resizeWindow(win_name, w, h)  #  use variables defined/computed BEFOREHAND
cv2.imshow(win_name, img)

resizeWindow 对我有用的唯一方法是在 imshow 之后使用它。这是我使用的顺序:

# Create a Named Window
cv2.namedWindow(win_name, cv2.WINDOW_NORMAL)

# Move it to (X,Y)
cv2.moveWindow(win_name, X, Y)
    
# Show the Image in the Window
cv2.imshow(win_name, image)
    
# Resize the Window
cv2.resizeWindow(win_name, width, height)
    
# Wait for <> miliseconds
cv2.waitKey(wait_time)

cv2.WINDOW_NORMAL 选项工作正常,但它第一次以标准尺寸显示 window。

如果您像计算机中的任何其他 windows 一样调整 window 的大小,请将鼠标放在要调整大小的 window 的边缘上,然后将鼠标拖动到你想要的位置。如果你这样做将 window 的宽度和高度都调整到你想要获得的大小。

在您刷新 window 之后,通过执行代码,OpenCV 将生成具有上次显示或修改大小的 window。

此代码将调整图像的大小,以便它可以保持其宽高比并且只占据屏幕区域的指定部分。

它会根据您的屏幕尺寸和图像尺寸自动调整。

使用区域变量更改您希望图像能够占据的最大屏幕区域。该示例显示它以屏幕尺寸的四分之一显示。

import cv2
import tkinter as tk
from math import *

img = cv2.imread("test.jpg")
area = 0.25

h, w = img.shape[:2]
root = tk.Tk()
screen_h = root.winfo_screenheight()
screen_w = root.winfo_screenwidth()
vector = sqrt(area)
window_h = screen_h * vector
window_w = screen_w * vector

if h > window_h or w > window_w:
    if h / window_h >= w / window_w:
        multiplier = window_h / h
    else:
        multiplier = window_w / w
    img = cv2.resize(img, (0, 0), fx=multiplier, fy=multiplier)

cv2.imshow("output", img)
cv2.waitKey(0)

我也做了一个类似的函数,其中面积仍然是一个参数,但 window 高度和 window 宽度也是如此。

如果没有输入任何区域,那么它将使用定义的高度和宽度(window_h、window_w),window 大小是您希望图像适合的大小。

如果为所有参数提供输入,则 'area' 优先。

import cv2
import tkinter as tk
from math import *

def resize_image(img, area=0.0, window_h=0, window_w=0):
    h, w = img.shape[:2]
    root = tk.Tk()
    screen_h = root.winfo_screenheight()
    screen_w = root.winfo_screenwidth()

    if area != 0.0:
        vector = math.sqrt(area)
        window_h = screen_h * vector
        window_w = screen_w * vector

    if h > window_h or w > window_w:
        if h / window_h >= w / window_w:
            multiplier = window_h / h
        else:
            multiplier = window_w / w
        img = cv2.resize(img, (0, 0), fx=multiplier, fy=multiplier)

    return img

# using area
initial_image = cv2.imread("test.jpg")
resized_image = resize_image(initial_image, area=0.25))
cv2.imshow("output", resized_image)
cv2.waitKey(0)

# using window height and width
initial_image = cv2.imread("test.jpg")
resized_image = resize_image(initial_image, window_h = 480, window_w = 270))
cv2.imshow("output", resized_image)
cv2.waitKey(0)

试试这个代码:

img = cv2.imread("Fab2_0.1 X 1.03MM GRID.jpg", cv2.IMREAD_GRAYSCALE)
image_scale_down = 3
x = (int)(img.shape[0]/image_scale_down)
y = (int)(img.shape[1]/image_scale_down)
image = cv2.resize(img, (x,y))
cv2.imshow("image_title", image)
cv2.waitKey(5000)
cv2.destroyAllWindows()

最赞的答案是完美的! 我只是为那些想要根据比例进行“动态”调整大小处理的人添加我的代码。

import cv2
from win32api import GetSystemMetrics

def get_resized_for_display_img(img):
    screen_w, screen_h = GetSystemMetrics(0), GetSystemMetrics(1)
    print("screen size",screen_w, screen_h)
    h,w,channel_nbr = img.shape
    # img get w of screen and adapt h
    h = h * (screen_w / w)
    w = screen_w
    if h > screen_h: #if img h still too big
        # img get h of screen and adapt w
        w = w * (screen_h / h)
        h = screen_h
    w, h = w*0.9, h*0.9 # because you don't want it to be that big, right ?
    w, h = int(w), int(h) # you need int for the cv2.resize
    return cv2.resize(img, (w, h))

试试这个代码

img = cv2.resize(img,(1280,800))