Resize RGB images using cv2 numpy and Python 2.7

I want to resize an RGB image using Python 2.7. I tried using cv2.resize funcion, but it always returns a single channel image:

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

I would like the end result of x.shape to be (40, 40, 3).

Is there a more pythonic way to resize an RGB image, except that it cycles through three channels and resizes each separately?

+4
source share
1 answer

Try this code:

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)
+2
source

Source: https://habr.com/ru/post/1599750/


All Articles