使用cv2 numpy和Python 2.7调整RGB图像的大小

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

(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图像的大小,而不是循环通过三个通道并分别调整每个通道的大小?

最佳答案 试试这段代码:

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)
点赞