<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=iso-8859-1"><meta name=Generator content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
h1
        {mso-style-priority:9;
        mso-style-link:"Título 1 Car";
        margin-top:24.0pt;
        margin-right:0cm;
        margin-bottom:0cm;
        margin-left:0cm;
        margin-bottom:.0001pt;
        line-height:115%;
        page-break-after:avoid;
        font-size:14.0pt;
        font-family:"Cambria","serif";
        color:#365F91;
        mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EstiloCorreo17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
span.Ttulo1Car
        {mso-style-name:"Título 1 Car";
        mso-style-priority:9;
        mso-style-link:"Título 1";
        font-family:"Cambria","serif";
        color:#365F91;
        font-weight:bold;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:70.85pt 3.0cm 70.85pt 3.0cm;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=ES link=blue vlink=purple><div class=WordSection1><h1><span lang=EN-US style='font-family:"Calibri","sans-serif"'>Erroneous Results calculating the gradient of an image with ITK<o:p></o:p></span></h1><p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:12.0pt;font-family:"Times New Roman","serif"'>In the header file, I declare all of this types, The &#8220;Gradient ImageFilter&#8221; is for calculate the gradient of a 2D image, and the &#8220;VectorIndexSelectionCastImageFilter&#8221; is for selecting the &#8217;x&#8217; and &#8216;y&#8217; component of the gradient calculating, due to the fact that the result of computing th gradient is a vector image.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:12.0pt;font-family:"Times New Roman","serif"'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US style='color:#0070C0'>typedef double               </span><span lang=EN-US>                operatorValueType;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='color:#0070C0'>typedef double               </span><span lang=EN-US>                outputValueType;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='color:#0070C0'>typedef double               </span><span lang=EN-US>                InputPixelType;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='color:#0070C0'>typedef  </span><span lang=EN-US>itk::Image&lt;InputPixelType, 2&gt; InputImageType;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='color:#0070C0'>typedef  </span><span lang=EN-US>itk::GradientImageFilter&lt; InputImageType, operatorValueType, outputValueType&gt; GradientFilterType;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='color:#00B050'>                //for extracting a scalar from the vector image<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='color:#0070C0'>typedef double</span><span lang=EN-US>                                OutputPixelTypeImage;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='color:#0070C0'>typedef double</span><span lang=EN-US>                                ComponentType;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='color:#0070C0'>typedef  </span><span lang=EN-US>itk::CovariantVector&lt;ComponentType,2&gt; OutputPixelType;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='color:#0070C0'>typedef  </span><span lang=EN-US>itk::Image &lt;OutputPixelType, 2&gt; OutputImageType;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='color:#0070C0'>typedef  </span><span lang=EN-US>itk::VectorIndexSelectionCastImageFilter&lt;OutputImageType,InputImageType&gt; SelectionFilterType; <span style='color:#00B050'>// &lt; intputType , outputType&gt;</span><o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US style='font-size:12.0pt;font-family:"Times New Roman","serif"'>After the declarations, the main part of interest of the code is below:<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US>GradientFilterType::Pointer gradientFilter = GradientFilterType::New();<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>gradientFilter-&gt;SetInput(T_g-&gt;GetOutput());  <span style='color:#00B050'>// From T_g (is a reader) comes the image</span><o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>gradientFilter-&gt;Update();<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>                <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>SelectionFilterType::Pointer componentExtractor_x = SelectionFilterType::New();<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>SelectionFilterType::Pointer componentExtractor_y = SelectionFilterType::New();<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US>componentExtractor_x-&gt;SetIndex(0);<span style='color:#00B050'>// x component of the gradient</span><o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>componentExtractor_y-&gt;SetIndex(1);<span style='color:#00B050'>// y component of the gradient<o:p></o:p></span></span></p><p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US>componentExtractor_x-&gt;SetInput(gradientFilter-&gt;GetOutput());<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>componentExtractor_y-&gt;SetInput(gradientFilter-&gt;GetOutput());<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US>componentExtractor_x-&gt;Update();<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>componentExtractor_y-&gt;Update();<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US>It seems that everything works fine, but the problem is that when I read the image and I compare it with the calculation of the gradient in Matlab (which I assume to be correct) the results are completely different&#8230;Anyone has used before the &#8220;VectorIndexSelectionCastImageFilter&#8221; and see something strange? Or in the process of calculating the gradient?<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US>Thanks so much!<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US> </span>Antonio Gómez Barquero<o:p></o:p></p><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal><span lang=ES-TRAD style='font-size:8.0pt;mso-fareast-language:ES'>Ingeniero de Telecomunicaciones -Becario Investigador asociado a Actividades de I+D+I <o:p></o:p></span></p><p class=MsoNormal><span lang=ES-TRAD style='font-size:8.0pt;mso-fareast-language:ES'>GTTS ( Grupo de Tratamiento y Teoría de la Seńal)[ </span><span style='font-size:8.0pt;mso-fareast-language:ES'><a href="http://gtts.upct.es/"><span style='color:blue'>http://gtts.upct.es/</span></a></span><span lang=ES-TRAD style='font-size:8.0pt;mso-fareast-language:ES'>]<o:p></o:p></span></p><p class=MsoNormal><span lang=ES-TRAD style='font-size:8.0pt;mso-fareast-language:ES'>UPCT (Universidad Politécnica de Cartagena)[ </span><span style='font-size:8.0pt;mso-fareast-language:ES'><a href="http://www.upct.es/"><span style='color:blue'>http://www.upct.es/</span></a>]</span><span lang=ES-TRAD style='font-size:8.0pt;mso-fareast-language:ES'><o:p></o:p></span></p><p class=MsoNormal><o:p>&nbsp;</o:p></p></div></body></html>