]> Creatis software - clitk.git/blob - tools/clitkSetBackgroundGenericFilter.txx
Initial revision
[clitk.git] / tools / clitkSetBackgroundGenericFilter.txx
1 #ifndef clitkSetBackgroundGenericFilter_txx
2 #define clitkSetBackgroundGenericFilter_txx
3
4 /* =================================================
5  * @file   clitkSetBackgroundGenericFilter.txx
6  * @author 
7  * @date   
8  * 
9  * @brief 
10  * 
11  ===================================================*/
12
13
14 namespace clitk
15 {
16
17   //-------------------------------------------------------------------
18   // Update with the number of dimensions
19   //-------------------------------------------------------------------
20   template<unsigned int Dimension>
21   void 
22   SetBackgroundGenericFilter::UpdateWithDim(std::string PixelType)
23   {
24     if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
25
26     if(PixelType == "short"){  
27       if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
28       UpdateWithDimAndPixelType<Dimension, signed short>(); 
29     }
30     //    else if(PixelType == "unsigned_short"){  
31     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
32     //       UpdateWithDimAndPixelType<Dimension, unsigned short>(); 
33     //     }
34     
35     else if (PixelType == "unsigned_char"){ 
36       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
37       UpdateWithDimAndPixelType<Dimension, unsigned char>();
38     }
39     
40     //     else if (PixelType == "char"){ 
41     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
42     //       UpdateWithDimAndPixelType<Dimension, signed char>();
43     //     }
44     else {
45       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
46       UpdateWithDimAndPixelType<Dimension, float>();
47     }
48   }
49
50
51   //-------------------------------------------------------------------
52   // Update with the number of dimensions and the pixeltype
53   //-------------------------------------------------------------------
54   template <unsigned int Dimension, class  PixelType> 
55   void 
56   SetBackgroundGenericFilter::UpdateWithDimAndPixelType()
57   {
58
59   // ImageTypes
60     typedef itk::Image<PixelType, Dimension> InputImageType;
61     typedef itk::Image<float, Dimension> MaskImageType;
62     
63     // Read the input
64     typedef itk::ImageFileReader<InputImageType> InputReaderType;
65     typename InputReaderType::Pointer reader = InputReaderType::New();
66     reader->SetFileName( m_InputFileName);
67     typename InputImageType::Pointer input= reader->GetOutput();
68
69     // Read the mask
70     typedef itk::ImageFileReader<MaskImageType> MaskReaderType;
71     typename MaskReaderType::Pointer maskReader = MaskReaderType::New();
72     maskReader->SetFileName( m_ArgsInfo.mask_arg);
73     typename MaskImageType::Pointer mask= maskReader->GetOutput();
74
75     // Filter setting background
76     typedef clitk::SetBackgroundImageFilter<InputImageType,MaskImageType, InputImageType> SetBackgroundFilterType; 
77     typename SetBackgroundFilterType::Pointer setBackgroundFilter = SetBackgroundFilterType::New(); 
78     setBackgroundFilter->SetInput(input);
79     setBackgroundFilter->SetInput2(mask);
80     if(m_ArgsInfo.fg_flag)  setBackgroundFilter->SetForeground(m_ArgsInfo.fg_flag);
81     if(m_ArgsInfo.maskValue_given)  setBackgroundFilter->SetMaskValue(m_ArgsInfo.maskValue_arg);
82     if(m_ArgsInfo.outsideValue_given)  setBackgroundFilter->SetOutsideValue(m_ArgsInfo.outsideValue_arg);
83     setBackgroundFilter->Update();
84     typename InputImageType::Pointer output =setBackgroundFilter->GetOutput();
85           
86     // Output
87     typedef itk::ImageFileWriter<InputImageType> WriterType;
88     typename WriterType::Pointer writer = WriterType::New();
89     writer->SetFileName(m_ArgsInfo.output_arg);
90     writer->SetInput(output);
91     writer->Update();
92
93   }
94
95
96 }//end clitk
97  
98 #endif //#define clitkSetBackgroundGenericFilter_txx