Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasfilter.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include <Base/Image/Image.hh>
26 #include <Filter/Mean.hh>
27 #include <Filter/Gauss.hh>
28 #include <Filter/Median.hh>
29 #include <Filter/Rescale.hh>
30 #include <Base/Image/ImageIO.hh>
31 #include <Utils/Param.hh>
32 #include <Utils/IOUtils.hh>
33 
34 #ifndef WIN32
35 #include <unistd.h>
36 #endif //WIN32
37 
38 using namespace std;
39 using namespace BIAS;
40 
41 
42 /** @file
43  @ingroup g_tools
44  @brief Tool to filter an image with Mean, Gauss, Median or rescale, see biasfilter.cpp
45  @author evers */
46 
47 /** \internal */
48 enum filtertype {none, mean, median, gauss, rescale};
49 
50 
51 void usage()
52 {
53  cout <<"Usage: biasfilter [options] infile outfile"<<endl
54  <<" Options: "<<endl
55  <<"-r radius : size of the filter kernel: size = 2 * radius + 1"<<endl
56  <<"-a : use mean filter"<<endl
57  <<"-g : use gauss filter"<<endl
58  <<"-b : use median filter"<<endl
59  <<"-s : scale image, give factor with ="<<endl
60 
61  <<"-v : verbose"<<endl
62  <<"-t thresh : median: ignore x<=thresh"<<endl;
63 }
64 
65 int main(int argc, char *argv[])
66 {
68  Median<float, float> MedianFilterFLT;
70  Mean<float, float> meanfilterFLT;
72  Gauss<float, float> gaussfilterFLT;
74  Rescale<float, float> rescalefilterFLT;
75 
84  // RankValue rankvaluefilter;
85 
86  Param param;
87  vector<string> enums;
88  vector<int> ids;
89  enums.push_back("None"); ids.push_back(none);
90  enums.push_back("Mean"); ids.push_back(mean);
91  enums.push_back("Median"); ids.push_back(median);
92  enums.push_back("Gauss"); ids.push_back(gauss);
93  enums.push_back("Rescale");ids.push_back(rescale);
94  int *filter = param.AddParamEnum("FilterType","The filter to apply",enums,0,&ids,'f');
95  int *rad = param.AddParamInt("Radius", "Radius of image filter", 3,1,1000,'r');
96  int *threshold = param.AddParamInt("Threshold", "threshold for image filter", 0,1,100000000,'t');
97  int *factor = param.AddParamInt("Factor", "Rescale factor", 0,1,100000000,'s');
98  bool *verbose = param.AddParamBool("Verbose", "Verbose output", false,'v');
99  string *input = param.AddParamString("Input", "Input image to filter", "", 'i');
100  string *output = param.AddParamString("Output", "Output image for result", "out.mip", 'o');
101 
102  if (!IOUtils::ParseCommandLineEvalHelp(param, argc, argv))
103  { return 0; }
104 
105  filtertype UseFilter = (filtertype)(*filter);
106  if (UseFilter == none ) {
107  cout <<"==> No filtertype specified."<<endl;
108  usage();
109  exit(1);
110  }
111 
112  MedianFilterUC.SetSize(*rad);
113  MedianFilterFLT.SetSize(*rad);
114  meanfilterUC.SetSize(*rad);
115  meanfilterFLT.SetSize(*rad);
116  gaussfilterUC.SetHalfWinSize(*rad);
117  gaussfilterFLT.SetHalfWinSize(*rad);
118  rescalefilterUC.SetFactor(*factor);
119  rescalefilterFLT.SetFactor(*factor);
120 
121 
122  if (*verbose){
123  switch (UseFilter) {
124  case median: cout<<"Median filter "<<endl;//on zero values";
125  break;
126  case mean: cout<<"Mean filter"<<endl;
127  break;
128  case gauss: cout<<"Gauss filter"<<endl;
129  break;
130  case rescale: cout<<"Rescale filter with factor: "<<rescale<<endl;
131  break;
132  default: ;
133  }
134  cout <<" size "<<2*(*rad)+1<<"x"<<2*(*rad)+1<<endl;
135  if (*threshold > -FLT_MAX) cout <<" threshold "<<*threshold<<endl;
136  }
137 
138  ImageBase inimage,outimage;
139  if (ImageIO::Load(*input,inimage)<0){
140  BIASERR("Could not load input image:"<<*input);
141  exit(1);
142  }
143 
144  if (inimage.GetStorageType() == ImageBase::ST_unsignedchar) {
145  Image<unsigned char> img1(inimage),img2;
146  switch (UseFilter){
147  case median:
148  if(inimage.GetChannelCount() == 1)
149  MedianFilterUC.Filter(img1, img2);
150  else
151  MedianFilterUC.FilterColorImg(img1, img2);
152  break;
153  case mean:
154  // \bug mean filter moves image content to left top corner by at least one pixel!
155  meanfilterUC.Filter(img1,img2);
156  break;
157  case gauss:
158  gaussfilterUC.Filter(img1,img2);
159  break;
160  case rescale:
161  rescalefilterUC.Filter(img1,img2);
162  break;
163  default:
164  cout <<"==> No filtertype specified."<<endl;
165  usage();
166  exit(1);
167  break;
168  }
169  ImageIO::Save(*output,img2);
170  }
171  else if (inimage.GetStorageType() == ImageBase::ST_float) {
172  Image<float> img1(inimage),img2;
173  switch (UseFilter){
174  case median:
175  //MedianFilterFLT.FilterOnlyZeroIgnoreZero3x3(img1, img2);
176  MedianFilterFLT.Filter(img1, img2);
177  break;
178  case mean:
179  meanfilterFLT.Filter(img1,img2);
180  break;
181  case gauss:
182  gaussfilterFLT.Filter(img1,img2);
183  break;
184  case rescale:
185  rescalefilterFLT.Filter(img1,img2);
186  break;
187  default:
188  cout <<"==> No filtertype specified."<<endl;
189  usage();
190  exit(1);
191  break;
192  }
193  ImageIO::Save(*output,img2);
194  }
195  else{
196  BIASERR("Neither a unsigned char nor a float image, returning unfiltered.");
197  }
198  return 0;
199 }
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
virtual function for interface definition
Definition: Median.cpp:69
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
averages over a region with constant weights
Definition: Mean.cpp:61
void SetHalfWinSize(const int hws, bool AdjustSigma=true)
define the half win size of the kernel, if AdjustSigma is true sigma is computed according to the cut...
Definition: Gauss.hh:173
bool * AddParamBool(const std::string &name, const std::string &help, bool deflt=false, char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:305
int * AddParamEnum(const std::string &name, const std::string &help, const std::vector< std::string > &enums, const int deflt=0, const std::vector< int > *IDs=NULL, const char cmdshort=0, const int Group=GRP_NOSHOW)
Definition: Param.cpp:468
virtual parent class for API definition of all (future) filters
Definition: FilterBase.hh:77
void SetSize(const int size)
Definition: Mean.hh:84
Implements a 2D median filter for images.
Definition: Median.hh:39
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
int FilterColorImg(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
Definition: Median.cpp:1030
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
scales the src to dst using the downsampling factor from SetFactor()
Definition: Rescale.cpp:89
void SetBorderHandling(const int bh)
Definition: FilterBase.hh:127
void SetSize(int newsize, int secondsize=-1)
Definition: Median.hh:140
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
sets gauss kernel if params changed and calls convolution or fast grey implementation if possible ...
Definition: Gauss.cpp:89
average mean filter
Definition: Mean.hh:41
This class Param provides generic support for parameters.
Definition: Param.hh:231
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
int * AddParamInt(const std::string &name, const std::string &help, int deflt=0, int min=std::numeric_limits< int >::min(), int max=std::numeric_limits< int >::max(), char cmdshort=0, int Group=GRP_NOSHOW)
For all adding routines:
Definition: Param.cpp:276
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
std::string * AddParamString(const std::string &name, const std::string &help, std::string deflt="", char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:327
void SetFactor(double factor)
the downsampling factor
Definition: Rescale.hh:361