-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpoisson.cpp
More file actions
executable file
·227 lines (183 loc) · 6.55 KB
/
poisson.cpp
File metadata and controls
executable file
·227 lines (183 loc) · 6.55 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
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
#include "poisson.h"
#include "fft.h"
#include <algorithm>
const float POURCENT = 0.5f;
// Fonction affine amenant les min et max de F (ignorant les POURCENT valeurs
// extremes de chaque cote) a 0 et 255.
void affineContraste(Image<float> F, float &a, float &b) {
F = F.clone();
sort(F.begin(), F.end());
int p = (int) (F.totalSize() * POURCENT / 100);
float min = F[p];
float max = F[F.totalSize() - 1 - p];
a = (min < max) ? 255.0f / (max - min) : 0;
b = (min < max) ? -a * min : 255.0f / 2;
}
// Renvoie une image affichable.
Image<byte> affichable(const Image<float> &F) {
float a, b;
affineContraste(F, a, b);
Image<byte> I(F.width(), F.height());
for (int i = 0; i < I.height(); i++)
for (int j = 0; j < I.width(); j++) {
float f = a * F(j, i) + b + 0.5f;
if (f < 0) f = 0;
if (f > 255) f = 255;
I(j, i) = (byte) f;
}
return I;
}
// Affichage d'une image avec intensites reelles.
void affiche(const Image<float> &F) {
display(affichable(F));
}
// Prend la partie reelle de chaque pixel d'une image complexe.
Image<float> realImage(const Image<complex<float> > &F) {
const int w = F.width(), h = F.height();
Image<float> I(w, h);
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
I(j, i) = F(j, i).real();
return I;
}
// Puissance de 2 immediatement superieure a i-1.
int puis2(int i) {
int j = 1;
while (j < i)
j *= 2;
return j;
}
// Genere une image plus grande en rajoutant des 0.
Image<complex<float> > agrandis(const Image<complex<float> > &I,
int w, int h) {
Image<complex<float> > I2(w, h);
I2.fill(0.0f);
for (int i = 0; i < I.height(); i++)
for (int j = 0; j < I.width(); j++)
I2(j, i) = I(j, i);
return I2;
}
Image<float> agrandis(const Image<float> &I,
int w, int h) {
Image<float> I2(w, h);
I2.fill(0.0f);
for (int i = 0; i < I.height(); i++)
for (int j = 0; j < I.width(); j++)
I2(j, i) = I(j, i);
return I2;
}
// Gradient de l'image I par differences finies.
void gradient(const Image<float> &I, Image<float> &Vx, Image<float> &Vy) {
Vx = Image<float>(I.width(), I.height());
Vy = Image<float>(I.width(), I.height());
FVector<float, 2> T;
for (int i = 0; i < I.width(); i++) {
for (int j = 0; j < I.height(); j++) {
T = gradient(I, Coords<2>(i, j));
Vx(i, j) = T[0];
Vy(i, j) = T[1];
}
}
}
// Calcul en Fourier de la derivee suivant x.
void Fourier_dx(Image<complex<float> > &F) {
fft2(F.data(), F.width(), F.height());
complex<float> u = polar<float>(1.0f, float(M_PI / 2));
for (int i = 0; i < F.width() / 2; i++) {
for (int j = 0; j < F.height(); j++) {
F(i, j) *= 2 * float(M_PI) * i * u * (1 / float(F.width()));
}
}
for (int j = 0; j < F.height(); j++) {
F(F.width() / 2, j) = 0;
}
for (int i = F.width() / 2 + 1; i < F.width(); i++) {
for (int j = 0; j < F.height(); j++) {
F(i, j) *= 2 * float(M_PI) * (i - F.width()) * u * (1 / float(F.width()));
}
}
}
// Derivee suivant x par DFT.
Image<float> dx(Image<complex<float> > F) {
F = F.clone();
int w = F.width();
int h = F.height();
int a = puis2(w);
int b = puis2(h);
F = agrandis(F, a, b);
Fourier_dx(F);
ifft2(F.data(), F.width(), F.height());
return realImage(F.getSubImage(0, 0, w, h));
}
// Calcul en Fourier de la derivee suivant y.
void Fourier_dy(Image<complex<float> > &F) {
fft2(F.data(), F.width(), F.height());
complex<float> u = polar<float>(1.0f, float(M_PI / 2));
for (int i = 0; i < F.height() / 2; i++) {
for (int j = 0; j < F.width(); j++) {
F(j, i) *= 2 * float(M_PI) * i * u * (1 / float(F.height()));
}
}
for (int j = 0; j < F.height(); j++) {
F(j, F.height() / 2) = 0;
}
for (int i = F.height() / 2 + 1; i < F.height(); i++) {
for (int j = 0; j < F.width(); j++) {
F(j, i) *= 2 * float(M_PI) * (i - F.height()) * u * (1 / float(F.height()));
}
}
}
// Derivee suivant y par DFT.
Image<float> dy(Image<complex<float> > F) {
F = F.clone();
int w = F.width();
int h = F.height();
int a = puis2(w);
int b = puis2(h);
F = agrandis(F, a, b);
Fourier_dy(F);
ifft2(F.data(), F.width(), F.height());
F.getSubImage(0, 0, w, h);
return realImage(F.getSubImage(0, 0, w, h));
}
// Resouds l'equation de Poisson.
Image<float> poisson(Image<complex<float> > Vx,
Image<complex<float> > Vy) {
int w = Vx.width();
int h = Vx.height();
int a = puis2(w);
int b = puis2(h);
Vx = agrandis(Vx, a, b);
Vy = agrandis(Vy, a, b);
Image<complex<float> > u(a, b);
Fourier_dx(Vx);
Fourier_dy(Vy);
for (int i = 0; i < b; ++i) {
for (int j = 0; j < a; ++j) {
if (i == 0 && j == 0)
u(j, i) = 0;
else {
if (i < b / 2 && j < a / 2)
u(j, i) = (Vx(j, i) + Vy(j, i)) / complex<float>(
-(4 * pow(M_PI, 2)) * ((float(j) / a) * (float(j) / a) + (float(i) / b) * (float(i) / b)),
0);
if (i < b / 2 && j > a / 2)
u(j, i) = (Vx(j, i) + Vy(j, i)) / complex<float>(-(4 * pow(M_PI, 2)) *
(((float(j - a)) / a) * ((float(j - a)) / a) +
(float(i) / b) * (float(i) / b)), 0);
if (i > b / 2 && j < a / 2)
u(j, i) = (Vx(j, i) + Vy(j, i)) / complex<float>(-(4 * pow(M_PI, 2)) *
((float(j) / a) * (float(j) / a) +
(float(i - b) / b) * (float(i - b) / b)), 0);
if (i > b / 2 && j > a / 2)
u(j, i) = (Vx(j, i) + Vy(j, i)) / complex<float>(-(4 * pow(M_PI, 2)) *
((float(j - a) / a) * (float(j - a) / a) +
(float(i - b) / b) * (float(i - b) / b)), 0);
if (i == b / 2 || j == a / 2)
u(j, i) = 0;
}
}
}
ifft2(u.data(), u.width(), u.height());
return realImage(u.getSubImage(0, 0, w, h));
}