Leptonica 1.68
C Image Processing Library
|
00001 /*====================================================================* 00002 - Copyright (C) 2001 Leptonica. All rights reserved. 00003 - This software is distributed in the hope that it will be 00004 - useful, but with NO WARRANTY OF ANY KIND. 00005 - No author or distributor accepts responsibility to anyone for the 00006 - consequences of using this software, or for whether it serves any 00007 - particular purpose or works at all, unless he or she says so in 00008 - writing. Everyone is granted permission to copy, modify and 00009 - redistribute this source code, for commercial or non-commercial 00010 - purposes, with the following restrictions: (1) the origin of this 00011 - source code must not be misrepresented; (2) modified versions must 00012 - be plainly marked as such; and (3) this notice may not be removed 00013 - or altered from any source or modified source distribution. 00014 *====================================================================*/ 00015 00016 /* 00017 * fpix_reg.c 00018 * 00019 * Regression test for a number of functions in the FPix utility. 00020 * FPix allows you to do floating point operations such as 00021 * convolution, with conversions to and from Pix. 00022 */ 00023 00024 #include <math.h> 00025 #ifndef _WIN32 00026 #include <unistd.h> 00027 #else 00028 #include <windows.h> /* for Sleep() */ 00029 #endif /* _WIN32 */ 00030 #include "allheaders.h" 00031 00032 main(int argc, 00033 char **argv) 00034 { 00035 l_float32 sum, sumx, sumy, diff; 00036 FPIX *fpixs, *fpixs2, *fpixs3, *fpixt1, *fpixt2, *fpixg, *fpixd; 00037 L_KERNEL *kel, *kelx, *kely; 00038 PIX *pixs, *pixs2, *pixs3, *pixt, *pixd, *pixg; 00039 PIX *pixt1, *pixt2, *pixt3, *pixt4, *pixt5, *pixt6; 00040 PIXA *pixa; 00041 L_REGPARAMS *rp; 00042 00043 if (regTestSetup(argc, argv, &rp)) 00044 return 1; 00045 00046 pixa = pixaCreate(0); 00047 00048 /* Gaussian kernel */ 00049 kel = makeGaussianKernel(5, 5, 3.0, 4.0); 00050 kernelGetSum(kel, &sum); 00051 if (rp->display) fprintf(stderr, "Sum for 2d gaussian kernel = %f\n", sum); 00052 pixt = kernelDisplayInPix(kel, 41, 2); 00053 regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 0 */ 00054 pixSaveTiled(pixt, pixa, 1, 1, 20, 8); 00055 pixDestroy(&pixt); 00056 00057 /* Separable gaussian kernel */ 00058 makeGaussianKernelSep(5, 5, 3.0, 4.0, &kelx, &kely); 00059 kernelGetSum(kelx, &sumx); 00060 if (rp->display) fprintf(stderr, "Sum for x gaussian kernel = %f\n", sumx); 00061 kernelGetSum(kely, &sumy); 00062 if (rp->display) fprintf(stderr, "Sum for y gaussian kernel = %f\n", sumy); 00063 if (rp->display) fprintf(stderr, "Sum for x * y gaussian kernel = %f\n", 00064 sumx * sumy); 00065 pixt = kernelDisplayInPix(kelx, 41, 2); 00066 regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 1 */ 00067 pixSaveTiled(pixt, pixa, 1, 0, 20, 8); 00068 pixDestroy(&pixt); 00069 pixt = kernelDisplayInPix(kely, 41, 2); 00070 regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 2 */ 00071 pixSaveTiled(pixt, pixa, 1, 0, 20, 8); 00072 pixDestroy(&pixt); 00073 00074 /* Use pixRasterop() to generate source image */ 00075 pixs = pixRead("test8.jpg"); 00076 pixs2 = pixRead("karen8.jpg"); 00077 pixRasterop(pixs, 150, 125, 150, 100, PIX_SRC, pixs2, 75, 100); 00078 regTestWritePixAndCheck(rp, pixs, IFF_JFIF_JPEG); /* 3 */ 00079 00080 /* Convolution directly with pix */ 00081 pixt1 = pixConvolve(pixs, kel, 8, 1); 00082 regTestWritePixAndCheck(rp, pixt1, IFF_JFIF_JPEG); /* 4 */ 00083 pixSaveTiled(pixt1, pixa, 1, 1, 20, 8); 00084 pixt2 = pixConvolveSep(pixs, kelx, kely, 8, 1); 00085 regTestWritePixAndCheck(rp, pixt2, IFF_JFIF_JPEG); /* 5 */ 00086 pixSaveTiled(pixt2, pixa, 1, 0, 20, 8); 00087 00088 /* Convolution indirectly with fpix, using fpixRasterop() 00089 * to generate the source image. */ 00090 fpixs = pixConvertToFPix(pixs, 3); 00091 fpixs2 = pixConvertToFPix(pixs2, 3); 00092 fpixRasterop(fpixs, 150, 125, 150, 100, fpixs2, 75, 100); 00093 fpixt1 = fpixConvolve(fpixs, kel, 1); 00094 pixt3 = fpixConvertToPix(fpixt1, 8, L_CLIP_TO_ZERO, 1); 00095 regTestWritePixAndCheck(rp, pixt3, IFF_JFIF_JPEG); /* 6 */ 00096 pixSaveTiled(pixt3, pixa, 1, 1, 20, 8); 00097 fpixt2 = fpixConvolveSep(fpixs, kelx, kely, 1); 00098 pixt4 = fpixConvertToPix(fpixt2, 8, L_CLIP_TO_ZERO, 1); 00099 regTestWritePixAndCheck(rp, pixt4, IFF_JFIF_JPEG); /* 7 */ 00100 pixSaveTiled(pixt4, pixa, 1, 0, 20, 8); 00101 pixDestroy(&pixs2); 00102 fpixDestroy(&fpixs2); 00103 fpixDestroy(&fpixt1); 00104 fpixDestroy(&fpixt2); 00105 00106 /* Comparison of results */ 00107 pixCompareGray(pixt1, pixt2, L_COMPARE_ABS_DIFF, 0, NULL, 00108 &diff, NULL, NULL); 00109 if (rp->display) 00110 fprintf(stderr, "Ave diff of pixConvolve and pixConvolveSep: %f\n", 00111 diff); 00112 pixCompareGray(pixt3, pixt4, L_COMPARE_ABS_DIFF, 0, NULL, 00113 &diff, NULL, NULL); 00114 if (rp->display) 00115 fprintf(stderr, "Ave diff of fpixConvolve and fpixConvolveSep: %f\n", 00116 diff); 00117 pixCompareGray(pixt1, pixt3, L_COMPARE_ABS_DIFF, 0, NULL, 00118 &diff, NULL, NULL); 00119 if (rp->display) 00120 fprintf(stderr, "Ave diff of pixConvolve and fpixConvolve: %f\n", diff); 00121 pixCompareGray(pixt2, pixt4, L_COMPARE_ABS_DIFF, GPLOT_PNG, NULL, 00122 &diff, NULL, NULL); 00123 if (rp->display) 00124 fprintf(stderr, "Ave diff of pixConvolveSep and fpixConvolveSep: %f\n", 00125 diff); 00126 pixDestroy(&pixt1); 00127 pixDestroy(&pixt2); 00128 pixDestroy(&pixt3); 00129 pixDestroy(&pixt4); 00130 00131 /* Test arithmetic operations; add in a fraction rotated by 180 */ 00132 pixs3 = pixRotate180(NULL, pixs); 00133 regTestWritePixAndCheck(rp, pixs3, IFF_JFIF_JPEG); /* 8 */ 00134 pixSaveTiled(pixs3, pixa, 1, 1, 20, 8); 00135 fpixs3 = pixConvertToFPix(pixs3, 3); 00136 fpixd = fpixLinearCombination(NULL, fpixs, fpixs3, 20.0, 5.0); 00137 fpixAddMultConstant(fpixd, 0.0, 23.174); /* multiply up in magnitude */ 00138 pixd = fpixDisplayMaxDynamicRange(fpixd); /* bring back to 8 bpp */ 00139 regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 9 */ 00140 pixSaveTiled(pixd, pixa, 1, 0, 20, 8); 00141 pixDestroy(&pixs3); 00142 fpixDestroy(&fpixs3); 00143 fpixDestroy(&fpixd); 00144 pixDestroy(&pixd); 00145 pixDestroy(&pixs); 00146 fpixDestroy(&fpixs); 00147 00148 /* Save the comparison graph; gnuplot should have made it by now! */ 00149 #ifndef _WIN32 00150 sleep(2); 00151 #else 00152 Sleep(2000); 00153 #endif /* _WIN32 */ 00154 pixt5 = pixRead("/tmp/grayroot.png"); 00155 regTestWritePixAndCheck(rp, pixt5, IFF_PNG); /* 10 */ 00156 pixSaveTiled(pixt5, pixa, 1, 1, 20, 8); 00157 pixDestroy(&pixt5); 00158 00159 /* Display results */ 00160 pixd = pixaDisplay(pixa, 0, 0); 00161 regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 11 */ 00162 pixDisplayWithTitle(pixd, 100, 100, NULL, rp->display); 00163 pixDestroy(&pixd); 00164 pixaDestroy(&pixa); 00165 00166 /* Test some more convolutions, with sampled output. First on pix */ 00167 pixa = pixaCreate(0); 00168 pixs = pixRead("1555-7.jpg"); 00169 pixg = pixConvertTo8(pixs, 0); 00170 l_setConvolveSampling(5, 5); 00171 pixt1 = pixConvolve(pixg, kel, 8, 1); 00172 regTestWritePixAndCheck(rp, pixt1, IFF_JFIF_JPEG); /* 12 */ 00173 pixSaveTiled(pixt1, pixa, 1, 1, 20, 32); 00174 pixt2 = pixConvolveSep(pixg, kelx, kely, 8, 1); 00175 regTestWritePixAndCheck(rp, pixt2, IFF_JFIF_JPEG); /* 13 */ 00176 pixSaveTiled(pixt2, pixa, 1, 0, 20, 32); 00177 pixt3 = pixConvolveRGB(pixs, kel); 00178 regTestWritePixAndCheck(rp, pixt3, IFF_JFIF_JPEG); /* 14 */ 00179 pixSaveTiled(pixt3, pixa, 1, 0, 20, 32); 00180 pixt4 = pixConvolveRGBSep(pixs, kelx, kely); 00181 regTestWritePixAndCheck(rp, pixt4, IFF_JFIF_JPEG); /* 15 */ 00182 pixSaveTiled(pixt4, pixa, 1, 0, 20, 32); 00183 00184 /* Then on fpix */ 00185 fpixg = pixConvertToFPix(pixg, 1); 00186 fpixt1 = fpixConvolve(fpixg, kel, 1); 00187 pixt5 = fpixConvertToPix(fpixt1, 8, L_CLIP_TO_ZERO, 0); 00188 regTestWritePixAndCheck(rp, pixt5, IFF_JFIF_JPEG); /* 16 */ 00189 pixSaveTiled(pixt5, pixa, 1, 1, 20, 32); 00190 fpixt2 = fpixConvolveSep(fpixg, kelx, kely, 1); 00191 pixt6 = fpixConvertToPix(fpixt2, 8, L_CLIP_TO_ZERO, 0); 00192 regTestWritePixAndCheck(rp, pixt6, IFF_JFIF_JPEG); /* 17 */ 00193 pixSaveTiled(pixt2, pixa, 1, 0, 20, 32); 00194 regTestCompareSimilarPix(rp, pixt1, pixt5, 2, 0.00, 0); /* 18 */ 00195 regTestCompareSimilarPix(rp, pixt2, pixt6, 2, 0.00, 0); /* 19 */ 00196 pixDestroy(&pixt1); 00197 pixDestroy(&pixt2); 00198 pixDestroy(&pixt3); 00199 pixDestroy(&pixt4); 00200 pixDestroy(&pixt5); 00201 pixDestroy(&pixt6); 00202 fpixDestroy(&fpixg); 00203 fpixDestroy(&fpixt1); 00204 fpixDestroy(&fpixt2); 00205 00206 pixd = pixaDisplay(pixa, 0, 0); 00207 regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 20 */ 00208 pixDisplayWithTitle(pixd, 600, 100, NULL, rp->display); 00209 pixDestroy(&pixd); 00210 pixaDestroy(&pixa); 00211 00212 regTestCleanup(rp); 00213 pixDestroy(&pixs); 00214 pixDestroy(&pixg); 00215 kernelDestroy(&kel); 00216 kernelDestroy(&kelx); 00217 kernelDestroy(&kely); 00218 return 0; 00219 }