-
Notifications
You must be signed in to change notification settings - Fork 0
/
chambolle_ipol.cpp
193 lines (158 loc) · 5.38 KB
/
chambolle_ipol.cpp
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
/*
* Copyright 2009-2013 IPOL Image Processing On Line http://www.ipol.im/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @mainpage Chambolle's projection algorithm for TV-denoising
*
* README.txt:
* @verbinclude README.txt
*/
/**
* @file chambolle_ipol.cpp
* @brief Main executable file
*
* @author Joan Duran <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libdenoising.h"
#include "libauxiliar.h"
#include "io_png.h"
// Usage: chambolle_ipol option input.png sigma lambda noisy.png denoised.png
int main(int argc, char **argv)
{
if(argc < 7)
{
printf("usage: chambolle_ipol option input.png sigma lambda noisy.png "
"denoised.png\n");
printf("option :: 1 :: Add noise to the input image and then "
"denoise with dynamic lambda.\n"
" - Sigma required from user.\n"
" - Lambda is set to 0.\n"
" 2 :: Add noise to the input image and then "
"denoise with fixed lambda.\n"
" - Sigma required from user.\n"
" - Lambda required from user.\n"
" 3 :: Denoise the input image with dynamic "
"lambda.\n"
" - Sigma required from user.\n"
" - Lambda is set to 0.\n"
" - noisy = input.\n"
" 4 :: Denoise the input image with fixed "
"lambda.\n"
" - Sigma required from user.\n"
" - Lambda required from user.\n"
" - noisy = input.\n");
printf("input.png :: input image.\n");
printf("sigma :: noise standard deviation.\n");
printf("lambda :: trade-off parameter.\n");
printf("noisy.png :: noisy image.\n");
printf("denoised.png :: denoised image by Chambolle's algorithm.\n");
return EXIT_FAILURE;
}
// Read option
int option = atoi(argv[1]);
if(option != 1 && option != 2 && option != 3 && option != 4)
{
fprintf(stderr, "Error - option must be 1, 2, 3 or 4.\n");
return EXIT_FAILURE;
}
// Read input image
size_t nx, ny, nc;
float *d_v = NULL;
d_v = io_png_read_f32(argv[2], &nx, &ny, &nc);
if(!d_v)
{
fprintf(stderr, "Error - %s not found or not a correct png image.\n",
argv[2]);
return EXIT_FAILURE;
}
// Image variables
int d_w = (int) nx;
int d_h = (int) ny;
int d_c = (int) nc;
int d_wh = d_w * d_h;
int d_whc = d_c * d_w * d_h;
if(d_c == 2) // We do not use the alpha channel
d_c = 1;
if(d_c > 3) // We do not use the alpha channel
d_c = 3;
// Parameters
float sigma = atof(argv[3]);
if(sigma < 0.0f)
{
fprintf(stderr, "Error - %s must be nonnegative.\n", argv[3]);
return EXIT_FAILURE;
}
float lambda = 0.0f;
if(option == 2 || option == 4)
lambda = atof(argv[4]);
if(lambda < 0.0f)
{
fprintf(stderr, "Error - %s must be nonnegative.\n", argv[4]);
return EXIT_FAILURE;
}
// Add noise to input image if required
float *noisy;
if(option == 1 || option == 2)
{
printf("Add noise to the input image and then denoise.\n");
noisy = new float[d_whc];
for(int i = 0; i < d_c; i++)
fiAddNoise(&d_v[i*d_wh], &noisy[i*d_wh], sigma, i, d_wh);
} else
{
printf("Denoise the input image.\n");
noisy = d_v;
}
// TV-denoising
float **input = new float*[d_c];
float **output = new float*[d_c];
for(int c = 0; c < d_c; c++)
{
input[c] = &noisy[c*d_wh];
output[c] = new float[d_wh];
}
if(TVdenoising(option, output, input, sigma, lambda, d_c, d_w, d_h) != 1)
return EXIT_FAILURE;
// Save noisy and denoised as png images
if(option == 1 || option == 2)
if(io_png_write_f32(argv[5], noisy, (size_t) d_w, (size_t) d_h,
(size_t) d_c) != 0)
fprintf(stderr, "Error - Failed to save png image %s.\n", argv[5]);
float *denoised_png = new float[d_whc];
int k = 0;
for(int c = 0; c < d_c; c++)
for(int i = 0; i < d_wh; i++)
{
denoised_png[k] = output[c][i];
k++;
}
if(io_png_write_f32(argv[6], denoised_png, (size_t) d_w, (size_t) d_h,
(size_t) d_c) != 0)
fprintf(stderr, "Error - Failed to save png image %s.\n", argv[6]);
// Free memory
free(d_v);
delete[] input;
for(int c = 0; c < d_c; c++)
delete[] output[c];
delete[] output;
delete[] denoised_png;
if(option == 1 || option == 2)
delete[] noisy;
return EXIT_SUCCESS;
}