App
TLoGSharpen.hpp
Go to the documentation of this file.
1 
2 namespace app {
3  template <typename TPIXEL, typename TCHANNEL> class TLoGSharpen;
4 }
5 #ifndef TLOGSHARPEN_HPP_
6 #define TLOGSHARPEN_HPP_
7 
8 #include <cstdlib>
9 #include <cmath>
10 #include <cstring>
11 
12 #include "TFilter.hpp"
13 #include "TLinearFilter.hpp"
14 
15 #include "../typedefs.hpp"
16 #include "../bitmaps/TBitmap.hpp"
17 
18 namespace app {
19 
20 template <typename TPIXEL, typename TCHANNEL>
21 class TLoGSharpen : public TLinearFilter<TPIXEL, TCHANNEL> {
22 public:
23  TLoGSharpen(double sigma, double force, int area) {
24 
25  if (sigma <=0.34) {
26  this->initOk = 1; // Aplikace filtru neprobehne, ale inicializace je OK - filtr nema zadny efekt
27  return;
28  }
29  this->kernelHCenter = 3 * sigma;
30  this->kernelWidth = 2 * this->kernelHCenter + 1;
31  this->kernelWCenter = 3 * sigma;
32  this->kernelHeight = 2 * this->kernelWCenter + 1;
33 
34  force = (force + 1) / (1.0000001 - force);
35 
36  this->kernel = (double *) malloc(this->kernelWidth * this->kernelHeight * sizeof(double));
37  if (this->kernel == NULL) { return; }
38 
39  double sig2 = sigma * sigma;
40  double k1 = 2 * sig2;
41  double k2 = 1 / (k1);
42  double k3 = force * -k2 / (0.3141592654e1 * sig2 * sig2);
43 
44  int x = 0;
45  int y = 0;
46  double r2 = 0;
47  double posSum = 0;
48  double negSum = 0;
49  double tmp = 0;
50 
51  // Inicializace koeficientu
52  for (int i = 0; i < this->kernelHeight; i++) {
53  for (int j = 0; j < this->kernelWidth; j++) {
54  x = this->kernelWCenter - j;
55  y = this->kernelHCenter - i;
56  r2 = x * x + y * y;
57  tmp = k3 * exp(-k2 * (r2)) * (-k1 + r2);
58  if (tmp >= 0) { posSum += tmp; } else { negSum += tmp; }
59  this->kernel[j + this->kernelWidth * i] = tmp;
60  }
61  }
62 
63  // Normalizace na celkovy soucet 0, udela se vazeny prumer ze sumy negativnich hodnot a adekvatne se upravi pozitivni koeficienty, dle vah jake maji.
64  // lze optimalizovat kladne hodnoty by mely byt jen do polomeru sigma
65  double normDelta = (area) ? -(posSum + negSum - 1) / posSum : -(posSum + negSum) / posSum;
66  double * ptr = this->kernel;
67  double * ptrEnd = this->kernel + this->kernelWidth * this->kernelHeight;
68  while (ptr < ptrEnd) {
69  if (*ptr >= 0) {
70  *ptr += *ptr * normDelta;
71  }
72  ptr++;
73  }
74 
75  // Přičtení jedničky k střednímu, aby nebyla jen detekce hran
76  if (!area) { this->kernel[this->kernelWCenter + this->kernelWidth * this->kernelHCenter] += 1; }
77 
78  this->initOk = 1;
79  }
80 
81  virtual ~TLoGSharpen();
82 };
83 
84 } /* namespace app */
85 #endif /* TLOGSHARPEN_HPP_ */