You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整cv2.imshow输出窗口至适配900尺寸屏幕?

解决OpenCV imshow窗口超出屏幕的问题

针对你遇到的cv2.imshow窗口超出屏幕的问题,有两种实用的解决方案:

方法1:创建可调整窗口并固定显示尺寸

通过设置窗口属性为可调整,再指定窗口的显示宽度(比如900),高度按图像比例自动计算,确保窗口适配屏幕:

修改原代码中cv2.imshow('img', image)及后续部分为:

# 创建支持手动调整大小的窗口
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
# 计算高度比例,确保宽度固定为900时图像不变形
height = int(image.shape[0] * 900 / image.shape[1])
# 设置窗口大小
cv2.resizeWindow('img', 900, height)
cv2.imshow('img', image)
cv2.waitKey(0)

方法2:缩放图像后再显示

直接将原始图像缩放到最长边不超过900的尺寸,再显示缩放后的图像,这样窗口会自动适配图像大小:

修改原代码中cv2.imshow('img', image)及后续部分为:

max_display_size = 900
img_height, img_width = image.shape[:2]

# 计算缩放比例,保证最长边不超过设定值
scale_ratio = max_display_size / max(img_height, img_width)
if scale_ratio < 1:
    # 缩放图像
    resized_img = cv2.resize(
        image,
        (int(img_width * scale_ratio), int(img_height * scale_ratio)),
        interpolation=cv2.INTER_AREA  # 缩小用这个插值更清晰
    )
else:
    resized_img = image.copy()

cv2.imshow('img', resized_img)
cv2.waitKey(0)

完整修改后的代码

以下是整合方法2的完整代码,你可以直接替换原代码使用:

# Import required modules
import cv2
import numpy as np
import os
import glob
    
    
# Define the dimensions of checkerboard
CHECKERBOARD = (9, 6)
    
# stop the iteration when specified
# accuracy, epsilon, is reached or
# specified number of iterations are completed.
criteria = (cv2.TERM_CRITERIA_EPS +
            cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

# Vector for 3D points
threedpoints = []

# Vector for 2D points
twodpoints = []

# 3D points real world coordinates
objectp3d = np.zeros((1, CHECKERBOARD[0]
                      * CHECKERBOARD[1],
                      3), np.float32)
objectp3d[0, :, :2] = np.mgrid[0:CHECKERBOARD[0],
                               0:CHECKERBOARD[1]].T.reshape(-1, 2)
prev_img_shape = None

# Extracting path of individual image stored
# in a given directory. Since no path is
# specified, it will take current directory
# jpg files alone
images = glob.glob('*.jpeg')

for filename in images:
    print(filename)
    image = cv2.imread(filename)
    grayColor = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Find the chess board corners
    # If desired number of corners are
    # found in the image then ret = true
    ret, corners = cv2.findChessboardCorners(
                    grayColor, CHECKERBOARD,
                    cv2.CALIB_CB_ADAPTIVE_THRESH
                    + cv2.CALIB_CB_FAST_CHECK +
                    cv2.CALIB_CB_NORMALIZE_IMAGE)

    # If desired number of corners can be detected then,
    # refine the pixel coordinates and display
    # them on the images of checker board
    if ret == True:
        threedpoints.append(objectp3d)

        # Refining pixel coordinates
        # for given 2d points.
        corners2 = cv2.cornerSubPix(
            grayColor, corners, (11, 11), (-1, -1), criteria)

        twodpoints.append(corners2)

        # Draw and display the corners
        image = cv2.drawChessboardCorners(image,
                                          CHECKERBOARD,
                                          corners2, ret)

    # 缩放图像适配900尺寸屏幕
    max_display_size = 900
    img_height, img_width = image.shape[:2]
    scale_ratio = max_display_size / max(img_height, img_width)
    if scale_ratio < 1:
        resized_img = cv2.resize(
            image,
            (int(img_width * scale_ratio), int(img_height * scale_ratio)),
            interpolation=cv2.INTER_AREA
        )
    else:
        resized_img = image.copy()
    
    cv2.imshow('img', resized_img)
    cv2.waitKey(0)

cv2.destroyAllWindows()

内容的提问来源于stack exchange,提问作者6digit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 15:36:34