-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilter.cpp
More file actions
183 lines (175 loc) · 5.04 KB
/
Filter.cpp
File metadata and controls
183 lines (175 loc) · 5.04 KB
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
#include "Filter.h"
#include "assert.h"
#include<math.h>
const double pi = 3.14159265;
const double e = 2.718281828459045;
void fftshift2D(cx_fmat* data, size_t xdim, size_t ydim);
void ifftshift2D(cx_fmat* data, size_t xdim, size_t ydim);
arma::fmat cx_abs(cx_fmat matrix);
arma::fmat real(arma::cx_fmat c_mat);
Filter::Filter()
{
}
void Filter::addData(fcube* img)
{
// ´íÎó¼ì²é
assert(rowHeightY > 1 && colWidthX > 1);
this->img = img;
}
Mask::Mask(string maskType, int rowHeightY, int colWidthX)
{
int cy = rowHeightY / 2;
int cx = colWidthX / 2;
maskArray = new cx_fmat(rowHeightY, colWidthX);
if (maskType == "Threshold")
{
float threshold = 0.5;
for (int r = 0; r < rowHeightY; r++) {
for (int c = 0; c < colWidthX; c++) {
if (sqrt((r - cy) * (r - cy) + (cx - c) * (cx - c)) <= (rowHeightY+colWidthX)/3) {
maskArray->at(r, c) = 1;
}
else {
maskArray->at(r, c) = 0;
}
}
}
}
else if (maskType == "Gaussian")
{
double sig_square = 1;
double base = sqrt(2 * pi) * sig_square;
for (int r = 0; r < rowHeightY; r++) {
for (int c = 0; c < colWidthX; c++) {
double x = double(cx - c) / double(cx);
double y = double(cy - r) / double(cy);
x = x * x; y = y * y;
double p = pow(e, -(x + y) / (2 * sig_square));
maskArray->at(r, c) = p / base;
}
}
}
}
Mask::~Mask()
{
delete maskArray;
}
void Mask::domask(cx_fmat& shift)
{
for (int r = 0; r < shift.n_rows; r++) {
for (int c = 0; c < shift.n_cols; c++) {
shift.at(r, c) = shift.at(r, c) * maskArray->at(r, c);
}
}
}
bool Filter::execute(string option, string maskType)
{
if (option == "hpfilter")
{
result = arma::fcube(img->n_rows, img->n_cols, img->n_slices);
for (int s = 0; s < img->n_slices; s++) {
cx_fmat img_fft = arma::fft2(img->slice(s));
fftshift2D(&img_fft, img->n_rows, img->n_cols);
Mask m(maskType, img->n_rows, img->n_cols);
m.domask(img_fft);
ifftshift2D(&img_fft, img->n_rows, img->n_cols);
cx_fmat img_ifft = ifft2(img_fft);
result.slice(s) = cx_abs(img_ifft);
return true;
}
}
else if (option == "lpfilter")
{
return false;
}
else
{
return false;
}
}
Filter::~Filter()
{
//delete img;
}
void fftshift2D(cx_fmat* data, size_t xdim, size_t ydim)
{
size_t xshift = xdim / 2;
size_t yshift = ydim / 2;
if ((xdim * ydim) % 2 != 0) {
std::vector<cx_float> out;
out.resize(xdim * ydim);
for (size_t x = 0; x < xdim; x++) {
size_t outX = (x + xshift) % xdim;
for (size_t y = 0; y < ydim; y++) {
size_t outY = (y + yshift) % ydim;
out[outX + xdim * outY] = data->at(x, y);
}
}
copy(out.begin(), out.end(), &data[0]);
}
else {
for (size_t x = 0; x < xdim; x++) {
size_t outX = (x + xshift) % xdim;
for (size_t y = 0; y < yshift; y++) {
size_t outY = (y + yshift) % ydim;
swap(data->at(outX, outY), data->at(x, y));
//swap(data[outX + xdim * outY], data[x + xdim * y]);
}
}
}
}
void ifftshift2D(cx_fmat* data, size_t xdim, size_t ydim)
{
size_t xshift = xdim / 2;
if (xdim % 2 != 0) {
xshift++;
}
size_t yshift = ydim / 2;
if (ydim % 2 != 0) {
yshift++;
}
if ((xdim * ydim) % 2 != 0) {
std::vector<cx_float > out;
out.resize(xdim * ydim);
for (size_t x = 0; x < xdim; x++) {
size_t outX = (x + xshift) % xdim;
for (size_t y = 0; y < ydim; y++) {
size_t outY = (y + yshift) % ydim;
out[outX + xdim * outY] = data->at(x, y);
}
}
copy(out.begin(), out.end(), &data[0]);
}
else {
for (size_t x = 0; x < xdim; x++) {
size_t outX = (x + xshift) % xdim;
for (size_t y = 0; y < yshift; y++) {
size_t outY = (y + yshift) % ydim;
swap(data->at(outX, outY), data->at(x, y));
//swap(data[outX + xdim * outY], data[x + xdim * y]);
}
}
}
}
arma::fmat real(arma::cx_fmat c_mat)
{
arma::fmat mat(c_mat.n_rows, c_mat.n_cols);
for (int r = 0; r < c_mat.n_rows; r++) {
for (int c = 0; c < c_mat.n_cols; c++) {
mat.at(r, c) = c_mat.at(r, c).real();
}
}
return mat;
}
arma::fmat cx_abs(cx_fmat matrix)
{
arma::fmat result(matrix.n_rows, matrix.n_cols);
for (int r = 0; r < matrix.n_rows; r++) {
for (int c = 0; c < matrix.n_cols; c++) {
float real_val = matrix.at(r, c).real();
float img_val = matrix.at(r, c).imag();
result.at(r, c) = sqrt(real_val * real_val + img_val * img_val);
}
}
return result;
}