how to modify a metric in itk

because I am using v4 of itk, and I was wondering if optimizers are all compatible with v4 or if I need to change to v5 to use for instance Powell…

Powell optimizer exists in ITKv4 too. There was a big update to registration framework with introduction of ITKv4 (many classes got a v4 version, including Powell), but not much architecture changes since then.

ok, I finally update my code to v5 of itk. Then I tried to use itkAmoebaOptimizer with itkMeanSquaresImageToImageMetric. My code builds well, and executes itself without any errors, but after a first registration, when the metric is computed, I get this error message: Description: itk::ERROR: MeanSquaresImageToImageMetric(0x55a6efbcdc30): Too many samples map outside moving image buffer: 1386000 / 13823460
And I do not know how I can fix this…

Your images do not overlap enough, you should have a better initial transform e.g. by using CenteredTransformInitializer.

I try to registrate two same images. Does it mean that when I load two same images, they are not at the same position/orientation and placed randomly in the physical space?

because here is what I did. I took this example:
and replaced itkAffineTransform by itkEuler3DTransform and itkMatchCardinalityImageToImageMetric by itkMeanSquaresImageToImageMetric and I still get Description: itk::ERROR: MeanSquaresImageToImageMetric(0x562b41c12c30): Too many samples map outside moving image buffer: 0 / 13823460 after a few iterations of the registration process

You need to adjust optimizerScales when you change the transform. I think that is your problem.

ok thank you very much, I have no more execution error now. I have still one last question to ask: I upgraded itk to v5, and I try to modify the computation of itkMeanSquaresImageToImageMetric. I reached this file where I think the metric is computed and I wonder where exactly the metric is computed. Is it here: or here

It is computed in both those places. One is inside a function to compute the metric value, the other to compute both the metric value and derivative.