<div dir="ltr"><div><div><div><div><div>Hello Constantinus,<br><br></div>You need to initialize the metric first.<br><br></div>You can add:<br>      metric->Initialize();<br></div>before calling your "test()" function.<br><br></div>Hope this helps,<br></div>Francois<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 11, 2017 at 9:07 AM, Constantinus Spanakis <span dir="ltr"><<a href="mailto:c.spanakis83@gmail.com" target="_blank">c.spanakis83@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello. I want to compute the Normalized Mutual information of two images when I give a specific transformation. Below is my code.<div><br></div><div><div>#include "<wbr>itkMutualInformationImageToIma<wbr>geMetric.h"</div><div>#include "itkRandomImageSource.h"</div><div>#include "itkTranslationTransform.h"</div><div>#include"<wbr>itkCenteredRigid2DTransform.h"</div><div>#include "<wbr>itkCenteredTransformInitialize<wbr>r.h"</div><div>#include "<wbr>itkLinearInterpolateImageFunct<wbr>ion.h"</div><div>#include"itkImageFileReader.h"</div><div>#include"<wbr>itkNormalizedMutualInformation<wbr>HistogramImageToImageMetric.h"</div><div>#include "<wbr>itkMattesMutualInformationImag<wbr>eToImageMetric.h"</div><div>#include "<wbr>itkMutualInformationImageToIma<wbr>geMetric.h"</div><div><br></div><div># include <cstdlib></div><div># include <iostream></div><div># include <iomanip></div><div># include <fstream></div><div># include <iomanip></div><div># include <cmath></div><div># include <ctime></div><div># include <cstring></div><div># include <random></div><div><br></div><div><br></div><div><br></div><div><br></div><div>using namespace std;</div><div><br></div><div><br></div><div><br></div><div>int main(int argc, char *argv[]);</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div>const    unsigned int    ImageDimension = 2;</div><div>typedef  unsigned char    PixelType;</div><div>typedef itk::Image<PixelType, ImageDimension> FixedImage;</div><div>typedef itk::Image<PixelType, ImageDimension> MovingImage;</div><div><br></div><div>typedef itk::ImageFileReader<<wbr>FixedImage> FixedImageTypeReader;</div><div>typedef itk::ImageFileReader<<wbr>MovingImage> MovingImageTypeReader;</div><div><br></div><div>typedef itk::CenteredRigid2DTransform<<wbr>double> RigidTransformType;</div><div><br></div><div><br></div><div>typedef itk::<wbr>NormalizedMutualInformationHis<wbr>togramImageToImageMetric<<wbr>FixedImage, MovingImage> MetricType;</div><div>//typedef itk::<wbr>MattesMutualInformationImageTo<wbr>ImageMetric<FixedImage, MovingImage> MetricType;</div><div>//typedef itk::<wbr>MutualInformationImageToImageM<wbr>etric<FixedImage, MovingImage> MetricType;</div><div><br></div><div><br></div><div><br></div><div>typedef itk::<wbr>CenteredTransformInitializer<<wbr>RigidTransformType, FixedImage, MovingImage> InitializerType;</div><div><br></div><div>RigidTransformType::Pointer rigidtransform;</div><div><br></div><div><br></div><div><br></div><div><br></div><div>MetricType::MeasureType test(double *arg , MetricType::Pointer metric){</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">       </span>MetricType::MeasureType v=0;</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>int i;</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">       </span>double a, e, f;</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">      </span>a = arg[0] ;</div><div><br></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>e = arg[1];</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">  </span>f = arg[2];</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">  </span></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>rigidtransform->SetAngle(a);</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">      </span>RigidTransformType::<wbr>OutputVectorType t = rigidtransform-><wbr>GetTranslation();</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">  </span>t[0] = e;</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">    </span>t[1] = f;</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">    </span>rigidtransform-><wbr>SetTranslation(t);</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>RigidTransformType::<wbr>ParametersType p;</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>p.SetSize(5);</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>p[0] = a;</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">    </span>p[1] = 255.5;</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>p[2] = 255.5;</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>p[3] = e;</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">    </span>p[4] = f;</div><div><br></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">   </span></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>metric->UseAllPixelsOn();</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>metric->SetTransform(<wbr>rigidtransform);</div><div><br></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>cout<<p<<endl;</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">   </span>return metric->GetValue(p);</div><div>}</div><div><br></div><div><br></div><div><br></div><div><br></div><div>int main( int argc, char *argv[] )</div><div>{</div><div><br></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap"> </span></div><div><br></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">    </span>FixedImageTypeReader::Pointer fixedreader = FixedImageTypeReader::New();</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>MovingImageTypeReader::Pointer movingreader = MovingImageTypeReader::New();</div><div><br></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>fixedreader->SetFileName(argv[<wbr>1]);</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>movingreader->SetFileName(<wbr>argv[2]);</div><div><br></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">   </span>fixedreader->Update();</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">    </span>movingreader->Update();</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">   </span>FixedImage::Pointer fixedImage = fixedreader->GetOutput();</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>MovingImage::Pointer movingImage = movingreader->GetOutput();</div><div><br></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">    </span></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>MetricType::Pointer metric;</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">  </span>rigidtransform = RigidTransformType::New();</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">  </span></div><div>  </div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">  </span>MetricType::HistogramSizeType h;</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>h.SetSize(2);</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>h[0] = h[1]=64;</div><div><br></div><div><br></div><div><br></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">   </span>metric = MetricType::New();</div><div>  </div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">       </span>metric->SetTransform(<wbr>rigidtransform);</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">  </span>metric->SetHistogramSize(h);</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">      </span>metric-><wbr>SetNumberOfFixedImageSamples(<wbr>10000);</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>metric-><wbr>SetNumberOfSpatialSamples(<wbr>10000);</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">   </span>metric->SetFixedImage(<wbr>fixedImage);</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>metric->SetMovingImage(<wbr>movingImage);</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">   </span>metric->SetFixedImageRegion(<wbr>fixedImage-><wbr>GetLargestPossibleRegion());</div><div><br></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">    </span>const unsigned int numberOfParameters = rigidtransform-><wbr>GetNumberOfParameters();</div><div>  </div><div>  </div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap"> </span></div><div>  </div><div><br></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap"> </span></div><div>  </div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">  </span>std::cout<<"Spacing: "<<movingImage->GetSpacing()<<<wbr>", Origin: "<<movingImage->GetOrigin()<<"<wbr>, Direction: "<<movingImage->GetDirection() <<std::endl;</div><div>  </div><div> </div><div><br></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>itk::<wbr>LinearInterpolateImageFunction<wbr><MovingImage, double>::Pointer interpolator = itk::<wbr>LinearInterpolateImageFunction<wbr><MovingImage, double>::New();</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">    </span>interpolator->SetInputImage(<wbr>movingImage);</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">      </span>metric->SetInterpolator(<wbr>interpolator);</div><div><br></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>InitializerType::Pointer initializer = InitializerType::New();</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">       </span>initializer->SetTransform(<wbr>rigidtransform);</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>initializer->SetFixedImage(<wbr>fixedreader->GetOutput());</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">       </span>initializer->SetMovingImage(<wbr>movingreader->GetOutput());</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>initializer->GeometryOn();</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>initializer-><wbr>InitializeTransform();</div><div><br></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">   </span>std::cout << fixedreader->GetOutput()-><wbr>GetLargestPossibleRegion().<wbr>GetSize();</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">       </span>RigidTransformType::<wbr>InputPointType mc = rigidtransform->GetCenter();</div><div>  </div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">        </span></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>MovingImage::SizeType c = movingreader->GetOutput()-><wbr>GetLargestPossibleRegion().<wbr>GetSize();</div><div><br></div><div><br></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>rigidtransform->SetCenter(mc);</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">    </span>std::cout << "parameters: " << rigidtransform->GetParameters(<wbr>) << std::endl;</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">       </span>std::cout << "pcenter: " << mc << std::endl;</div><div>  </div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">  </span>double p[3];</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>p[0] = 0.52;</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>p[1] = -138;</div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap"> </span>p[2] = 52;</div><div> </div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">  </span>std::cout << "Value: " << test(p,metric) << std::endl;</div><div><br></div><div><br></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap"> </span></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">     </span></div><div><span class="m_7253365210291797430gmail-Apple-tab-span" style="white-space:pre-wrap">     </span>return EXIT_SUCCESS;</div><div>}</div></div><div><br></div><div>Whenever I execute the code, I get a strange message (see attached image). What could be the problem?</div></div>
<br>______________________________<wbr>_________________<br>
Community mailing list<br>
<a href="mailto:Community@itk.org">Community@itk.org</a><br>
<a href="http://public.kitware.com/mailman/listinfo/community" rel="noreferrer" target="_blank">http://public.kitware.com/<wbr>mailman/listinfo/community</a><br>
<br></blockquote></div><br></div>