-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffttools.hpp
237 lines (190 loc) · 6.59 KB
/
ffttools.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
Author: Christian Bailer
Contact address: Christian.Bailer@dfki.de
Department Augmented Vision DFKI
License Agreement
For Open Source Computer Vision Library
(3-clause BSD License)
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the names of the copyright holders nor the names of the contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are disclaimed.
In no event shall copyright holders or contributors be liable for any direct,
indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.
*/
#pragma once
//#include <cv.h>
#ifndef _OPENCV_FFTTOOLS_HPP_
#define _OPENCV_FFTTOOLS_HPP_
#endif
//NOTE: FFTW support is still shaky, disabled for now.
/*#ifdef USE_FFTW
#include <fftw3.h>
#endif*/
namespace FFTTools
{
// Previous declarations, to avoid warnings
cv::Mat fftd(cv::Mat img, bool backwards = false);
cv::Mat real(cv::Mat img);
cv::Mat imag(cv::Mat img);
cv::Mat magnitude(cv::Mat img);
cv::Mat complexMultiplication(cv::Mat a, cv::Mat b);
cv::Mat complexDivision(cv::Mat a, cv::Mat b);
void rearrange(cv::Mat &img);
void normalizedLogTransform(cv::Mat &img);
cv::Mat fftd(cv::Mat img, bool backwards)
{
/*
#ifdef USE_FFTW
fftw_complex * fm = (fftw_complex*) fftw_malloc(sizeof (fftw_complex) * img.cols * img.rows);
fftw_plan p = fftw_plan_dft_2d(img.rows, img.cols, fm, fm, backwards ? 1 : -1, 0 * FFTW_ESTIMATE);
if (img.channels() == 1)
{
for (int i = 0; i < img.rows; i++)
for (int j = 0; j < img.cols; j++)
{
fm[i * img.cols + j][0] = img.at<float>(i, j);
fm[i * img.cols + j][1] = 0;
}
}
else
{
assert(img.channels() == 2);
for (int i = 0; i < img.rows; i++)
for (int j = 0; j < img.cols; j++)
{
fm[i * img.cols + j][0] = img.at<cv::Vec2d > (i, j)[0];
fm[i * img.cols + j][1] = img.at<cv::Vec2d > (i, j)[1];
}
}
fftw_execute(p);
cv::Mat res(img.rows, img.cols, CV_64FC2);
for (int i = 0; i < img.rows; i++)
for (int j = 0; j < img.cols; j++)
{
res.at<cv::Vec2d > (i, j)[0] = fm[i * img.cols + j][0];
res.at<cv::Vec2d > (i, j)[1] = fm[i * img.cols + j][1];
// _iout(fm[i * img.cols + j][0]);
}
if (backwards)res *= 1.d / (float) (res.cols * res.rows);
fftw_free(p);
fftw_free(fm);
return res;
#else
*/
if (img.channels() == 1)
{
cv::Mat planes[] = {cv::Mat_<float> (img), cv::Mat_<float>::zeros(img.size())};
//cv::Mat planes[] = {cv::Mat_<double> (img), cv::Mat_<double>::zeros(img.size())};
cv::merge(planes, 2, img);
}
cv::dft(img, img, backwards ? (cv::DFT_INVERSE | cv::DFT_SCALE) : 0 );
return img;
/*#endif*/
}
cv::Mat real(cv::Mat img)
{
std::vector<cv::Mat> planes;
cv::split(img, planes);
return planes[0];
}
cv::Mat imag(cv::Mat img)
{
std::vector<cv::Mat> planes;
cv::split(img, planes);
return planes[1];
}
cv::Mat magnitude(cv::Mat img)
{
cv::Mat res;
std::vector<cv::Mat> planes;
cv::split(img, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
if (planes.size() == 1) res = cv::abs(img);
else if (planes.size() == 2) cv::magnitude(planes[0], planes[1], res); // planes[0] = magnitude
else assert(0);
return res;
}
cv::Mat complexMultiplication(cv::Mat a, cv::Mat b)
{
std::vector<cv::Mat> pa;
std::vector<cv::Mat> pb;
cv::split(a, pa);
cv::split(b, pb);
std::vector<cv::Mat> pres;
pres.push_back(pa[0].mul(pb[0]) - pa[1].mul(pb[1]));
pres.push_back(pa[0].mul(pb[1]) + pa[1].mul(pb[0]));
cv::Mat res;
cv::merge(pres, res);
return res;
}
cv::Mat complexDivision(cv::Mat a, cv::Mat b)
{
std::vector<cv::Mat> pa;
std::vector<cv::Mat> pb;
cv::split(a, pa);
cv::split(b, pb);
cv::Mat divisor = 1. / (pb[0].mul(pb[0]) + pb[1].mul(pb[1]));
std::vector<cv::Mat> pres;
pres.push_back((pa[0].mul(pb[0]) + pa[1].mul(pb[1])).mul(divisor));
pres.push_back((pa[1].mul(pb[0]) + pa[0].mul(pb[1])).mul(divisor));
cv::Mat res;
cv::merge(pres, res);
return res;
}
void rearrange(cv::Mat &img)
{
// img = img(cv::Rect(0, 0, img.cols & -2, img.rows & -2));
int cx = img.cols / 2;
int cy = img.rows / 2;
cv::Mat q0(img, cv::Rect(0, 0, cx, cy)); // Top-Left - Create a ROI per quadrant
cv::Mat q1(img, cv::Rect(cx, 0, cx, cy)); // Top-Right
cv::Mat q2(img, cv::Rect(0, cy, cx, cy)); // Bottom-Left
cv::Mat q3(img, cv::Rect(cx, cy, cx, cy)); // Bottom-Right
cv::Mat tmp; // swap quadrants (Top-Left with Bottom-Right)
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
q1.copyTo(tmp); // swap quadrant (Top-Right with Bottom-Left)
q2.copyTo(q1);
tmp.copyTo(q2);
}
/*
template < typename type>
cv::Mat fouriertransFull(const cv::Mat & in)
{
return fftd(in);
cv::Mat planes[] = {cv::Mat_<type > (in), cv::Mat_<type>::zeros(in.size())};
cv::Mat t;
assert(planes[0].depth() == planes[1].depth());
assert(planes[0].size == planes[1].size);
cv::merge(planes, 2, t);
cv::dft(t, t);
//cv::normalize(a, a, 0, 1, CV_MINMAX);
//cv::normalize(t, t, 0, 1, CV_MINMAX);
// cv::imshow("a",real(a));
// cv::imshow("b",real(t));
// cv::waitKey(0);
return t;
}*/
void normalizedLogTransform(cv::Mat &img)
{
img = cv::abs(img);
img += cv::Scalar::all(1);
cv::log(img, img);
// cv::normalize(img, img, 0, 1, CV_MINMAX);
}
}