Hi Lassi,<br><br>First of all thanks for your help.<br><br>Indeed this filter does perfectly what I want, and even in a better way than the one I was thinking to. Nevertheless, I would like to understand the mistake I have done in my code, it would probably help me and others in future works.<br>
<br>Regards,<br>Loïc<br><br><div class="gmail_quote">2010/5/10 Lassi Paavolainen <span dir="ltr">&lt;<a href="mailto:lassi.paavolainen@jyu.fi">lassi.paavolainen@jyu.fi</a>&gt;</span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi Loic,<br>
<br>
You might want to have a look at following Insight Journal paper that should do exactly what you plan to do:<br>
<a href="http://insight-journal.org/browse/publication/702" target="_blank">http://insight-journal.org/browse/publication/702</a><br>
<br>
Best regards,<br><font color="#888888">
Lassi</font><div><div></div><div class="h5"><br>
<br>
On Mon, 10 May 2010, Loïc Giraldi wrote:<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi all!<br>
<br>
I&#39;m trying to make an adaptive thresholding using the Otsu method. To do so,<br>
I want to process my image by region. The idea is to separate the original<br>
picture in several blocks, process each block and reconstruct the final<br>
output by gathering the blocks.<br>
I am trying to do it using the RegionOfInterestImageFilter, the<br>
OtsuThresholdImageFilter and the PasteImageFilter in a loop over regions, I<br>
have grabbed some informations in this mailing list but I can&#39;t manage to<br>
make it working. The process works if I do not loop over regions.<br>
<br>
Here is my code:<br>
<br>
#include &quot;itkImage.h&quot;<br>
#include &quot;itkImageFileReader.h&quot;<br>
#include &quot;itkImageFileWriter.h&quot;<br>
<br>
#include &quot;itkRegionOfInterestImageFilter.h&quot;<br>
#include &quot;itkOtsuThresholdImageFilter.h&quot;<br>
#include &quot;itkPasteImageFilter.h&quot;<br>
<br>
#include &lt;iostream&gt;<br>
#include &lt;string&gt;<br>
#include &lt;sstream&gt;<br>
#include &lt;cstdlib&gt;<br>
using namespace std;<br>
<br>
<br>
template &lt;typename T&gt; string toString(T val){<br>
    ostringstream oss;<br>
    oss &lt;&lt; val;<br>
    return oss.str();<br>
}<br>
<br>
int main(int argc, char *argv[]){<br>
    ////////////////////////////  Usage //////////////////////////////<br>
    if (argc&lt;3){<br>
        std::cerr &lt;&lt; &quot;Missing arguments!&quot; &lt;&lt; std::endl;<br>
        std::cerr &lt;&lt; &quot;Usage : &quot; &lt;&lt; argv[0]<br>
                  &lt;&lt; &quot; InputFile&quot;<br>
                  &lt;&lt; &quot; Size&quot; &lt;&lt; std::endl;<br>
        return 1;<br>
    }<br>
       <br>
    const char *inputFileName = argv[1];<br>
    const unsigned int size = atoi(argv[2]);<br>
   <br>
    //////////////////////  Image definitions ////////////////////////<br>
    const int Dimension = 3;<br>
<br>
    typedef unsigned short InputPixelType;<br>
    typedef unsigned char OutputPixelType;<br>
<br>
    typedef itk::Image&lt; InputPixelType, Dimension &gt; InputImageType;<br>
    typedef itk::Image&lt; OutputPixelType, Dimension &gt; OutputImageType;<br>
<br>
    ////////////////////////  Image reading //////////////////////////<br>
    typedef itk::ImageFileReader&lt; InputImageType &gt; ReaderType;<br>
   <br>
    ReaderType::Pointer reader = ReaderType::New();<br>
    reader-&gt;SetFileName(inputFileName);<br>
    try{<br>
        reader-&gt;Update();<br>
    }<br>
    catch( itk::ExceptionObject &amp; excep ){<br>
        std::cerr &lt;&lt; &quot;Exception caught !&quot; &lt;&lt; std::endl;<br>
        std::cerr &lt;&lt; excep &lt;&lt; std::endl;<br>
    }<br>
   <br>
    //////////////////////////  Filtering ////////////////////////////<br>
    //Output Allocation<br>
    OutputImageType::Pointer output = OutputImageType::New();<br>
    OutputImageType::IndexType outIndex;<br>
    OutputImageType::SizeType outSize;<br>
    OutputImageType::RegionType outRegion;<br>
<br>
    for (int i = 0; i &lt; Dimension; ++i){<br>
        outIndex[i]=reader-&gt;GetOutput()-&gt;GetBufferedRegion().GetIndex()[i];<br>
        outSize[i]=reader-&gt;GetOutput()-&gt;GetBufferedRegion().GetSize()[i];<br>
    }<br>
<br>
    outRegion.SetIndex(outIndex);<br>
    outRegion.SetSize(outSize);<br>
   <br>
    output-&gt;SetRegions(outRegion);<br>
    output-&gt;Allocate();<br>
    output-&gt;FillBuffer(0);<br>
   <br>
    //Moving box definition<br>
    InputImageType::IndexType boxIndex;<br>
    InputImageType::SizeType boxSize;<br>
    InputImageType::RegionType boxRegion;<br>
<br>
    for (int i = 0; i &lt; Dimension; ++i){<br>
        boxIndex[i] = outIndex[i];<br>
    }<br>
    for (int i = 0; i &lt; Dimension; ++i){<br>
        boxSize[i] = size;<br>
    }<br>
   <br>
    boxRegion.SetIndex(boxIndex);<br>
    boxRegion.SetSize(boxSize);<br>
<br>
    unsigned numberOfBoxes[3];<br>
    for (int i = 0; i &lt; Dimension; ++i){<br>
        numberOfBoxes[i] = static_cast&lt;unsigned&gt;((outSize[i]/size));<br>
    }<br>
<br>
    //Processing<br>
    typedef itk::RegionOfInterestImageFilter&lt; InputImageType,<br>
InputImageType&gt; ROIFilterType;<br>
    typedef itk::OtsuThresholdImageFilter&lt; InputImageType, OutputImageType &gt;<br>
OtsuThresholdFilterType;<br>
    typedef itk::PasteImageFilter&lt;OutputImageType&gt; PasteImageFilterType;<br>
   <br>
    ROIFilterType::Pointer roi = ROIFilterType::New();<br>
    OtsuThresholdFilterType::Pointer otsu = OtsuThresholdFilterType::New();<br>
    PasteImageFilterType::Pointer paste = PasteImageFilterType::New();<br>
<br>
    for(unsigned i = 0; i &lt; numberOfBoxes[0]; ++i){<br>
        for(unsigned j = 0; j &lt; numberOfBoxes[1]; ++j){<br>
            for(unsigned k = 0; k &lt; numberOfBoxes[2]; ++k){<br>
               <br>
                boxIndex[0] += i * size;<br>
                boxIndex[1] += j * size;<br>
                boxIndex[2] += k * size;<br>
                boxRegion.SetIndex(boxIndex);<br>
               <br>
                roi-&gt;SetInput(reader-&gt;GetOutput());<br>
                roi-&gt;SetRegionOfInterest(boxRegion);<br>
<br>
                try{<br>
                    roi-&gt;UpdateLargestPossibleRegion();<br>
                    roi-&gt;Update();<br>
                }<br>
                catch( itk::ExceptionObject &amp; excep ){<br>
                    std::cerr &lt;&lt; &quot;ROI!&quot; &lt;&lt; std::endl;<br>
                    std::cerr &lt;&lt; &quot;Exception caught !&quot; &lt;&lt; std::endl;<br>
                    std::cerr &lt;&lt; excep &lt;&lt; std::endl;<br>
                }<br>
               <br>
                otsu-&gt;SetInput(roi-&gt;GetOutput());<br>
                otsu-&gt;SetNumberOfHistogramBins(128);<br>
                otsu-&gt;SetInsideValue(255);<br>
                otsu-&gt;SetOutsideValue(0);<br>
               <br>
                try{<br>
                    otsu-&gt;UpdateLargestPossibleRegion();<br>
                    otsu-&gt;Update();<br>
                }<br>
                catch( itk::ExceptionObject &amp; excep ){<br>
                    std::cerr &lt;&lt; &quot;Otsu!&quot; &lt;&lt; std::endl;<br>
                    std::cerr &lt;&lt; &quot;Exception caught !&quot; &lt;&lt; std::endl;<br>
                    std::cerr &lt;&lt; excep &lt;&lt; std::endl;<br>
                }<br>
                otsu-&gt;Print(std::cout);<br>
               <br>
                paste-&gt;SetDestinationImage(output);<br>
                paste-&gt;SetSourceImage(otsu-&gt;GetOutput());<br>
                paste-&gt;SetDestinationIndex( boxIndex );<br>
                paste-&gt;SetSourceRegion( boxRegion );<br>
<br>
                try{<br>
                    paste-&gt;UpdateLargestPossibleRegion();<br>
                    paste-&gt;Update();<br>
                }<br>
                catch( itk::ExceptionObject &amp; excep ){<br>
                    std::cerr &lt;&lt; &quot;Paste!&quot; &lt;&lt; std::endl;<br>
                    std::cerr &lt;&lt; &quot;Exception caught !&quot; &lt;&lt; std::endl;<br>
                    std::cerr &lt;&lt; excep &lt;&lt; std::endl;<br>
                    std::cerr &lt;&lt; &quot;i,j,k = &quot; &lt;&lt; i &lt;&lt; &#39;,&#39; &lt;&lt; j &lt;&lt; &#39;,&#39; &lt;&lt; k &lt;&lt;<br>
std::endl;<br>
                    boxRegion.Print(std::cerr);<br>
                    paste.Print(std::cerr);<br>
                    paste-&gt;GetOutput()-&gt;Print(std::cerr);<br>
                }<br>
                output = paste-&gt;GetOutput();<br>
                output-&gt;DisconnectPipeline();<br>
            }<br>
        }<br>
    }<br>
   <br>
    ////////////////////////  Image writing //////////////////////////<br>
    typedef itk::ImageFileWriter&lt; OutputImageType &gt; WriterType;<br>
<br>
    WriterType::Pointer writer = WriterType::New();<br>
<br>
    string outputFileName(inputFileName);<br>
    size_t pos = outputFileName.find_last_of(&#39;.&#39;);<br>
    outputFileName.insert(pos, &quot;_result_&quot;+toString(size));<br>
<br>
    writer-&gt;SetInput(output);<br>
    writer-&gt;SetFileName(outputFileName.c_str());<br>
    writer-&gt;Update();<br>
    try{<br>
        writer-&gt;Update();<br>
    }<br>
    catch( itk::ExceptionObject &amp; excep ){<br>
        std::cerr &lt;&lt; &quot;Write!&quot; &lt;&lt; std::endl;<br>
        std::cerr &lt;&lt; &quot;Exception caught !&quot; &lt;&lt; std::endl;<br>
        std::cerr &lt;&lt; excep &lt;&lt; std::endl;<br>
    }<br>
    return 0;<br>
}<br>
<br>
Thanks for your help,<br>
Regards,<br>
Loïc<br>
<br>
<br>
<br>
</blockquote>
</div></div></blockquote></div><br>