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