Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasGPUFilter.cpp
1 #include <Base/Image/Image.hh>
2 #include <Base/Image/ImageIO.hh>
3 #include <OpenGLFramework/SpecializedBatches/GpuImageFilter.hh>
4 #include <OpenGLFramework/Base/glfException.hh>
5 #include <OpenGLFramework/RenderingContext/glfPBuffer.hh>
6 
7 #ifndef WIN32
8 #include <unistd.h>
9 #else //WIN32
10 #include "Base/Common/getopt_W32.h"
11 #endif //WIN32
12 using namespace std;
13 using namespace BIAS;
14 
15 /**@file
16  @ingroup g_tools
17  @brief Tool to filter an image with Mean, Gauss or Median, see biasGPUFilter.cpp
18  @author djung */
19 
20 /** \internal */
21 enum filtertype {
22  none, mean, median, gauss
23 };
24 
25 void usage() {
26  cout << "Usage: biasGPUfilter [options] infile outfile" << endl;
27  cout << " Options: " << endl;
28  cout << "-r radius : size of the filter kernel: size = 2 * radius + 1" << endl;
29  cout << "-a : use mean filter" << endl;
30  cout << "-g : use gauss filter" << endl;
31  cout << "-b : use median filter" << endl;
32  cout << "-v : verbose" << endl;
33  cout << "-p : median: independent channel sort. Else sort will be done on grey values" << endl;
34  cout << "-t thresh : gauss: sigma-value" << endl;
35 }
36 
37 int main(int argc, char *argv[]) {
38 
39  filtertype UseFilter = none;
40  int rad = -1;
41  int c = 1;
42  float threshold = -FLT_MAX;
43  bool verbose = false;
44  bool planar = false;
45 
46  while (c > 0) {
47  c = getopt(argc, argv, "r:abpt:vg:");
48  if (c > 0) {
49  switch (c) {
50  case 'a':
51  UseFilter = mean;
52  break;
53  case 'b':
54  UseFilter = median;
55  break;
56  case 'g':
57  UseFilter = gauss;
58  break;
59  case 'r':
60  rad = atoi(optarg);
61  break;
62  case 't':
63  threshold = float(atof(optarg));
64  break;
65  case 'v':
66  verbose = true;
67  break;
68  case 'p':
69  planar = true;
70  break;
71  default:
72  usage();
73  exit(1);
74  }
75  }
76  }
77 
78  if (argc - optind < 2) {
79  cout << "==> To few arguments." << endl;
80  usage();
81  exit(1);
82  }
83 
84  if (UseFilter == none) {
85  cout << "==> No filtertype specified." << endl;
86  usage();
87  exit(1);
88  }
89 
90  if (verbose) {
91  switch (UseFilter) {
92  case median:
93  cout << "Median filter ";
94  if (planar) {
95  cout << " planar ";
96  }
97  else {
98  cout << " grey ";
99  }
100  break;
101  case mean:
102  cout << "Mean filter";
103  break;
104  case gauss:
105  cout << "Gauss filter";
106  break;
107  default:
108  break;
109  }
110  cout << " size " << 2 * rad + 1 << "x" << 2 * rad + 1;
111  if (threshold > -FLT_MAX) cout << " threshold " << threshold;
112  cout << " : " << argv[optind] << " -> " << argv[optind + 1] << endl;
113  }
114 
115  ImageBase inImage, outImage;
116  if (ImageIO::Load(argv[optind], inImage) < 0) exit(1);
117 
118  glfPBuffer pbuffer;
119  try {
120 
122  cfg.width = 5;
123  cfg.height = 5;
124  cfg.doubleBuffer = 0;
125  cfg.redSize = 8;
126  cfg.greenSize = 8;
127  cfg.blueSize = 8;
128  cfg.alphaSize = 0;
129  cfg.depthSize = 24;
130  cfg.stencilSize = 0;
131 
132  pbuffer.Init(cfg);
133 
134  GpuImageFilter gpuFilter;
135 
136  switch (UseFilter) {
137  case median:
138  if (rad > 15) {
139  BIASERR("Median is limited a half-window size of 15"<<endl);
140  return -1;
141  }
142  if (rad > 5) {
143  BIASWARN("Median with rad > 5 will most likely not work on the GPU with this implementation"<<endl<<" -> check the image for artefacts!");
144  }
145  gpuFilter.FilterMedian(inImage, outImage, inImage.GetStorageType(), rad, threshold, planar);
146  break;
147  case mean:
148  gpuFilter.FilterMean(inImage, outImage, inImage.GetStorageType(), rad, threshold);
149  break;
150  case gauss:
151  if (threshold < 0.) {
152  threshold = 1.;
153  }
154  gpuFilter.FilterGauss(inImage, outImage, inImage.GetStorageType(), rad, threshold);
155  break;
156  default:
157  cout << "==> No filtertype specified." << endl;
158  usage();
159  exit(1);
160  break;
161  }
162  }
163  catch (glfException& e) {
164  pbuffer.Destroy();
165  std::cout << "Error: " << e.GetMessageString() << std::endl;
166  return -1;
167  }
168 
169  ImageIO::Save(argv[optind + 1], outImage);
170 
171  return 0;
172 
173 }
basic image filter on the gpu
Exception class used for run-time errors in the OpenGLFramework.
Definition: glfException.hh:79
virtual void Destroy()
Uninitializes the rendering context.
const std::string & GetMessageString() const
Returns the description of the error including the file name and line number where the error occured...
void FilterMean(ImageBase &in, ImageBase &out, enum ImageBase::EStorageType type, int rad, float threshold)
GLX pbuffer rendering context.
void FilterGauss(ImageBase &in, ImageBase &out, enum ImageBase::EStorageType type, int rad, float threshold)
virtual void Init(const glfRenderingContextConfig &config)
Initializes the rendering context with the given configuration.
void FilterMedian(ImageBase &in, ImageBase &out, enum ImageBase::EStorageType type, int rad, float threshold, bool planar)
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
Configuration for a rendering context.