首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >OpenCV & Python -图像太大,无法显示

OpenCV & Python -图像太大,无法显示
EN

Stack Overflow用户
提问于 2016-02-03 15:14:17
回答 13查看 267.7K关注 0票数 112

我有一个6400 × 3200的图像,而我的屏幕是1280x800。因此,图像需要调整大小,仅供显示。我使用的是Python和OpenCV 2.4.9。据OpenCV文档称,

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

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

代码语言:javascript
运行
复制
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
EN

回答 13

Stack Overflow用户

回答已采纳

发布于 2016-02-03 17:02:15

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

代码语言:javascript
运行
复制
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
票数 130
EN

Stack Overflow用户

发布于 2019-09-27 02:02:51

其他答案执行固定的(width, height)调整大小。如果要在保持高宽比的同时将大小调整到特定大小,请使用以下方法

代码语言:javascript
运行
复制
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)

示例

代码语言:javascript
运行
复制
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()
票数 50
EN

Stack Overflow用户

发布于 2020-09-16 07:04:20

例如,使用这个:

代码语言:javascript
运行
复制
cv2.namedWindow('finalImg', cv2.WINDOW_NORMAL)
cv2.imshow("finalImg",finalImg)
票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35180764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档