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

9

我想使用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图像的大小吗?

1个回答

5

尝试使用这段代码:

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接