]> Creatis software - clitk.git/blob - filters/clitkFooImageGenericFilter.txx
d0967afd0493fabd8e4f86d41d2e668ef68a3ab6
[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
36 namespace clitk
37 {
38
39 //--------------------------------------------------------------------
40 template<class args_info_type>
41 FooImageGenericFilter<args_info_type>::FooImageGenericFilter():
42         ImageToImageGenericFilter<Self>("FooImage") {
43     InitializeImageType<2>();
44     InitializeImageType<3>();
45     InitializeImageType<4>();
46 }
47 //--------------------------------------------------------------------
48
49
50 //--------------------------------------------------------------------
51 template<class args_info_type>
52 template<unsigned int Dim>
53 void FooImageGenericFilter<args_info_type>::InitializeImageType() {
54     ADD_DEFAULT_IMAGE_TYPES(Dim);
55 }
56 //--------------------------------------------------------------------
57
58
59 //--------------------------------------------------------------------
60 template<class args_info_type>
61 void FooImageGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a) {
62     mArgsInfo=a;
63     SetIOVerbose(mArgsInfo.verbose_flag);
64     if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
65
66     if (mArgsInfo.input_given) {
67         SetInputFilename(mArgsInfo.input_arg);
68     }
69     if (mArgsInfo.output_given) {
70         SetOutputFilename(mArgsInfo.output_arg);
71     }
72 }
73 //--------------------------------------------------------------------
74
75 //--------------------------------------------------------------------
76 // Update with the number of dimensions and the pixeltype
77 //--------------------------------------------------------------------
78 template<class args_info_type>
79 template<class InputImageType>
80 void
81 FooImageGenericFilter<args_info_type>::UpdateWithInputImageType()
82 {
83
84     // Reading input
85     typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
86
87     // Main filter
88     typedef typename InputImageType::PixelType PixelType;
89     typedef itk::Image<char, InputImageType::ImageDimension> OutputImageType;
90
91     // Filter
92     typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
93     typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
94     thresholdFilter->SetInput(input);
95     thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
96
97     if (mArgsInfo.lower_given) thresholdFilter->SetLowerThreshold(static_cast<PixelType>(mArgsInfo.lower_arg));
98     if (mArgsInfo.upper_given) thresholdFilter->SetUpperThreshold(static_cast<PixelType>(mArgsInfo.upper_arg));
99
100     if (mArgsInfo.mode_arg == std::string("both")) {
101         thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
102         thresholdFilter->Update();
103
104         typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
105         this->template SetNextOutput<OutputImageType>(outputImage);
106     }
107     else {
108         typename InputImageType::Pointer outputImage;
109         thresholdFilter->SetOutsideValue(0);
110         if (mArgsInfo.mode_arg == std::string("BG")) {
111             typedef itk::MaskImageFilter<InputImageType,OutputImageType> maskFilterType;
112             typename maskFilterType::Pointer maskFilter = maskFilterType::New();
113             maskFilter->SetInput1(input);
114             maskFilter->SetInput2(thresholdFilter->GetOutput());
115             maskFilter->SetOutsideValue(mArgsInfo.bg_arg);
116             maskFilter->Update();
117             outputImage = maskFilter->GetOutput();
118         }
119         else {
120             typedef itk::MaskNegatedImageFilter<InputImageType,OutputImageType> maskFilterType;
121             typename maskFilterType::Pointer maskFilter = maskFilterType::New();
122             maskFilter->SetInput1(input);
123             maskFilter->SetInput2(thresholdFilter->GetOutput());
124             maskFilter->SetOutsideValue(mArgsInfo.fg_arg);
125             maskFilter->Update();
126             outputImage = maskFilter->GetOutput();
127         }
128         // Write/Save results
129         this->template SetNextOutput<InputImageType>(outputImage);
130     }
131 }
132 //--------------------------------------------------------------------
133
134
135 }//end clitk
136
137 #endif //#define clitkFooImageGenericFilter_txx