Eigen Values extraction in Python

Hey,

I’m trying to extract the eigen values of a Hessian Image using itk.SymmetricEigenAnalysisImageFilter to further implement different vesselness filters. But the problem is that i couldn’t manipulate the result from the symmetricEigenAnalysis using for an example an ImageRegionIterator. I also tried to turn the result from the filter into an array to extract the values by hand but it didn’t work as well.
So there are any filters or algorithms that i can use to extract the eigen values and also do some calculations with them?

Thanks for the attention,

Lucca

1 Like

Hi Lucca,

It would help if you show the code of what you have tried.
There are no ImageRegionIterators in python.

The output of the symmetricEigenAnalysisImageFilter is a symmetric second rank tensor image, which might need to be handled a bit differently in the python side.

Let us know more.

1 Like

Hi Pablo,

So, basically i was trying to extract the eigen values and do the Jerman’s vesselness method base in a C++ code that i’ve found. In first, my goal is to extract the eigen values and do some operation with them, and after create a vesselness image based on those results. I’ve done more research and some people say to use numpy to run over the second rank order image and catch those values, i’m trying to do this, but any results yet.

  • One more doubt, i’m also having problem to set the OrderEigenValuesBy() to magnitude order, i’m not knowing how to change this.

OBS: sorry for the format of the code, i’m a beginner user in using foruns for code support.

Thanks for the quick answer.

Lucca

The code :

 eigenCalculator = itk.SymmetricEigenAnalysisImageFilter[ImageType].New(img)
 eigenCalculator.OrderEigenValuesBy()
 eigenCalculator.SetDimension(3)
 eigenCalculator.Update()
 image = eigenCalculator.GetOutput()

    
  ImageCalculatorFilterType = itk.MinimumMaximumImageCalculator[ImageType, ImageType];
  imageCalculatorFilter = ImageCalculatorFilterType.New()
  imageCalculatorFilter.SetImage(img)
  imageCalculatorFilter.ComputeMaximum()
  maxLambda3 = imageCalculatorFilter.GetMaximum()
  Tau = tau
  lambdaRho = maxLambda3 * Tau
    
  ConstIteratorType = itk.ImageRegionConstIterator[ImageType]
  IteratorType = itk.ImageRegionIterator[ImageType]
   
    
  it = ConstIteratorType.it(eigenImage, eigenImage.GetLargestRegion())
  out = IteratorType.out(inputImage, inputImage.GetLargestRegion())
    
   it.GoToBegin();
   out.GoToBegin();
  while( it.IsAtEnd() == False ):
      lambda2 = it.Get()[1]
       lambda3 = it.Get()[2]
       if( lambda3 > maxLambda3 * Tau):
           lambdaRho = lambda3;
       else :
           if( lambda3 > 0 ):
               lambdaRho = maxLambda3 * Tau;
           else:
               lambdaRho = 0;
       if( lambda2 >= lambdaRho /2.0 and lambdaRho > 0.0) :
           out.Set(1.0)
           ++out
           ++it
           continue;
       '''// Jerman's ratio
   // lambda2^2 * ( lambdaP - lambda2 ) * [3/(lambdaP + lambda2)]^3'''
       vesselnessMeasure = lambda2* lambda2 * (lambdaRho - lambda2); #lambda2^2 * ( lambdaP - lambda2 )
       vesselnessMeasure *= 27 / ( (lambda2 + lambdaRho) * (lambda2 + lambdaRho) * (lambda2 + lambdaRho) ); #[3/(lambdaP + lambda2)]^3
                            
      out.Set(vesselnessMeasure);   
       ++out;
       ++it;
          
   OutputPixelType = itk.UC
   OutputImageType = itk.Image[OutputPixelType, 3]

   rescale_filter = itk.RescaleIntensityImageFilter[ImageType, OutputImageType].New()
   rescale_filter.SetInput(inputImage)
   rescale_filter.Update()
   image = rescale_filter.GetOutput()

Here is a guide for code formatting on discourse forums:

1 Like

Thanks Dzenan!!

Thanks for formatting it, way easier to read!
However, this is a complete mixture of python and randomly pythonized c++.

The input for the SymmetricEigenAnalysisImageFilter is an image with a second rank tensor as pixels. You haven’t showed how you have computed the hessian. This class might help.

Also for future reference, have a look at the test file of itkSymmetricEigenAnalysisImageFilter for inspiration in your hacking managing that filter.

After you have finished your itk pipeline, and you have the EigenAnalysis image, you can try to convert it to a numpy array with np_img = itk.GetArrayFromImage(itk_eigen_analysis_image), and deal with particular pixels (tensors) in python, and apply extra algorithm from there. The python wrappings of ITK don’t expose iterators for performance reasons.

Hope it helps. I would recommend you explore things a little bit first, and come with questions supported by a minimal set of code that you have tried to run and thought about it.

3 Likes

Thank you very much Pablo!

1 Like

Dear @juccapirama , I encountered exactly the same question as you. Have you found a good solution? Or having a working code to share?

Many thanks,
Jianxu