-
Notifications
You must be signed in to change notification settings - Fork 5
Mask the background out of loaded MRIs (#387) #391
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f9b73d2
Mask the background out of loaded MRIs (#387)
ebrahimebrahim c317478
Call "modified" after thresholding volume (#387)
ebrahimebrahim c9c23c8
Address integer overflow when labeling background (#387)
ebrahimebrahim 0ca336a
Move volume thresholding tools to their own module (#387)
ebrahimebrahim 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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| """Volume thresholding tools""" | ||
|
|
||
| import logging | ||
| import vtk | ||
| import slicer | ||
| from slicer import vtkMRMLScalarVolumeNode | ||
| from OpenLIFULib.util import BusyCursor | ||
| from OpenLIFULib.lazyimport import openlifu_lz | ||
|
|
||
| def cast_volume_to_float(volume_node:vtkMRMLScalarVolumeNode) -> None: | ||
| """Converts a volume node to float, replacing the underlying vtkImageData.""" | ||
| image_cast = vtk.vtkImageCast() | ||
| image_cast.SetInputData(volume_node.GetImageData()) | ||
| image_cast.SetOutputScalarTypeToDouble() | ||
| image_cast.Update() | ||
| volume_node.SetAndObserveImageData(image_cast.GetOutput()) | ||
|
|
||
| # I am not certain that the display node will know to update itself to handle the new image data type, | ||
| # so I hope poking `CreateDefaultDisplayNodes` here makes it do the right thing. If it's not needed then it's harmless anyway: | ||
| volume_node.CreateDefaultDisplayNodes() | ||
|
|
||
| def threshold_volume_by_foreground_mask(volume_node:vtkMRMLScalarVolumeNode) -> None: | ||
| """Compute the foreground mask for a loaded volume and threshold the volume to strip out the background. | ||
| This modifies the values of the background region in the volume and sets them to 1 less than the minimum value in the volume. | ||
| This way we can simply enable volume thresholding to remove | ||
| It can take a moment to actually compute the foreground mask. | ||
| """ | ||
| volume_array = slicer.util.arrayFromVolume(volume_node) | ||
| volume_array_min = volume_array.min() | ||
|
|
||
| background_value = volume_array_min - 1 | ||
| if background_value < volume_node.GetImageData().GetScalarTypeMin(): # e.g. if volume_array_min is 0 and it's an unsigned int type | ||
| logging.info("Casting volume to float for the sake of `threshold_volume_by_foreground_mask`.") | ||
| cast_volume_to_float(volume_node) | ||
|
|
||
| foreground_mask = openlifu_lz().seg.skinseg.compute_foreground_mask(volume_array) | ||
| slicer.util.arrayFromVolume(volume_node)[~foreground_mask] = background_value | ||
| volume_node.GetDisplayNode().SetThreshold(volume_array_min,volume_array.max()) | ||
| volume_node.GetDisplayNode().SetApplyThreshold(1) | ||
| volume_node.GetDisplayNode().SetAutoThreshold(0) | ||
| volume_node.Modified() | ||
|
|
||
| def load_volume_and_threshold_background(volume_filepath) -> vtkMRMLScalarVolumeNode: | ||
| """Load a volume node from file, and also set the background values to a certain value that can be threshoded out, and threshold it out.""" | ||
| volume_node = slicer.util.loadVolume(volume_filepath) | ||
| with BusyCursor(): | ||
| threshold_volume_by_foreground_mask(volume_node) | ||
| return volume_node | ||
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 how I addressed the problem you pointed out: if the value I am trying to use for background (e.g.
-1) is less than the minimum for the data type (e.g. 0 for unsigned integer types), then we cast to float before proceeding.