-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimdiff_ipol.cpp
169 lines (136 loc) · 4.56 KB
/
imdiff_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
/*
* Copyright 2009-2015 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/>.
*/
/**
* - Visualize the difference between two images in such a way that the error
* range [-4*sigma, 4*sigma] is linearly transformed to [0, 255] for
* visualization purposes. Errors outside this range are saturated to 0
* and 255, respectively.
* - Compute the Root Mean Squared Error :
* @f$ \text{RMSE} = \sqrt{\frac{\sum_{c=1}^{\#\text{channels}}
* \sum_{i=1}^{\#\text{pixels}} (u[c][i] - v[c][i])^2}{\#\text{channels}
* \cdot\#\text{pixels}}} @f$.
* - Compute the Peak Signal-to-Noise Ratio :
* @f$ \text{PNSR} = 10 \log \dfrac{255^2}{\text{RMSE}^2} @f$.
*
* README.txt:
* @verbinclude README.txt
*/
/**
* @file imdiff_ipol.cpp
* @brief Main executable file
*
* @author Joan Duran <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include "io_png.h"
// Usage: imdiff_ipol image1.png image2.png imdiff.png sigma
int main(int argc, char **argv)
{
if(argc < 5)
{
printf("usage: imdiff_ipol image1.png image2.png imdiff.png sigma\n");
printf("image1.png :: first image.\n");
printf("image2.png :: second image.\n");
printf("imdiff.png :: difference image.\n");
printf("sigma :: noise standard deviation.\n");
return EXIT_FAILURE;
}
// Read first input
size_t nx, ny, nc;
float *d_v = NULL;
d_v = io_png_read_f32(argv[1], &nx, &ny, &nc);
if (!d_v)
{
fprintf(stderr, "Error - %s not found or not a correct png image.\n", argv[1]);
return EXIT_FAILURE;
}
if (nc == 2) // We do not use the alpha channel if grayscale image
nc = 1;
if (nc > 3) // We do not use the alpha channel if RGB image
nc = 3;
// Read second input
size_t nx2, ny2, nc2;
float *d_v2 = NULL;
d_v2 = io_png_read_f32(argv[2], &nx2, &ny2, &nc2);
if (!d_v2)
{
fprintf(stderr, "Error - %s not found or not a correct png image.\n",
argv[2]);
return EXIT_FAILURE;
}
if (nc2 == 2) // We do not use the alpha channel if grayscale image
nc2 = 1;
if (nc2 > 3) // We do not use the alpha channel if RGB image
nc2 = 3;
if (nx != nx2 || ny != ny2)
{
// Check if both images have same size
fprintf(stderr, "Error - %s and %s sizes don't match.\n", argv[1],
argv[2]);
return EXIT_FAILURE;
}
if (nc != nc2)
{
// Check if both images have same number of channels
fprintf(stderr, "Error - %s and %s channels don't match.\n", argv[1],
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_wh;
// Compute RMSE and PSNR
float fRMSE = 0.0f;
float fPSNR = 0.0f;
for(int c = 0; c < d_c ; c++)
for(int i = 0; i < d_wh; i++)
{
float dif = d_v[c*d_wh+i] - d_v2[c*d_wh+i];
fRMSE += dif * dif;
}
fRMSE = fRMSE / (float)d_whc;
fPSNR = 10.0f * log10f(255.0f * 255.0f / fRMSE);
fRMSE = sqrtf(fRMSE);
printf("RMSE: %2.2f\n", fRMSE);
printf("PSNR: %2.2f\n", fPSNR);
// Compute image difference. Convert from [-4*sigma, 4*sigma] to [0,255]
float sigma = atof(argv[4]);
sigma *= 4.0f;
float *difference = new float[d_whc];
for(int i = 0; i < d_whc; i++)
{
float value = d_v[i] - d_v2[i];
value = (value + sigma) * 255.0f / (2.0f * sigma);
if (value < 0.0)
value = 0.0f;
if (value > 255.0)
value = 255.0f;
difference[i] = value;
}
// Save difference image
if (io_png_write_f32(argv[3], difference, (size_t) d_w, (size_t) d_h,
(size_t) d_c) != 0)
fprintf(stderr, "Error - Failed to save png image %s.\n", argv[3]);
// Free memory
delete[] difference;
return EXIT_SUCCESS;
}