InsightSoftwareConsortium / ITK

Insight Toolkit (ITK) -- Official Repository. ITK builds on a proven, spatially-oriented architecture for processing, segmentation, and registration of scientific images in two, three, or more dimensions.
https://itk.org
Apache License 2.0
1.37k stars 660 forks source link

Inconsistent conversion of VectorImage to numpy #4646

Closed ncullen93 closed 1 month ago

ncullen93 commented 2 months ago

In itk Python, if I have a VectorImage of size (10,10) with 2 channels, then converting it to numpy gives me an array of shape (10,10,2). But if I have a VectorImage with 1 channel, it squeezes that channel and gives me a numpy array of (10,10). Shouldn't the expected behavior be (10,10,1). Or is there a way to ensure this?

import itk
import numpy as np

arr = np.ones((10,10,2))
img = itk.image_from_array(arr, is_vector=True)
arr2 = itk.array_from_image(img)
print(arr2.shape) # (10,10,2)

arr = np.ones((10,10,1))
img = itk.image_from_array(arr, is_vector=True)
arr2 = itk.array_from_image(img)
print(arr2.shape) # (10,10)
github-actions[bot] commented 2 months ago

Thank you for contributing an issue! 🙏

Welcome to the ITK community! 🤗👋☀️

We are glad you are here and appreciate your contribution. Please keep in mind our community participation guidelines. 📜 Also, please check existing open issues and consider discussion on the ITK Discourse. 📖

This is an automatic message. Allow for time for the ITK community to be able to read the issue and comment on it.

thewtex commented 1 month ago

Hi, thanks for the nice report! Patch is here: #4648