00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <cmath>
00024
00025
00026 #include "io_png.h"
00027
00028
00029
00030
00031
00032 int main(int argc, char **argv) {
00033
00034
00035 if (argc < 3) {
00036 printf("usage: img_mse_ipol image1 image2\n");
00037 return EXIT_FAILURE;
00038 }
00039
00040
00041
00042
00043
00044 size_t nx,ny,nc;
00045 float *d_v = NULL;
00046 d_v = io_png_read_f32(argv[1], &nx, &ny, &nc);
00047 if (!d_v) {
00048 printf("error :: %s not found or not a correct png image \n", argv[1]);
00049 return EXIT_FAILURE;
00050 }
00051
00052 if (nc == 2) {
00053 nc = 1;
00054 }
00055 if (nc > 3) {
00056 nc = 3;
00057 }
00058
00059
00060 if (nc > 1) {
00061 int nxy = nx * ny;
00062
00063 int i=0;
00064 while (i < nxy && d_v[i] == d_v[nxy + i] && d_v[i] == d_v[2 * nxy + i ]) {
00065 i++;
00066 }
00067 if (i == nxy) nc = 1;
00068 }
00069
00070
00071
00072 size_t nx2,ny2,nc2;
00073 float *d_v2 = NULL;
00074 d_v2 = io_png_read_f32(argv[2], &nx2, &ny2, &nc2);
00075 if (!d_v2) {
00076 printf("error :: %s not found or not a correct png image \n", argv[2]);
00077 return EXIT_FAILURE;
00078 }
00079
00080
00081 if (nc2 == 2) {
00082 nc2 = 1;
00083 }
00084 if (nc2 > 3) {
00085 nc2 = 3;
00086 }
00087
00088
00089 if (nc2 > 1) {
00090 int nxy2 = nx2 * ny2;
00091
00092 int i=0;
00093 while (i < nxy2 && d_v[i] == d_v[nxy2 + i] && d_v[i] == d_v[2 * nxy2 + i ]) {
00094 i++;
00095 }
00096 if (i == nxy2) nc2 = 1;
00097 }
00098
00099
00100
00101 if (nc != nc2 || nx != nx2 || ny != ny2) {
00102 printf("error :: input images of different size or number of channels \n");
00103 return EXIT_FAILURE;
00104 }
00105
00106
00107
00108
00109
00110 int d_w = (int) nx;
00111 int d_h = (int) ny;
00112 int d_c = (int) nc;
00113
00114 int d_wh = d_w * d_h;
00115
00116
00117
00118
00119 float fDist = 0.0f;
00120 float fPSNR = 0.0f;
00121
00122
00123 for (int ii=0; ii < d_c ; ii++) {
00124
00125 for (int jj=0; jj < d_wh; jj++) {
00126
00127 float dif = d_v[ ii * d_wh + jj] - d_v2[ii * d_wh + jj];
00128 fDist += dif * dif;
00129
00130 }
00131
00132 }
00133
00134
00135 fDist /= (float) (d_c * d_wh);
00136 fDist /= sqrtf(fDist);
00137
00138 fPSNR = 10.0f * log10f(255.0f * 255.0f / (fDist * fDist));
00139
00140 printf("RMSE: %2.2f\n", fDist);
00141 printf("PSNR: %2.2f\n", fPSNR);
00142
00143 return EXIT_SUCCESS;
00144 }