Page MenuHomePhabricator

CastToMITK / CastToITK changes the geometry sometimes
Closed, ResolvedPublic

Description

  • Load an image with a rotated geometry (e.g. T:\Temo\Extracted2DMitralValve.NRRD)
  • Cast it to ITK with CastToItk
  • Cast it back to MITK with CastToMitk
  • Store it in Dataset

--> You will see, the geometry changed!

Matrix: (Original)
0.100648 0 0
-0.00294193 -0.0983475 0
0 0 1

Matrix: (Casted)
0.100691 0 0
0 0.0983475 0
0 0 1

Not good.

Event Timeline

Handling of Rotation in Image Geometries is not functionable.. need to fix that first: See T8477

I was wrong! Can be solved without T8477!

The problem only occurs, when you have a 2D image.
Generally 2D mitk images can be rotated around every axis in space. This is possible, because even though they are 2D, they have a 3x3 TransformMatrix, that can express this kind of rotation (and MITK provides interactions to fill that matrix respectivly).

2D ITK Images cannot do that! Their member matrix m_Direction, which defines their rotation, is only as big as their dimension. So for 2D it is a 2x2 Matrix. With that matrix you can only describe 2D Rotation.

So, when you cast an mitk image to an itk image AND the itk image is 2D AND you have a rotation in the matrix, you might lose information! When you call castToItk() the following happens:

In mitkImageToItk.txx in function
void mitk::ImageToItk<TOutputImage>::GenerateOutputInformation()
in line 203 and following:
The indexToWorld transform matrix (which contains spacing and rotation) of the mitk image is taken and the rotation matrix is extracted. That matrix is 3x3. If the itk Image holds a 2x2 as m_direction matrix, MITK gives up and just puts the identy matrix in the direction matrix of the itk image.

What it rather should do:

  • If the mitk rotation matrix looks like this: a b 0 c d 0 0 0 1

Copy the entries for a,b,c,d in to the 2x2 itk matrix! No information will be lost. If it looks different, do the same, but additionally give out a warning, that information has been lost, since ITK does not support that kind of rotation for 2D images

Changed the code as described. But the warning is coming to the output a few times, when loading an rotated image, which should actually be allright.

This happens because.. when you load an image, mitk::ImageMapperGL2D::SetDefaultProperties(...) is called. This method is apparently a test, to see if your image is binary (still it does not seem to work properly, since the if-statement is wrong. Anyway, it is extracting a slice with the mitk::ImageSliceSelector, which seems to be mirrored for some unknown reason.
That mirrored Mitk Image is then casted to itk and used for image statistics calcuation. The cast to ITK is causing the warning message to appear, since the "mirroring" is lost.
The Image is afterwards not used anymore, so being mirrored or not does not change anything.

Yet, we need to check if the fact, why ImageSliceSelector is (sometimes?) mirroring the images. I mailed Ivo about it, maybe he knows more.

We fixed it now partly.

THE PROBLEM:
Load a 2D Image in MITK, rotate it, cast to itk, cast back to mitk: The rotation might be lost.

THE REASON:
MITK provides a 3x3 geometry matrix even for 2D images, while ITK (and btw most file formats like nrrd & co) have a 2x2 matrix for 2D images. So information might be lost when casting from MITK to ITK and back again.

PREVIOUS BEHAVIOUR:
When a 2D MITK image is casted to a 2D ITK Image, all rotation would be lost.

THE NOW IMPLEMENTED SOLUTION:
When a 2D MITK image is casted to a 2D ITK image, rotation is conserved in SOME cases. When you the MITK image is ONLY rotated around the transversal plane normal, it is conserved! You can cast forth and back, as you want. If any other rotation is added to that, the ITK image will be without any rotation, just spacing.

POSSIBLE WORKAROUND:
Cast the rotated MITK image to ITK. Do your thing, filter, whatever. Cast it back to MITK and set manually a new geometry, the geometry of the initial image. That implies that the ITK filter does work without rotation information.

In Future we might only support 3D image, which would handling geometries much easier. See T9012

Sounds ok. Two things you mention on the wiki page are really imporant:

  • we should not attempt to guess the best 2x2 matrix for any 3x3 matrix, but only calculate the 2x2 matrix if there was only a in-plane transformation
  • you should write a good unit test for this because it is a central and delicate feature which could easily break

I will create a test for it and then push it

Test implemented for Casting 3D ITK to MITK...

need to extend Test for 2D ITK to MITK and MITK to 2D/3D ITK.

4D as well.

Will do that next Bugsquashing.

[38d851]: Merge branch 'bug-8509-CastChangesGeometry'

Merged commits:

2011-09-28 15:35:06 Bastian Graser [3e5563]
Extended Geometry3D Test. Now testing that no rotation information is lost when casting from ITK to MITK and MITK to ITK.


2011-08-10 16:05:49 Bastian Graser [96103c]
Keep rotation for 2D images, if possible.

[375035]: Merge branch 'bug-8509-NewCast'

Merged commits:

2011-10-17 17:49:38 Bastian Graser [174933]
fixed if statement parenthesis

[444013]: Merge branch 'bug-8509-NewCast2'

Merged commits:

2011-10-18 16:39:22 Bastian Graser [edcfa4]
fixed parenthesis for sure