-
Notifications
You must be signed in to change notification settings - Fork 1
Add reverse_coords option to point conversion functions #15
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,16 +62,34 @@ def image_from_image_layer(image_layer): | |
| return image | ||
|
|
||
|
|
||
| def points_layer_from_point_set(point_set): | ||
| """Convert an itk.PointSet to a napari.layers.Points.""" | ||
| def points_layer_from_point_set(point_set, reverse_coords=True): | ||
| """Convert an itk.PointSet to a napari.layers.Points. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| point_set : itk.PointSet | ||
| The ITK PointSet to convert. | ||
| reverse_coords : bool, optional | ||
| If True (default), reverse the coordinate order from ITK's x,y,z | ||
| to napari's z,y,x order. | ||
|
|
||
| Returns | ||
| ------- | ||
| napari.layers.Points | ||
| The converted napari Points layer. | ||
| """ | ||
| # Get points as numpy array | ||
| number_of_points = point_set.GetNumberOfPoints() | ||
|
|
||
| if number_of_points == 0: | ||
| data = np.array([]).reshape(0, 3) # Default to 3D empty array | ||
| else: | ||
| points_array = itk.array_from_vector_container(point_set.GetPoints()) | ||
| data = points_array | ||
| if reverse_coords: | ||
| # Reverse coordinate order from ITK (x,y,z) to napari (z,y,x) | ||
| data = points_array[:, ::-1] | ||
| else: | ||
| data = points_array | ||
|
|
||
| # Get point data (features) if available | ||
| point_data = point_set.GetPointData() | ||
|
|
@@ -92,8 +110,22 @@ def points_layer_from_point_set(point_set): | |
| return points_layer | ||
|
|
||
|
|
||
| def point_set_from_points_layer(points_layer): | ||
| """Convert a napari.layers.Points to an itk.PointSet.""" | ||
| def point_set_from_points_layer(points_layer, reverse_coords=True): | ||
| """Convert a napari.layers.Points to an itk.PointSet. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| points_layer : napari.layers.Points | ||
| The napari Points layer to convert. | ||
| reverse_coords : bool, optional | ||
| If True (default), reverse the coordinate order from napari's z,y,x | ||
| to ITK's x,y,z order. | ||
|
|
||
| Returns | ||
| ------- | ||
| itk.PointSet | ||
| The converted ITK PointSet. | ||
| """ | ||
| # Apply transformations (rotate, scale, translate) to points | ||
| data = points_layer.data.copy() # Make a copy to avoid modifying original | ||
|
|
||
|
|
@@ -122,6 +154,9 @@ def point_set_from_points_layer(points_layer): | |
|
|
||
| # Set points | ||
| if len(data) > 0: | ||
| if reverse_coords: | ||
| # Reverse coordinate order from napari (z,y,x) to ITK (x,y,z) | ||
| data = data[:, ::-1] | ||
|
Comment on lines
+157
to
+159
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see, the other "main" part of the fix 😃 |
||
| points = itk.vector_container_from_array(data.astype(np.float32).flatten()) | ||
| point_set.SetPoints(points) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the main part of the fix, right? Cool! I can't wait to try it out, with SuperElastix/elastix-napari#65 😃