]> Creatis software - clitk.git/blob - filters/clitkFooImageGenericFilter.txx
b2c9c293a2ad23ce1818794512907b246076dd10
[clitk.git] / filters / clitkFooImageGenericFilter.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18 #ifndef clitkFooImageGenericFilter_txx
19 #define clitkFooImageGenericFilter_txx
20
21 /* =================================================
22  * @file   clitkFooImageGenericFilter.txx
23  * @author Jef Vandemeulebroucke <jef@creatis.insa-lyon.fr>
24  * @date   29 june 2009
25  *
26  * @brief
27  *
28  ===================================================*/
29
30 // itk include
31 #include "itkBinaryThresholdImageFilter.h"
32 #include "itkMaskImageFilter.h"
33 #include "itkMaskNegatedImageFilter.h"
34 #include <clitkCommon.h>
35 #include <tiff.h>
36
37 namespace clitk
38 {
39
40 //--------------------------------------------------------------------
41 template<class args_info_type>
42 FooImageGenericFilter<args_info_type>::FooImageGenericFilter():
43         ImageToImageGenericFilter<Self>("FooImage") {
44     InitializeImageType<2>();
45     InitializeImageType<3>();
46     InitializeImageType<4>();
47 }
48 //--------------------------------------------------------------------
49
50
51 //--------------------------------------------------------------------
52 template<class args_info_type>
53 template<unsigned int Dim>
54 void FooImageGenericFilter<args_info_type>::InitializeImageType() {
55     ADD_IMAGE_TYPE(Dim, char);
56     ADD_IMAGE_TYPE(Dim, uchar);
57     ADD_IMAGE_TYPE(Dim, short);
58     ADD_IMAGE_TYPE(Dim, ushort);
59     ADD_IMAGE_TYPE(Dim, int);
60     ADD_IMAGE_TYPE(Dim, float);
61     ADD_IMAGE_TYPE(Dim, double);
62 }
63 //--------------------------------------------------------------------
64
65
66 //--------------------------------------------------------------------
67 template<class args_info_type>
68 void FooImageGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a) {
69     mArgsInfo=a;
70     SetIOVerbose(mArgsInfo.verbose_flag);
71     if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
72
73     if (mArgsInfo.input_given) {
74         SetInputFilename(mArgsInfo.input_arg);
75     }
76     if (mArgsInfo.output_given) {
77         SetOutputFilename(mArgsInfo.output_arg);
78     }
79 }
80 //--------------------------------------------------------------------
81
82 //--------------------------------------------------------------------
83 // Update with the number of dimensions and the pixeltype
84 //--------------------------------------------------------------------
85 template<class args_info_type>
86 template<class InputImageType>
87 void
88 FooImageGenericFilter<args_info_type>::UpdateWithInputImageType()
89 {
90
91     // Reading input
92     typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
93
94     // Main filter
95     typedef typename InputImageType::PixelType PixelType;
96     typedef itk::Image<char, InputImageType::ImageDimension> OutputImageType;
97
98     // Filter
99     typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
100     typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
101     thresholdFilter->SetInput(input);
102     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
103
104     if (mArgsInfo.lower_given) thresholdFilter->SetLowerThreshold(static_cast<PixelType>(mArgsInfo.lower_arg));
105     if (mArgsInfo.upper_given) thresholdFilter->SetUpperThreshold(static_cast<PixelType>(mArgsInfo.upper_arg));
106
107     if (mArgsInfo.mode_arg == std::string("both")) {
108         thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
109         thresholdFilter->Update();
110
111         typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
112         this->template SetNextOutput<OutputImageType>(outputImage);
113     }
114     else {
115         typename InputImageType::Pointer outputImage;
116         thresholdFilter->SetOutsideValue(0);
117         if (mArgsInfo.mode_arg == std::string("BG")) {
118             typedef itk::MaskImageFilter<InputImageType,OutputImageType> maskFilterType;
119             typename maskFilterType::Pointer maskFilter = maskFilterType::New();
120             maskFilter->SetInput1(input);
121             maskFilter->SetInput2(thresholdFilter->GetOutput());
122             maskFilter->SetOutsideValue(mArgsInfo.bg_arg);
123             maskFilter->Update();
124             outputImage = maskFilter->GetOutput();
125         }
126         else {
127             typedef itk::MaskNegatedImageFilter<InputImageType,OutputImageType> maskFilterType;
128             typename maskFilterType::Pointer maskFilter = maskFilterType::New();
129             maskFilter->SetInput1(input);
130             maskFilter->SetInput2(thresholdFilter->GetOutput());
131             maskFilter->SetOutsideValue(mArgsInfo.fg_arg);
132             maskFilter->Update();
133             outputImage = maskFilter->GetOutput();
134         }
135         // Write/Save results
136         this->template SetNextOutput<InputImageType>(outputImage);
137     }
138 }
139 //--------------------------------------------------------------------
140
141
142 }//end clitk
143
144 #endif //#define clitkFooImageGenericFilter_txx