When upgrading to scikit-image 0.18 I found that the behaviour of dcmrtstruct2nii changed, and when debugging I added some improvements that I would like to share back.
Going from sickit image 0.17 to 0.18, they changed the method that determines if a pixel is inside a polygon for cases when the pixel is exactly on the border of the polygon. This caused the change in behaviour of dcmrtstruct2nii.
This happened consistently with dcmrtstruct2nii, even in a test where I constructed a polygon that is not exactly on the pixels. I noticed that the converter uses ITK's function TransformPhysicalPointToIndex to map the RTStruct's physical coordinates to pixel coordinates. However, this version always returns integer indices, which is why the polygon always coincides with pixel coordinates.
In my opinion the function TransformPhysicalPointToContinuousIndex is the one to use here as it allows for non-integer indices. To make sure the z coordinate is always the correct integer, I added a round as well. With this change, the result stays the same for scikit-image 0.17 and 0.18 for my tests.
When looking at the changes of scikit-image, I found that since version 0.17 it also includes a function polygon2mask, which makes the function _poly2mask superfluous. I used that function but I had to flip the shape to make it work correctly. I suspect that the shape should have been flipped anyway in the _poly2mask because of the old problem that ITK and numpy reverse the dimensions.
I did not find any tests in the repository. So before merging you may have to run your tests to make sure everything works correctly.
Thanks for this! I haven't forgotten about this PR, but it's been bussy. I'll get to testing this code as soon as possible™ and then merge your code into a new release if everything works as expected.
Hi, happy new year!
When upgrading to scikit-image 0.18 I found that the behaviour of dcmrtstruct2nii changed, and when debugging I added some improvements that I would like to share back.
TransformPhysicalPointToIndex
to map the RTStruct's physical coordinates to pixel coordinates. However, this version always returns integer indices, which is why the polygon always coincides with pixel coordinates.TransformPhysicalPointToContinuousIndex
is the one to use here as it allows for non-integer indices. To make sure thez
coordinate is always the correct integer, I added around
as well. With this change, the result stays the same for scikit-image 0.17 and 0.18 for my tests.polygon2mask
, which makes the function_poly2mask
superfluous. I used that function but I had to flip the shape to make it work correctly. I suspect that the shape should have been flipped anyway in the_poly2mask
because of the old problem that ITK and numpy reverse the dimensions.I did not find any tests in the repository. So before merging you may have to run your tests to make sure everything works correctly.