Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing RGB image with 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 final output of x.shape to be (40, 40, 3).

Is there a more pythonic way to resize the RGB image other than looping through the three channels and resizing each one separately?

like image 628
unicorn_poet Avatar asked Jul 26 '15 17:07

unicorn_poet


1 Answers

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)
like image 133
boardrider Avatar answered Oct 23 '22 07:10

boardrider