Hi all!<br><br>I'm trying to make an adaptive thresholding using the Otsu method. To do so, I want to process my image by region. The idea is to separate the original picture in several blocks, process each block and reconstruct the final output by gathering the blocks. <br>
I am trying to do it using the RegionOfInterestImageFilter, the OtsuThresholdImageFilter and the PasteImageFilter in a loop over regions, I have grabbed some informations in this mailing list but I can't manage to make it working. The process works if I do not loop over regions.<br>
<br>Here is my code:<br><br>#include "itkImage.h"<br>#include "itkImageFileReader.h"<br>#include "itkImageFileWriter.h"<br><br>#include "itkRegionOfInterestImageFilter.h"<br>#include "itkOtsuThresholdImageFilter.h"<br>
#include "itkPasteImageFilter.h"<br><br>#include <iostream><br>#include <string><br>#include <sstream><br>#include <cstdlib><br>using namespace std;<br><br><br>template <typename T> string toString(T val){<br>
ostringstream oss;<br> oss << val;<br> return oss.str();<br>}<br><br>int main(int argc, char *argv[]){<br> //////////////////////////// Usage //////////////////////////////<br> if (argc<3){<br> std::cerr << "Missing arguments!" << std::endl;<br>
std::cerr << "Usage : " << argv[0]<br> << " InputFile"<br> << " Size" << 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< InputPixelType, Dimension > InputImageType;<br> typedef itk::Image< OutputPixelType, Dimension > OutputImageType;<br><br> //////////////////////// Image reading //////////////////////////<br>
typedef itk::ImageFileReader< InputImageType > ReaderType;<br> <br> ReaderType::Pointer reader = ReaderType::New();<br> reader->SetFileName(inputFileName);<br> try{<br> reader->Update();<br>
}<br> catch( itk::ExceptionObject & excep ){<br> std::cerr << "Exception caught !" << std::endl;<br> std::cerr << excep << 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 < Dimension; ++i){<br> outIndex[i]=reader->GetOutput()->GetBufferedRegion().GetIndex()[i];<br> outSize[i]=reader->GetOutput()->GetBufferedRegion().GetSize()[i];<br> }<br>
<br> outRegion.SetIndex(outIndex);<br> outRegion.SetSize(outSize);<br> <br> output->SetRegions(outRegion);<br> output->Allocate();<br> output->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 < Dimension; ++i){<br> boxIndex[i] = outIndex[i];<br> }<br>
for (int i = 0; i < 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 < Dimension; ++i){<br>
numberOfBoxes[i] = static_cast<unsigned>((outSize[i]/size));<br> }<br><br> //Processing<br> typedef itk::RegionOfInterestImageFilter< InputImageType, InputImageType> ROIFilterType;<br> typedef itk::OtsuThresholdImageFilter< InputImageType, OutputImageType > OtsuThresholdFilterType;<br>
typedef itk::PasteImageFilter<OutputImageType> 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 < numberOfBoxes[0]; ++i){<br> for(unsigned j = 0; j < numberOfBoxes[1]; ++j){<br> for(unsigned k = 0; k < 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->SetInput(reader->GetOutput());<br> roi->SetRegionOfInterest(boxRegion);<br><br> try{<br> roi->UpdateLargestPossibleRegion();<br> roi->Update();<br>
}<br> catch( itk::ExceptionObject & excep ){<br> std::cerr << "ROI!" << std::endl;<br> std::cerr << "Exception caught !" << std::endl;<br>
std::cerr << excep << std::endl;<br> }<br> <br> otsu->SetInput(roi->GetOutput());<br> otsu->SetNumberOfHistogramBins(128);<br>
otsu->SetInsideValue(255);<br> otsu->SetOutsideValue(0);<br> <br> try{<br> otsu->UpdateLargestPossibleRegion();<br> otsu->Update();<br>
}<br> catch( itk::ExceptionObject & excep ){<br> std::cerr << "Otsu!" << std::endl;<br> std::cerr << "Exception caught !" << std::endl;<br>
std::cerr << excep << std::endl;<br> }<br> otsu->Print(std::cout);<br> <br> paste->SetDestinationImage(output);<br> paste->SetSourceImage(otsu->GetOutput());<br>
paste->SetDestinationIndex( boxIndex );<br> paste->SetSourceRegion( boxRegion );<br><br> try{<br> paste->UpdateLargestPossibleRegion();<br> paste->Update();<br>
}<br> catch( itk::ExceptionObject & excep ){<br> std::cerr << "Paste!" << std::endl;<br> std::cerr << "Exception caught !" << std::endl;<br>
std::cerr << excep << std::endl;<br> std::cerr << "i,j,k = " << i << ',' << j << ',' << k << std::endl;<br>
boxRegion.Print(std::cerr);<br> paste.Print(std::cerr);<br> paste->GetOutput()->Print(std::cerr);<br> }<br> output = paste->GetOutput();<br>
output->DisconnectPipeline();<br> }<br> }<br> }<br> <br> //////////////////////// Image writing //////////////////////////<br> typedef itk::ImageFileWriter< OutputImageType > WriterType;<br>
<br> WriterType::Pointer writer = WriterType::New();<br><br> string outputFileName(inputFileName);<br> size_t pos = outputFileName.find_last_of('.');<br> outputFileName.insert(pos, "_result_"+toString(size));<br>
<br> writer->SetInput(output);<br> writer->SetFileName(outputFileName.c_str());<br> writer->Update();<br> try{<br> writer->Update();<br> }<br> catch( itk::ExceptionObject & excep ){<br>
std::cerr << "Write!" << std::endl;<br> std::cerr << "Exception caught !" << std::endl;<br> std::cerr << excep << std::endl;<br> }<br> return 0;<br>
}<br><br>Thanks for your help,<br>Regards,<br>Loïc<br><br>