Skip to content

Commit 3499538

Browse files
committed
split bregman post
1 parent cd69d4b commit 3499538

File tree

12 files changed

+196
-5
lines changed

12 files changed

+196
-5
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: Functional derivative
3+
tags: [math, statics]
4+
style: fill
5+
color: success
6+
description: How a function of functions changes with respect to its functions
7+
---
8+
9+
![image](./image.png)
10+
11+
# Introduction
12+
13+
Mucha gente está familiarizada con el concepto de derivada de una función. Menos lo están con el de derivada de una función de funciones, o derivada de un funcional.
14+
15+
BLABLABLA
16+
17+
# Remark
18+
19+
Demostrar lo de que derivada funcional de operador diferencial lineal (como gradiente) es el propio operador. (libreta cin 11/02/25)
20+
21+
# TODO
22+
23+
Citar este artículo en el paso 3.1. de post de split bregman.

_posts/XXXX-XX-XX-split-bregman.md

Lines changed: 173 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ where $$\langle p, u - v \rangle$$ is the inner product between $$p$$ (which bel
3737

3838
![bd](../assets/blog_images/XXXX-XX-XX-split-bregman/bd.jpg)
3939

40-
$$
41-
D^p_J (u, v)
42-
$$
43-
44-
compares the value $$J(u)$$ with the tangent plane (which in 1D is a line) $$J(v) + \langle p, u-v \rangle$$. Choosing a differentiable $$H$$, the subdifferential becomes the gradient $$\nabla H$$. This is not strictly a distance in the usual sense because it satisfies neither symmetry nor the triangle inequality, but it retains many distance properties (see [8]). Instead of directly measuring the distance between two points, it measures it as differences in function values, comparing $$J(u)$$ with a linear approximation of $$J$$ based on its tangent point $$J(v)$$. In other words, it measures the difference between the function value at $$u$$, which is $$J(u)$$, and the best linear approximation of $$J(u)$$ from $$v$$.
40+
$$ D^p_J (u, v) $$ compares the value $$J(u)$$ with the tangent plane (which in 1D is a line) $$J(v) + \langle p, u-v \rangle$$. Choosing a differentiable $$H$$, the subdifferential becomes the gradient $$\nabla H$$. This is not strictly a distance in the usual sense because it satisfies neither symmetry nor the triangle inequality, but it retains many distance properties (see [8]). Instead of directly measuring the distance between two points, it measures it as differences in function values, comparing $$J(u)$$ with a linear approximation of $$J$$ based on its tangent point $$J(v)$$. In other words, it measures the difference between the function value at $$u$$, which is $$J(u)$$, and the best linear approximation of $$J(u)$$ from $$v$$.
4541

4642
From the figure, it is clear that convexity is required for an effective linear approximation. The distance tends to zero when $$v$$ approaches the optimum $$\hat{u}$$. So, given an initial point $$u^0$$ and a parameter $$\gamma > 0$$, the Bregman iteration algorithm is formally:
4743

@@ -170,6 +166,174 @@ In [14], the explained approach is used for deconvolution, and instead of keepin
170166
</table>
171167
</p>
172168

169+
## Denoising
170+
171+
We will implement and test the Total Variation (TV) model proposed in [15] using the Split Bregman method in C++. See [16] for another interesting implementation.
172+
173+
### 1. Total Variation Model Functional
174+
175+
The combination of an L2 data fidelity term with **total variation (TV)** leads to a variational model proposed by Rudin, Osher, and Fatemi (ROF model). The functional is expressed as:
176+
177+
$$
178+
E(u) = \frac{1}{2} \int_\Omega (u - f)^2 + \alpha \int |\nabla u|
179+
$$
180+
181+
### 2. Split Bregman for the TV Model
182+
183+
To minimize this functional, we introduce an auxiliary variable $$ w = (w_1, w_2) $$ and a Bregman iterative parameter $$ b = (b_1, b_2) $$, transforming the original functional into:
184+
185+
$$
186+
E(u, w, b) = \frac{1}{2} \int (u - f)^2 + \alpha \int |w|^2 + \beta \int (w - \nabla u - b)^2
187+
$$
188+
189+
To solve this functional, we use an alternating optimization method, where each iteration alternates between updating $$ u $$ and $$ w $$.
190+
191+
### 3. Algorithm and Discretized Equations
192+
193+
The authors provide a pseudocode representation of the algorithm, which closely resembles the previously presented implementation:
194+
195+
![sbtv](../assets/blog_images/XXXX-XX-XX-split-bregman/sbtv.png)
196+
197+
#### 3.1. Updating $$ u $$
198+
199+
For $$ u $$, the Euler equation is obtained as:
200+
201+
$$
202+
\frac{\partial u}{\partial t} = \nabla \cdot (\nabla u) = f - \nabla \cdot (w - b)
203+
$$
204+
205+
This equation is discretized using finite differences and the Discrete Fourier Transform (DFT), applying the Fast Fourier Transform (FFT):
206+
207+
$$
208+
u_{i,j} = \mathcal{F}^{-1} \left( \frac{\mathcal{F}(f - \nabla \cdot (w - b))}{G_{i,j}} \right)
209+
$$
210+
211+
where $$ G_{i,j} $$ is a precomputed matrix related to the image frequencies:
212+
213+
```cpp
214+
auto computeCosineMatrix = [](int size, float scale, bool trueForRows) -> cv::Mat {
215+
cv::Mat indices(size, 1, CV_32F);
216+
for (int i = 0; i < size; ++i)
217+
indices.at<float>(i, 0) = static_cast<float>(i);
218+
if (!trueForRows)
219+
indices = indices.t();
220+
cv::Mat cos_mat;
221+
cv::multiply(indices, scale, cos_mat);
222+
cos_mat.forEach<float>([](float& pixel, const int*) { pixel = std::cos(pixel); });
223+
224+
return cos_mat;
225+
};
226+
cv::Mat cos_i = computeCosineMatrix(m, 2 * CV_PI / m, true);
227+
cv::Mat cos_j = computeCosineMatrix(n, 2 * CV_PI / n, false);
228+
cv::Mat G = repeat(cos_i, 1, n) + repeat(cos_j, m, 1) - 2;
229+
```
230+
231+
Thus, the update of $$ u $$ is performed in the frequency (Fourier) domain:
232+
```cpp
233+
// Update u using FFT
234+
Mat w1_b1, w2_b2;
235+
subtract(w1, b1, w1_b1);
236+
subtract(w2, b2, w2_b2);
237+
div_w_b = Bx(w1_b1) + By(w2_b2);
238+
multiply(theta, div_w_b, g);
239+
subtract(f0, g, g);
240+
241+
// FFT processing
242+
dft(g, g_fft, DFT_COMPLEX_OUTPUT);
243+
divide(g_fft, denominator_complex, g_fft);
244+
dft(g_fft, u, DFT_INVERSE | DFT_SCALE | DFT_REAL_OUTPUT);
245+
```
246+
247+
It should be mentioned that a padding of 10 pixels is added to avoid artifacts in the boundary during the filtering process:
248+
249+
```cpp
250+
// Clone and add padding
251+
Mat f0 = inputImage.clone();
252+
int padNum = 10;
253+
copyMakeBorder(f0, f0, padNum, padNum, padNum, padNum, BORDER_REPLICATE);
254+
```
255+
256+
In the end, the useful (original) subset of the image is cropped again in the spatial domain.
257+
Al final, se vuelve a recortar en el dominio espacial el subconjunto útil (original) de la imagen.
258+
259+
```cpp
260+
Mat result;
261+
u(Rect(padNum, padNum, inputImage.cols, inputImage.rows)).copyTo(result);
262+
```
263+
264+
#### 3.2. Updating $$ w $$
265+
266+
Once $$ u $$ is updated, $$ w $$ is updated using the **soft-thresholding** equation:
267+
268+
$$
269+
w_{i,j} = \max\left(\left|\nabla u_{i,j} + b_{i,j}\right| - \frac{\alpha}{\theta}, 0 \right)
270+
$$
271+
272+
Here, the value of $$ w $$ is computed component-wise using the gradient of $$ u $$ and the Bregman variables $$ b = (b_1, b_2)^ $$.
273+
274+
```cpp
275+
// Update w with soft-thresholding
276+
Mat ux = Fx(u), uy = Fy(u);
277+
Mat c1, c2;
278+
add(ux, b1, c1);
279+
add(uy, b2, c2);
280+
281+
Mat abs_c;
282+
magnitude(c1, c2, abs_c);
283+
abs_c += 1e-10; // Avoid division by zero
284+
285+
Mat thresholded;
286+
threshold(abs_c - lambda/theta, thresholded, 0, 0, THRESH_TOZERO);
287+
288+
multiply(thresholded, c1 / abs_c, w1);
289+
multiply(thresholded, c2 / abs_c, w2);
290+
```
291+
292+
#### 3.3 Updating $$ b $$
293+
Finally, we update the Bregman iterative parameter b, which serves as an error adjustment term in each iteration to reinforce the constraint $$ w = \nabla u $$:
294+
295+
$$
296+
b \leftarrow b + \nabla u - w
297+
$$
298+
299+
```cpp
300+
// Update Bregman variables
301+
subtract(c1, w1, b1);
302+
subtract(c2, w2, b2);
303+
```
304+
305+
### Results
306+
307+
We conclude by showing some original, noisy, and denoised versions of various images using Gaussian and impulsive noise to degrade them. The model parameters are sensibly modulated by hand, proportionally to the level of noise generated:
308+
309+
<p align="center">
310+
<table>
311+
<tr>
312+
<th>Original</th>
313+
<th>Degraded</th>
314+
<th>Restored</th>
315+
</tr>
316+
<tr>
317+
<td class="image-cell"><img src="../assets/blog_images/XXXX-XX-XX-split-bregman/input1.png" alt="input1" style="width: 400px; height: 300px;" /></td>
318+
<td class="image-cell"><img src="../assets/blog_images/XXXX-XX-XX-split-bregman/noisy1.png" alt="noisy1" style="width: 400px; height: 300px;" /></td>
319+
<td class="image-cell"><img src="../assets/blog_images/XXXX-XX-XX-split-bregman/denoised1.png" alt="denoised1" style="width: 400px; height: 300px;" /></td>
320+
</tr>
321+
<tr>
322+
<td class="image-cell"><img src="../assets/blog_images/XXXX-XX-XX-split-bregman/input2_lena.png" alt="input2_lena" style="width: 400px; height: 300px;" /></td>
323+
<td class="image-cell"><img src="../assets/blog_images/XXXX-XX-XX-split-bregman/noisy2.png" alt="noisy2" style="width: 400px; height: 300px;" /></td>
324+
<td class="image-cell"><img src="../assets/blog_images/XXXX-XX-XX-split-bregman/denoised2.png" alt="denoised2" style="width: 400px; height: 300px;" /></td>
325+
</tr>
326+
<tr>
327+
<td class="image-cell"><img src="../assets/blog_images/XXXX-XX-XX-split-bregman/input3_mouth.png" alt="input3_mouth" style="width: 400px; height: 300px;" /></td>
328+
<td class="image-cell"><img src="../assets/blog_images/XXXX-XX-XX-split-bregman/noisy3.png" alt="noisy3" style="width: 400px; height: 300px;" /></td>
329+
<td class="image-cell"><img src="../assets/blog_images/XXXX-XX-XX-split-bregman/denoised3.png" alt="denoised3" style="width: 400px; height: 300px;" /></td>
330+
</tr>
331+
332+
</table>
333+
</p>
334+
335+
Probably a data fidelity modeled under the L1 norm would have dealt better with the impulsive noise, but we'll leave that for another post.
336+
173337
## References
174338
175339
[1] Pascal Getreuer, *Total Variation Inpainting using Split Bregman*, [Online Article](http://www.ipol.im/pub/art/2012/g-tvi/)
@@ -199,3 +363,7 @@ In [14], the explained approach is used for deconvolution, and instead of keepin
199363
[13] E. Esser, *Applications of Lagrangian-Based Alternating Direction Methods and Connections to Split Bregman*, UCLA CAM Report 09-21, 2009, [FTP Link](ftp://ftp.math.ucla.edu/pub/camreport/cam09-31.pdf)
200364
201365
[14] Weihong Li et al., *Total Variation Blind Deconvolution Employing Split Bregman Iteration*, 2012, [Online Article](https://www.ipol.im/pub/art/2012/g-tvdc/)
366+
367+
[15] Lu, W., Duan, J., Qiu, Z., Pan, Z., Liu, R. W., & Bai, L. (2016). Implementation of high-order variational models made easy for image processing. Mathematical Methods in the Applied Sciences, 39(18), 5371–5387. https://doi.org/10.1002/mma.3858
368+
369+
[16] [GitHub - L1-norm-total-variation-inpainting-using-Split_Bregman-Iteration](https://github.com/ycynyu007/L1-norm-total-variation-inpainting-using-Split_Bregman-Iteration)
244 KB
Loading
704 KB
Loading
1.22 MB
Loading
192 KB
Loading
199 KB
Loading
746 KB
Loading
192 KB
Loading
1.24 MB
Loading

0 commit comments

Comments
 (0)