[ITK] [ITK-users] ImageToImageMetric in Python
Matt McCormick
matt.mccormick at kitware.com
Tue Apr 15 23:19:29 EDT 2014
Hi Sven,
Glad to hear it worked out well.
What exception is created and why is it occurring?
Thanks,
Matt
On Tue, Apr 15, 2014 at 7:18 AM, Prevrhal, Sven
<Sven.Prevrhal at philips.com> wrote:
> Yes it did, great! Thanks a lot! I went by the examples I found on the web, and they never called Initialize(). So, for documentation, below is what worked for me.
>
> One question remains, and that is how to wrap this in a loop in which the moving image changes. I tried only repeating the SetMovingImage call, but the result did not change. Then I tried adding the Initialize call to the loop after SetMovingImage, which caused an Exception. The only way I got it to work was to place all New()'s etc inside the loop as well. Is there a better way?
>
> Kind regards,
> Sven
>
> # ------ Python code that works for me to compute NCC between two images, both of type itk.Image.F3
> metricType = itk.NormalizedCorrelationImageToImageMetric.IF3IF3
> transformType = itk.IdentityTransform.D3
> interpolatorType = itk.NearestNeighborInterpolateImageFunction.IF3D
>
> metric = metricType.New()
> tr = transformType.New()
> ip = interpolatorType.New()
>
> metric.SetTransform(tr)
> metric.SetInterpolator(ip)
> metric.SetFixedImage(fixedImage)
> metric.SetFixedImageRegion( fixedImage.GetBufferedRegion() )
> metric.SetMovingImage(movingImage)
> metric.Initialize()
>
> zeroDisplacement = tr.GetParameters() # could also set to itk.OptimizerParameters.D(3)
> zeroDisplacement.Fill(0)
>
> v = metric.GetValue(zeroDisplacement)
> print 'NCC = %f' % v
>
> -----Original Message-----
> From: Matt McCormick [mailto:matt.mccormick at kitware.com]
> Sent: Montag, 14. April 2014 22:32
> To: Prevrhal, Sven
> Cc: Jagjeet Nain; insight-users at itk.org
> Subject: Re: [ITK] [ITK-users] ImageToImageMetric in Python
>
> Hi Sven,
>
> Does calling Initialize() [1] help?
>
> Thanks,
> Matt
>
> [1] http://www.itk.org/Doxygen/html/classitk_1_1ImageToImageMetric.html#a2bd5955a3bedf31e701aed3ee1d4e473
>
>
> On Mon, Apr 14, 2014 at 12:25 PM, Prevrhal, Sven <Sven.Prevrhal at philips.com> wrote:
>> Thanks Matt and Jagjeet,
>> It's better but still not working correctly. Here's what I have now:
>>
>>
>>
>> metricType = itk.NormalizedCorrelationImageToImageMetric.IF3IF3
>> transformType = itk.TranslationTransform.D3
>> interpolatorType = itk.LinearInterpolateImageFunction.IF3D
>> fixedImage = files['T2']['itkImage'] # I checked, data are
>> fine
>>
>> metric = metricType.New()
>> tr = transformType.New()
>> ip = interpolatorType.New()
>>
>> metric.SetTransform(tr)
>> tr.SetIdentity()
>> metric.SetInterpolator(ip)
>> metric.SetFixedImage(fixedImage)
>>
>> # metric.GetFixedImageRegion() is [0,0,0], [0,0,0] without a line like:
>> #metric.SetFixedImageRegion( fixedImage.GetBufferedRegion() )
>> metric.SetFixedImageRegion(
>> itk.ImageRegion._3([0,0,0],[10,10,10]) )
>>
>> zeroDisplacement = itk.OptimizerParameters.D(3)
>> for i,v in enumerate([0,0,0]):
>> zeroDisplacement.SetElement(i,v)
>>
>> movingImage = files[typ]['itkImage'] # data are fine and different from fixedImage
>> metric.SetMovingImage(movingImage)
>> v = metric.GetValue(zeroDisplacement)
>> print 'NCC T2 - %s: %f' % (typ, v)
>>
>>
>>
>> The code crashes without error message at the GetValue call. If I drop the SetFixedImageRegion, it does run but produces v=0.0 no matter what. Any more insight would be greatly appreciated.
>>
>> Kind regards,
>> Sven
>>
>> -----Original Message-----
>> From: Matt McCormick [mailto:matt.mccormick at kitware.com]
>> Sent: Dienstag, 8. April 2014 20:10
>> To: Prevrhal, Sven
>> Cc: insight-users at itk.org
>> Subject: Re: [ITK] [ITK-users] ImageToImageMetric in Python
>>
>> Hi Sven,
>>
>> To find the type of MetricType::TransformParametersType, look at the Doxygen documentation for NormalizedCorrelationImageToImageMetric [1].
>> Following the typedef trail [2], the type is OptimizerParameters [3],
>> which can be created with
>>
>> displacement = itk.OptimizerParameters.F( Dimension )
>>
>> Hope this helps,
>> Matt
>>
>> [1]
>> http://www.itk.org/Doxygen/html/classitk_1_1NormalizedCorrelationImage
>> ToImageMetric.html
>>
>> [2]
>> http://www.itk.org/Doxygen/html/classitk_1_1TransformBaseTemplate.html
>>
>> [3]
>> http://www.itk.org/Doxygen/html/classitk_1_1OptimizerParameters.html
>>
>> On Tue, Apr 8, 2014 at 1:34 PM, Prevrhal, Sven <Sven.Prevrhal at philips.com> wrote:
>>> I am looking at the itk Software Guide to figure out how to implement
>>> this in Python. I have:
>>>
>>>
>>>
>>> nccMetric =
>>> itk.NormalizedCorrelationImageToImageMetric.IF3IF3.New()
>>>
>>> nccMetric.SetTransform(itk.IdentityTransform.D3.New())
>>>
>>>
>>> nccMetric.SetInterpolator(itk.NearestNeighborInterpolateImageFunction.
>>> IF3D.New())
>>>
>>> nccMetric.SetFixedImage(fixedImage)
>>>
>>> nccMetric.SetMovingImage(movingImage)
>>>
>>>
>>>
>>> which I believe works fine. I am getting stumped by the translation
>>> of this to Python:
>>>
>>>
>>>
>>> # ITK software guide page 390
>>>
>>> # C++ code: MetricType::TransformParametersType displacement(
>>> Dimension );
>>>
>>> displacement = nccMetric.??
>>>
>>>
>>>
>>> After which
>>>
>>>
>>>
>>> value = nccMetric.GetValue( displacement )
>>>
>>>
>>>
>>> should work fine. Anybody knows this?
>>>
>>>
>>>
>>> Regards,
>>>
>>> Sven
>>>
>>>
>>>
>>> Sven Prevrhal
>>>
>>> Philips Research
>>>
>>>
>>>
>>>
>>> ________________________________
>>> The information contained in this message may be confidential and
>>> legally protected under applicable law. The message is intended
>>> solely for the addressee(s). If you are not the intended recipient,
>>> you are hereby notified that any use, forwarding, dissemination, or
>>> reproduction of this message is strictly prohibited and may be
>>> unlawful. If you are not the intended recipient, please contact the
>>> sender by return e-mail and destroy all copies of the original message.
>>>
>>> _____________________________________
>>> Powered by www.kitware.com
>>>
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>>
>>> Kitware offers ITK Training Courses, for more information visit:
>>> http://www.kitware.com/products/protraining.php
>>>
>>> Please keep messages on-topic and check the ITK FAQ at:
>>> http://www.itk.org/Wiki/ITK_FAQ
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://www.itk.org/mailman/listinfo/insight-users
>>>
>>> _______________________________________________
>>> Community mailing list
>>> Community at itk.org
>>> http://public.kitware.com/cgi-bin/mailman/listinfo/community
>>>
>>
>> ________________________________
>> The information contained in this message may be confidential and legally protected under applicable law. The message is intended solely for the addressee(s). If you are not the intended recipient, you are hereby notified that any use, forwarding, dissemination, or reproduction of this message is strictly prohibited and may be unlawful. If you are not the intended recipient, please contact the sender by return e-mail and destroy all copies of the original message.
>>
_____________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php
Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ
Follow this link to subscribe/unsubscribe:
http://www.itk.org/mailman/listinfo/insight-users
More information about the Community
mailing list