Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example notebook for interfacing with Numpy #344

Open
tbirdso opened this issue Apr 11, 2022 · 6 comments
Open

Add example notebook for interfacing with Numpy #344

tbirdso opened this issue Apr 11, 2022 · 6 comments
Assignees
Labels
area:Bridge Issues affecting the Bridge module Good first issue A good issue for community members new to contributing type:Python Notebook Changes to Python Notebook examples

Comments

@tbirdso
Copy link
Contributor

tbirdso commented Apr 11, 2022

  • Demonstrate initializing a numpy array and converting to ITK image format with itk.image_from_array and itk.image_view_from_array
  • Demonstrate manipulating an ITK image as a numpy array with itk.array_from_image and itk.array_view_from_image
  • Demonstrate ITK's KJI IJK notation vs numpy's IJK KJI notation with itk.size(image) vs image.shape

EDIT: Fixed image_view_from_array and array_view_from_image

@tbirdso tbirdso added Good first issue A good issue for community members new to contributing area:Bridge Issues affecting the Bridge module labels Apr 11, 2022
@dzenanz
Copy link
Member

dzenanz commented Apr 11, 2022

I believe that ITK uses IJK and numpy KJI, not the other way around.

@Leengit
Copy link
Collaborator

Leengit commented Apr 11, 2022

Perhaps we are already thinking along these lines, but just in case ... we may want to go all verbose and say that there are two distinct cases here, depending on whether the "third dimension" represents multiple planes (Z) or time points (T), or represents multiple colors (C, such as R, G, & B).

  1. For planes Z and time points T, and sometimes for colors C, the ITK order is [X, Y, Z], [X, Y, T], [X, Y, C], where the last case is applicable if each color of the image is treated as if it were its own monochromatic image. The corresponding numpy indexing is reversed. Both ITK and numpy consider these to be 3-dimensional.
  2. For "vector pixels" such as RGB, this is a 2-dimensional image according to ITK, the ITK index order is [X, Y] and each pixel retrieved is multiple numeric values. Numpy considers this to be a 3-dimensional array and the numpy order is [Y, X, C], which differs from the previous paragraph. Furthermore, in this case, itk.image_from_array and itk.image_from_array_view require a second parameter is_vector=True.

And of course there are monochromatic 2-dimensional images.

@tbirdso
Copy link
Contributor Author

tbirdso commented Apr 11, 2022

@dzenanz Maybe my thinking is backwards. In ITK the fastest element is the first index while in numpy it is the last index. When we say "IJK" is "K" the fastest or slowest index?

Relevant discourse discussion

@Leengit
Copy link
Collaborator

Leengit commented Apr 11, 2022

In calculus (x, y, z) is usually associated with unit vectors (i, j, k) respectively. If that's the convention to stick with then ITK is [IJK].

@tbirdso
Copy link
Contributor Author

tbirdso commented Apr 11, 2022

Works for me, updated the description.

@tbirdso tbirdso added the type:Python Notebook Changes to Python Notebook examples label May 3, 2022
@tbirdso
Copy link
Contributor Author

tbirdso commented May 9, 2022

Assigned to @andinet per request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:Bridge Issues affecting the Bridge module Good first issue A good issue for community members new to contributing type:Python Notebook Changes to Python Notebook examples
Projects
None yet
Development

No branches or pull requests

4 participants