<div dir="ltr">Hello. I want to caculate mutual information between 2 images. The problem is that the outcome changes only when the angle of rotation changes. What could be the problem?<div><br></div><div><div>#include "itkMutualInformationImageToImageMetric.h"</div><div>#include "itkRandomImageSource.h"</div><div>#include "itkTranslationTransform.h"</div><div>#include"itkCenteredRigid2DTransform.h"</div><div>#include "itkCenteredTransformInitializer.h"</div><div>#include "itkLinearInterpolateImageFunction.h"</div><div>#include"itkImageFileReader.h"</div><div>#include"itkNormalizedMutualInformationHistogramImageToImageMetric.h"</div><div><br></div><div><br></div><div>typedef itk::Image< unsigned char, 2>  ImageType;</div><div>//typedef itk::Image< float, 2> ImageType;</div><div><br></div><div>int main( int argc, char *argv[] )</div><div>{</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">    </span>const    unsigned int    ImageDimension = 2;</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">       </span>typedef  unsigned char    PixelType;</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>typedef itk::Image<PixelType, ImageDimension> FixedImage;</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>typedef itk::Image<PixelType, ImageDimension> MovingImage;</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">      </span>typedef itk::ImageFileReader<FixedImage> FixedImageTypeReader;</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">   </span>typedef itk::ImageFileReader<MovingImage> MovingImageTypeReader;</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>FixedImageTypeReader::Pointer fixedreader = FixedImageTypeReader::New();</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">       </span>MovingImageTypeReader::Pointer movingreader = MovingImageTypeReader::New();</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">   </span>fixedreader->SetFileName(argv[1]);</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">  </span>movingreader->SetFileName(argv[2]);</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>fixedreader->Update();</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">      </span>movingreader->Update();</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">     </span>FixedImage::Pointer fixedImage = fixedreader->GetOutput();</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">  </span>MovingImage::Pointer movingImage = movingreader->GetOutput();</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">      </span></div><div><br></div><div>  typedef itk::TranslationTransform<double, 2> TranslationTransformType; // This cannot be float for some reason?</div><div>  TranslationTransformType::Pointer transform = TranslationTransformType::New();</div><div><br></div><div>  typedef itk::CenteredRigid2DTransform<double> RigidTransformType;</div><div>  typedef itk::CenteredTransformInitializer < RigidTransformType, FixedImage, MovingImage> RigidTransformInitializerType;</div><div><br></div><div><br></div><div>  RigidTransformType::Pointer rigidtransform = RigidTransformType::New();</div><div>  RigidTransformInitializerType::Pointer initializer = RigidTransformInitializerType::New();</div><div><br></div><div>  initializer->SetTransform(rigidtransform);</div><div>  initializer->SetFixedImage(fixedreader->GetOutput());</div><div>  initializer->SetMovingImage(movingreader->GetOutput());</div><div>  initializer->GeometryOn();</div><div>  initializer->InitializeTransform();</div><div><br></div><div>  rigidtransform->SetAngleInDegrees(atof(argv[3]));</div><div><br></div><div>  RigidTransformType::OutputVectorType translation = rigidtransform->GetTranslation();</div><div>  translation[0] = atof(argv[4]);</div><div>  translation[1] = atof(argv[5]);</div><div><br></div><div>  </div><div>  </div><div>  /*typedef itk::MutualInformationImageToImageMetric<FixedImage, MovingImage >    MetricType;*/</div><div><br></div><div>  typedef itk::NormalizedMutualInformationHistogramImageToImageMetric<FixedImage, MovingImage> MetricType;</div><div><br></div><div><br></div><div>  MetricType::Pointer metric = MetricType::New();</div><div><br></div><div>  metric->SetTransform(rigidtransform);</div><div><br></div><div><br></div><div>  metric->SetFixedImageRegion(fixedImage->GetLargestPossibleRegion());</div><div>  MetricType::MeasurementVectorType lower, upper;</div><div>  lower.SetSize(2);</div><div>  lower.Fill(0);</div><div><br></div><div>  upper.SetSize(2);</div><div>  upper.Fill(255);</div><div><br></div><div>  metric->SetLowerBound(lower);</div><div>  metric->SetUpperBound(upper);</div><div>  </div><div>  MetricType::HistogramType::SizeType histogramSize;</div><div>  histogramSize.SetSize(2);</div><div>  histogramSize[0] = 128;</div><div>  histogramSize[1] = 128;</div><div><br></div><div>  metric->SetHistogramSize(histogramSize);</div><div><br></div><div>  const unsigned int numberOfParameters = rigidtransform->GetNumberOfParameters();</div><div>  typedef MetricType::ScalesType ScalesType;</div><div>  ScalesType scales(numberOfParameters);</div><div><br></div><div>  scales.Fill(1.0);</div><div><br></div><div>  metric->SetDerivativeStepLengthScales(scales);</div><div><br></div><div>  metric->SetFixedImage(fixedImage);</div><div>  metric->SetMovingImage(movingImage);</div><div><br></div><div><br></div><div>  </div><div><br></div><div>  metric->SetFixedImageRegion(fixedImage->GetLargestPossibleRegion());</div><div> </div><div><br></div><div>  itk::LinearInterpolateImageFunction<MovingImage, double>::Pointer interpolator = itk::LinearInterpolateImageFunction<MovingImage, double>::New();</div><div>  interpolator->SetInputImage(fixedImage);</div><div>  metric->SetInterpolator(interpolator);</div><div><br></div><div>  </div><div><br></div><div>  std::cout << fixedreader->GetOutput()->GetLargestPossibleRegion().GetSize();</div><div>  RigidTransformType::InputPointType m = rigidtransform->GetCenter();</div><div>  m[0] = 300;</div><div>  m[1] = 300;</div><div>  rigidtransform->SetCenter(m);</div><div>  std::cout << "parameters: " << rigidtransform->GetParameters() << std::endl;</div><div>  std::cout << "pcenter: " << m << std::endl;</div><div>  </div><div>  metric->SetTransformParameters(rigidtransform->GetParameters());</div><div>  MetricType::MeasureType value = metric->GetValue(rigidtransform->GetParameters());</div><div> </div><div>  std::cout << "Value: " << value << std::endl;</div><div><br></div><div>  return EXIT_SUCCESS;</div><div>}</div></div><div><br></div></div>