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

新手上路,请多包涵

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

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

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

原文由 Zynk 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.2k
2 个回答

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

 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

原文由 Zynk 发布,翻译遵循 CC BY-SA 4.0 许可协议

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

原文由 nathancy 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题