【问题标题】:Resizing RGB image with cv2 numpy and Python 2.7使用 cv2 numpy 和 Python 2.7 调整 RGB 图像的大小
【发布时间】:2015-10-16 20:30:54
【问题描述】:

我想使用 Python 2.7 调整 RGB 图像的大小。我尝试使用 cv2.resize 函数,但它总是返回单通道图像:

(Pdb) x = cv2.imread('image.jpg')
(Pdb) x.shape
(50, 50, 3)
(Pdb) x = cv2.resize(x, (40, 40)) 
(Pdb) x.shape
(40, 40)

我希望 x.shape 的最终输出为 (40, 40, 3)。

除了循环遍历三个通道并分别调整每个通道的大小之外,还有其他更 Pythonic 的方法来调整 RGB 图像的大小吗?

【问题讨论】:

    标签: python image python-2.7 opencv numpy


    【解决方案1】:

    试试这个代码:

    import numpy as np
    import cv2
    
    image = cv2.imread('image.jpg')
    cv2.imshow("Original", image)
    """
    The ratio is r. The new image will
    have a height of 50 pixels. To determine the ratio of the new
    height to the old height, we divide 50 by the old height.
    """
    r = 50.0 / image.shape[0]
    dim = (int(image.shape[1] * r), 50)
    
    resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
    cv2.imshow("Resized (Height) ", resized)
    cv2.waitKey(0)
    

    【讨论】:

      猜你喜欢
      • 2020-08-01
      • 1970-01-01
      • 2020-03-03
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 2021-02-26
      相关资源
      最近更新 更多