Commit 2eaf99ee6ea6a65cb74d45cb6aec74b9c6bce7d3

Authored by Josh Klontz
1 parent a3fd6780

remove gabor

Showing 1 changed file with 0 additions and 206 deletions
openbr/plugins/imgproc/gabor.cpp deleted
1   -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2   - * Copyright 2012 The MITRE Corporation *
3   - * *
4   - * Licensed under the Apache License, Version 2.0 (the "License"); *
5   - * you may not use this file except in compliance with the License. *
6   - * You may obtain a copy of the License at *
7   - * *
8   - * http://www.apache.org/licenses/LICENSE-2.0 *
9   - * *
10   - * Unless required by applicable law or agreed to in writing, software *
11   - * distributed under the License is distributed on an "AS IS" BASIS, *
12   - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13   - * See the License for the specific language governing permissions and *
14   - * limitations under the License. *
15   - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
16   -
17   -#include <opencv2/imgproc/imgproc.hpp>
18   -
19   -#include <openbr/plugins/openbr_internal.h>
20   -
21   -using namespace cv;
22   -
23   -namespace br
24   -{
25   -
26   -/*!
27   - * \ingroup transforms
28   - * \brief Implements a Gabor Filter
29   - * \br_link http://en.wikipedia.org/wiki/Gabor_filter
30   - * \author Josh Klontz \cite jklontz
31   - */
32   -class GaborTransform : public UntrainableTransform
33   -{
34   - Q_OBJECT
35   - Q_ENUMS(Component)
36   - Q_PROPERTY(float lambda READ get_lambda WRITE set_lambda RESET reset_lambda STORED false)
37   - Q_PROPERTY(float theta READ get_theta WRITE set_theta RESET reset_theta STORED false)
38   - Q_PROPERTY(float psi READ get_psi WRITE set_psi RESET reset_psi STORED false)
39   - Q_PROPERTY(float sigma READ get_sigma WRITE set_sigma RESET reset_sigma STORED false)
40   - Q_PROPERTY(float gamma READ get_gamma WRITE set_gamma RESET reset_gamma STORED false)
41   - Q_PROPERTY(Component component READ get_component WRITE set_component RESET reset_component STORED false)
42   -
43   -public:
44   - /*!< */
45   - enum Component { Real,
46   - Imaginary,
47   - Magnitude,
48   - Phase };
49   -
50   -private:
51   - BR_PROPERTY(float, lambda, 0)
52   - BR_PROPERTY(float, theta, 0)
53   - BR_PROPERTY(float, psi, 0)
54   - BR_PROPERTY(float, sigma, 0)
55   - BR_PROPERTY(float, gamma, 0)
56   - BR_PROPERTY(Component, component, Phase)
57   -
58   - Mat kReal, kImaginary;
59   -
60   - friend class GaborJetTransform;
61   -
62   - static void makeWavelet(float lambda, float theta, float psi, float sigma, float gamma, Mat &kReal, Mat &kImaginary)
63   - {
64   - float sigma_x = sigma;
65   - float sigma_y = sigma/gamma;
66   -
67   - // Bounding box
68   - const double nstds = 3;
69   - int xmax = std::ceil(std::max(1.0, std::max(std::abs(nstds*sigma_x*cos(theta)),
70   - std::abs(nstds*sigma_y*sin(theta)))));
71   - int ymax = std::ceil(std::max(1.0, std::max(std::abs(nstds*sigma_x*sin(theta)),
72   - std::abs(nstds*sigma_y*cos(theta)))));
73   -
74   - // Compute kernels
75   - kReal.create(2*ymax+1, 2*xmax+1, CV_32FC1);
76   - kImaginary.create(2*ymax+1, 2*xmax+1, CV_32FC1);
77   - for (int y = -ymax; y <= ymax; y++) {
78   - int row = y + ymax;
79   - for (int x = -xmax; x <= xmax; x++) {
80   - int col = x + xmax;
81   - float x_prime = x*cos(theta) + y*sin(theta);
82   - float y_prime = -x*sin(theta) + y*cos(theta);
83   - float a = exp(-0.5 * (x_prime*x_prime + gamma*gamma*y_prime*y_prime)/(sigma*sigma));
84   - float b = 2*CV_PI*x_prime/lambda+psi;
85   - kReal.at<float>(row, col) = a*cos(b);
86   - kImaginary.at<float>(row, col) = a*sin(b);
87   - }
88   - }
89   -
90   - // Remove DC component, should only effect real kernel
91   - subtract(kReal, mean(kReal), kReal);
92   - subtract(kImaginary, mean(kImaginary), kImaginary);
93   - }
94   -
95   - void init()
96   - {
97   - makeWavelet(lambda, theta, psi, sigma, gamma, kReal, kImaginary);
98   - }
99   -
100   - void project(const Template &src, Template &dst) const
101   - {
102   - Mat real, imaginary, magnitude, phase;
103   - if (component != Imaginary)
104   - filter2D(src, real, -1, kReal);
105   - if (component != Real)
106   - filter2D(src, imaginary, -1, kImaginary);
107   - if ((component == Magnitude) || (component == Phase))
108   - cartToPolar(real, imaginary, magnitude, phase);
109   -
110   - if (component == Real) dst = real;
111   - else if (component == Imaginary) dst = imaginary;
112   - else if (component == Magnitude) dst = magnitude;
113   - else if (component == Phase) dst = phase;
114   - else qFatal("Invalid component.");
115   - }
116   -};
117   -
118   -BR_REGISTER(Transform, GaborTransform)
119   -
120   -/*!
121   - * \ingroup transforms
122   - * \brief A vector of gabor wavelets applied at a point.
123   - * \author Josh Klontz \cite jklontz
124   - */
125   -class GaborJetTransform : public UntrainableTransform
126   -{
127   - Q_OBJECT
128   - Q_ENUMS(br::GaborTransform::Component)
129   - Q_PROPERTY(QList<float> lambdas READ get_lambdas WRITE set_lambdas RESET reset_lambdas STORED false)
130   - Q_PROPERTY(QList<float> thetas READ get_thetas WRITE set_thetas RESET reset_thetas STORED false)
131   - Q_PROPERTY(QList<float> psis READ get_psis WRITE set_psis RESET reset_psis STORED false)
132   - Q_PROPERTY(QList<float> sigmas READ get_sigmas WRITE set_sigmas RESET reset_sigmas STORED false)
133   - Q_PROPERTY(QList<float> gammas READ get_gammas WRITE set_gammas RESET reset_gammas STORED false)
134   - Q_PROPERTY(br::GaborTransform::Component component READ get_component WRITE set_component RESET reset_component STORED false)
135   - BR_PROPERTY(QList<float>, lambdas, QList<float>())
136   - BR_PROPERTY(QList<float>, thetas, QList<float>())
137   - BR_PROPERTY(QList<float>, psis, QList<float>())
138   - BR_PROPERTY(QList<float>, sigmas, QList<float>())
139   - BR_PROPERTY(QList<float>, gammas, QList<float>())
140   - BR_PROPERTY(GaborTransform::Component, component, GaborTransform::Phase)
141   -
142   - QList<Mat> kReals, kImaginaries;
143   -
144   - void init()
145   - {
146   - kReals.clear();
147   - kImaginaries.clear();
148   - foreach (float lambda, lambdas)
149   - foreach (float theta, thetas)
150   - foreach (float psi, psis)
151   - foreach (float sigma, sigmas)
152   - foreach (float gamma, gammas) {
153   - Mat kReal, kImaginary;
154   - GaborTransform::makeWavelet(lambda, theta, psi, sigma, gamma, kReal, kImaginary);
155   - kReals.append(kReal);
156   - kImaginaries.append(kImaginary);
157   - }
158   - }
159   -
160   - static float response(const cv::Mat &src, const QPointF &point, const Mat &kReal, const Mat &kImaginary, GaborTransform::Component component)
161   - {
162   - Rect roi(std::max(std::min((int)(point.x() - kReal.cols/2.f), src.cols - kReal.cols), 0),
163   - std::max(std::min((int)(point.y() - kReal.rows/2.f), src.rows - kReal.rows), 0),
164   - kReal.cols,
165   - kReal.rows);
166   -
167   - float real = 0, imaginary = 0, magnitude = 0, phase = 0;
168   - if (component != GaborTransform::Imaginary) {
169   - Mat dst;
170   - multiply(src(roi), kReal, dst);
171   - real = sum(dst)[0];
172   - }
173   - if (component != GaborTransform::Real) {
174   - Mat dst;
175   - multiply(src(roi), kImaginary, dst);
176   - imaginary = sum(dst)[0];
177   - }
178   - if ((component == GaborTransform::Magnitude) || (component == GaborTransform::Phase)) {
179   - magnitude = sqrt(real*real + imaginary*imaginary);
180   - phase = atan2(imaginary, real)*180/CV_PI;
181   - }
182   -
183   - float dst = 0;
184   - if (component == GaborTransform::Real) dst = real;
185   - else if (component == GaborTransform::Imaginary) dst = imaginary;
186   - else if (component == GaborTransform::Magnitude) dst = magnitude;
187   - else if (component == GaborTransform::Phase) dst = phase;
188   - else qFatal("Invalid component.");
189   - return dst;
190   - }
191   -
192   - void project(const Template &src, Template &dst) const
193   - {
194   - const QList<QPointF> points = src.file.points();
195   - dst = Mat(points.size(), kReals.size(), CV_32FC1);
196   - for (int i=0; i<points.size(); i++)
197   - for (int j=0; j<kReals.size(); j++)
198   - dst.m().at<float>(i,j) = response(src, points[i], kReals[j], kImaginaries[j], component);
199   - }
200   -};
201   -
202   -BR_REGISTER(Transform, GaborJetTransform)
203   -
204   -} // namespace br
205   -
206   -#include "imgproc/gabor.moc"