42 #define DISPLAY_SCALING 255
47 #define MSSIM_C1 (MSSIM_K1*MSSIM_K1)
48 #define MSSIM_C2 (MSSIM_K2*MSSIM_K2)
52 typedef enum {DEFAULT_METRICS, MAX_METRIC, MSE_METRIC, RMSE_METRIC,
53 PSNR_METRIC, MSSIM_METRIC}
metric;
80 int Width,
int Height,
int NumChannels,
float D);
81 void BasicMetrics(
float *Max,
float *Mse,
const float *A,
const float *B,
82 int Width,
int Height,
int NumChannels,
int Pad);
84 int Width,
int Height,
int NumChannels,
int Pad);
90 printf(
"Image difference calculator, P. Getreuer 2010-2011\n\n");
91 printf(
"Usage: imdiff [options] <exact file> <distorted file>\n\n"
94 printf(
" -m <metric> Metric to use for comparison, choices are\n");
95 printf(
" max Maximum absolute difference, max_n |A_n - B_n|\n");
96 printf(
" mse Mean squared error, 1/N sum |A_n - B_n|^2\n");
97 printf(
" rmse Root mean squared error, (MSE)^1/2\n");
98 printf(
" psnr Peak signal-to-noise ratio, -10 log10(MSE/255^2)\n");
99 printf(
" mssim Mean structural similarity index\n\n");
100 printf(
" -s Compute metric separately for each channel\n");
101 printf(
" -p <pad> Remove a margin of <pad> pixels before comparison\n");
102 printf(
" -D <number> D parameter for difference image\n\n");
103 #ifdef LIBJPEG_SUPPORT
104 printf(
" -q <number> Quality for saving JPEG images (0 to 100)\n\n");
106 printf(
"Alternatively, a difference image is generated by the syntax\n"
107 " imdiff [-D <number>] <exact file> <distorted file> <output file>\n\n");
108 printf(
"The difference image is computed as\n"
109 " D_n = 255/2 ((A_n - B_n)/D + 1).\n"
110 "Values outside of the range [0,255] are saturated.\n\n");
112 #ifdef LIBPNG_SUPPORT
113 " imdiff -mpsnr frog-exact.png frog-4x.bmp\n");
115 " imdiff -mpsnr frog-exact.bmp frog-4x.bmp\n");
120 int main(
int argc,
char *argv[])
127 } A = {NULL, 0, 0}, B = {NULL, 0, 0};
130 float Max, MaxC[3], Mse, MseC[3], Mssim;
131 int Channel, Status = 1;
138 if(!(A.Data = (
float *)
ReadImage(&A.Width, &A.Height, Param.
FileA,
139 IMAGEIO_FLOAT | IMAGEIO_RGB | IMAGEIO_PLANAR)))
143 if(!(B.Data = (
float *)
ReadImage(&B.Width, &B.Height, Param.
FileB,
144 IMAGEIO_FLOAT | IMAGEIO_RGB | IMAGEIO_PLANAR)))
147 if(A.Width != B.Width || A.Height != B.Height)
149 ErrorMessage(
"Image sizes don't match, %dx%d vs. %dx%d.\n",
150 A.Width, A.Height, B.Width, B.Height);
153 else if(A.Width <= 2*Param.
Pad || A.Height <= 2*Param.
Pad)
156 "Removal of %d-pixel padding removes entire %dx%d image.\n",
157 Param.
Pad, A.Width, A.Height);
166 IMAGEIO_FLOAT | IMAGEIO_RGB | IMAGEIO_PLANAR, Param.
JpegQuality)))
174 for(Channel = 0; Channel < 3; Channel++)
177 A.Data + Channel*A.Width*A.Height,
178 B.Data + Channel*B.Width*B.Height,
179 A.Width, A.Height, 1, Param.
Pad);
181 if(MaxC[Channel] > Max)
184 Mse += MseC[Channel];
191 case DEFAULT_METRICS:
195 printf(
"Peak signal-to-noise ratio: %.4f\n", -10*log10(Mse));
199 printf(
"Maximum absolute difference: %g %g %g\n",
202 printf(
"Peak signal-to-noise ratio: %.4f %.4f %.4f\n",
203 -10*log10(MseC[0]), -10*log10(MseC[1]), -10*log10(MseC[2]));
206 if(A.Width <= 2*(5 + Param.
Pad)
207 || A.Height <= 2*(5 + Param.
Pad))
208 printf(
"Image size is too small to compute MSSIM.\n");
213 A.Width, A.Height, 3, Param.
Pad);
216 printf(
"Mean structural similarity: %.4f\n", Mssim);
230 printf(
"%.4f %.4f %.4f\n",
239 printf(
"%.4f %.4f %.4f\n",
246 printf(
"%.4f\n", -10*log10(Mse));
248 printf(
"%.4f %.4f %.4f\n",
249 -10*log10(MseC[0]), -10*log10(MseC[1]), -10*log10(MseC[2]));
252 if(A.Width <= 2*(5 + Param.
Pad)
253 || A.Height <= 2*(5 + Param.
Pad))
254 printf(
"Image size is too small to compute MSSIM.\n");
258 A.Width, A.Height, 3, Param.
Pad);
263 printf(
"%.4f\n", Mssim);
279 int Width,
int Height,
int NumChannels,
float D)
281 const int NumEl = NumChannels*Width*Height;
286 for(n = 0; n < NumEl; n++)
287 A[n] = (A[n] - B[n])/D + (float)0.5;
292 void BasicMetrics(
float *Max,
float *Mse,
const float *A,
const float *B,
293 int Width,
int Height,
int NumChannels,
int Pad)
295 float Diff, CurMax = 0;
297 int x, y, Channel, n;
300 for(Channel = 0; Channel < NumChannels; Channel++)
301 for(y = Pad; y < Height - Pad; y++)
302 for(x = Pad; x < Width - Pad; x++)
304 n = x + Width*(y + Height*Channel);
305 Diff = (float)fabs(A[n] - B[n]);
310 AccumMse += Diff*Diff;
314 *Mse = (float)(AccumMse / (NumChannels*(Width - 2*Pad)*(Height - 2*Pad)));
320 int Width,
int Height,
int NumChannels,
int Pad)
328 const int NumPixels = Width*Height;
329 const int NumEl = NumChannels*NumPixels;
330 float *Buffer = NULL, *MuA = NULL, *MuB = NULL,
331 *MuAA = NULL, *MuBB = NULL, *MuAB = NULL;
332 double MuASqr, MuBSqr, MuAMuB,
333 SigmaASqr, SigmaBSqr, SigmaAB, Mssim = -1;
338 || !(Buffer = (
float *)
Malloc(
sizeof(
float)*NumPixels))
339 || !(MuA = (
float *)
Malloc(
sizeof(
float)*NumEl))
340 || !(MuB = (
float *)
Malloc(
sizeof(
float)*NumEl))
341 || !(MuAA = (
float *)
Malloc(
sizeof(
float)*NumEl))
342 || !(MuBB = (
float *)
Malloc(
sizeof(
float)*NumEl))
343 || !(MuAB = (
float *)
Malloc(
sizeof(
float)*NumEl)))
347 Boundary, Width, Height, NumChannels);
349 Boundary, Width, Height, NumChannels);
351 for(n = 0; n < NumEl; n++)
359 Boundary, Width, Height, NumChannels);
361 Boundary, Width, Height, NumChannels);
363 Boundary, Width, Height, NumChannels);
368 for(c = 0; c < NumChannels; c++)
369 for(y = Pad; y < Height - Pad; y++)
370 for(x = Pad; x < Width - Pad; x++)
372 n = x + Width*(y + Height*c);
373 MuASqr = MuA[n]*MuA[n];
374 MuBSqr = MuB[n]*MuB[n];
375 MuAMuB = MuA[n]*MuB[n];
376 SigmaASqr = MuAA[n] - MuASqr;
377 SigmaBSqr = MuBB[n] - MuBSqr;
378 SigmaAB = MuAB[n] - MuAMuB;
380 Mssim += ((2*MuAMuB + MSSIM_C1)*(2*SigmaAB + MSSIM_C2))
381 / ((MuASqr + MuBSqr + MSSIM_C1)
382 *(SigmaASqr + SigmaBSqr + MSSIM_C2));
385 Mssim /= NumChannels*(Width - 2*Pad)*(Height - 2*Pad);
416 Param->
Metric = DEFAULT_METRICS;
424 for(i = 1; i < argc;)
426 if(argv[i] && argv[i][0] ==
'-')
428 if((OptionChar = argv[i][1]) == 0)
435 OptionString = &argv[i][2];
437 OptionString = argv[i];
447 Param->
Pad = atoi(OptionString);
460 Param->
D = (float)atof(OptionString);
469 if(!strcmp(OptionString,
"max"))
470 Param->
Metric = MAX_METRIC;
471 else if(!strcmp(OptionString,
"mse"))
472 Param->
Metric = MSE_METRIC;
473 else if(!strcmp(OptionString,
"rmse"))
474 Param->
Metric = RMSE_METRIC;
475 else if(!strcmp(OptionString,
"psnr"))
476 Param->
Metric = PSNR_METRIC;
477 else if(!strcmp(OptionString,
"mssim"))
478 Param->
Metric = MSSIM_METRIC;
483 #ifdef LIBJPEG_SUPPORT
489 ErrorMessage(
"JPEG quality must be between 0 and 100.\n");
498 if(isprint(OptionChar))
511 Param->
FileA = argv[i];
512 else if(!Param->
FileB)
513 Param->
FileB = argv[i];