Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize 3D stack of images in Python

I have a 3D numpy array, which is a stack of 100 300x300 images. I want to resize all the images in the stack to be 200x200. I tried to use the numpy resize function:

import numpy as np

img_stack_sm = np.resize(img_stack, (100, 200, 200))

...but doing so scrambles the images (as shown by plotting). How can this be done in one pass? Thank you.

like image 464
Chris Parry Avatar asked Sep 14 '26 23:09

Chris Parry


1 Answers

I just used a for loop in the end and cv2:

import cv2

width = 200
height = 200
img_stack_sm = np.zeros((len(img_stack), width, height))

for idx in range(len(img_stack)):
    img = img_stack[idx, :, :]
    img_sm = cv2.resize(img, (width, height), interpolation=cv2.INTER_CUBIC)
    img_stack_sm[idx, :, :] = img_sm
like image 82
Chris Parry Avatar answered Sep 17 '26 12:09

Chris Parry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!