/*! * kernelNormalize() * * Input: kels (source kel, to be normalized) * normsum (desired sum of elements in keld) * Return: keld (normalized version of kels), or null on error * or if sum of elements is very close to 0) * * Notes: * (1) If the sum of kernel elements is close to 0, do not * try to calculate the normalized kernel. Instead, * return a copy of the input kernel, with a warning. */ L_KERNEL * kernelNormalize(L_KERNEL *kels, l_float32 normsum) { l_int32 i, j, sx, sy, cx, cy; l_float32 sum, factor; L_KERNEL *keld; PROCNAME("kernelNormalize"); if (!kels) return (L_KERNEL *)ERROR_PTR("kels not defined", procName, NULL); kernelGetSum(kels, &sum); if (L_ABS(sum) < 0.00001) { L_WARNING("null sum; not normalizing; returning a copy\n", procName); return kernelCopy(kels); } kernelGetParameters(kels, &sy, &sx, &cy, &cx); if ((keld = kernelCreate(sy, sx)) == NULL) return (L_KERNEL *)ERROR_PTR("keld not made", procName, NULL); keld->cy = cy; keld->cx = cx; factor = normsum / sum; for (i = 0; i < sy; i++) for (j = 0; j < sx; j++) keld->data[i][j] = factor * kels->data[i][j]; return keld; }
main(int argc, char **argv) { char *str; l_int32 i, j, same, ok; l_float32 sum, avediff, rmsdiff; L_KERNEL *kel1, *kel2, *kel3, *kel4, *kelx, *kely; BOX *box; PIX *pix, *pixs, *pixb, *pixg, *pixr, *pixd, *pixp, *pixt; PIX *pixt1, *pixt2, *pixt3; PIXA *pixa; SARRAY *sa; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; pixa = pixaCreate(0); /* Test creating from a string */ kel1 = kernelCreateFromString(5, 5, 2, 2, kdatastr); pixd = kernelDisplayInPix(kel1, 41, 2); pixWrite("/tmp/pixkern.png", pixd, IFF_PNG); regTestCheckFile(rp, "/tmp/pixkern.png"); /* 0 */ pixSaveTiled(pixd, pixa, 1, 1, 20, 8); pixDestroy(&pixd); kernelDestroy(&kel1); /* Test read/write for kernel. Note that both get * compared to the same golden file, which is * overwritten with a copy of /tmp/kern2.kel */ kel1 = kernelCreateFromString(5, 5, 2, 2, kdatastr); kernelWrite("/tmp/kern1.kel", kel1); regTestCheckFile(rp, "/tmp/kern1.kel"); /* 1 */ kel2 = kernelRead("/tmp/kern1.kel"); kernelWrite("/tmp/kern2.kel", kel2); regTestCheckFile(rp, "/tmp/kern2.kel"); /* 2 */ regTestCompareFiles(rp, 1, 2); /* 3 */ kernelDestroy(&kel1); kernelDestroy(&kel2); /* Test creating from a file */ sa = sarrayCreate(0); sarrayAddString(sa, (char *)"# small 3x3 kernel", L_COPY); sarrayAddString(sa, (char *)"3 5", L_COPY); sarrayAddString(sa, (char *)"1 2", L_COPY); sarrayAddString(sa, (char *)"20.5 50 80 50 20", L_COPY); sarrayAddString(sa, (char *)"82. 120 180 120 80", L_COPY); sarrayAddString(sa, (char *)"22.1 50 80 50 20", L_COPY); str = sarrayToString(sa, 1); l_binaryWrite("/tmp/kernfile.kel", "w", str, strlen(str)); kel2 = kernelCreateFromFile("/tmp/kernfile.kel"); pixd = kernelDisplayInPix(kel2, 41, 2); pixSaveTiled(pixd, pixa, 1, 1, 20, 0); pixWrite("/tmp/ker1.png", pixd, IFF_PNG); regTestCheckFile(rp, "/tmp/ker1.png"); /* 4 */ pixDestroy(&pixd); sarrayDestroy(&sa); lept_free(str); kernelDestroy(&kel2); /* Test creating from a pix */ pixt = pixCreate(5, 3, 8); pixSetPixel(pixt, 0, 0, 20); pixSetPixel(pixt, 1, 0, 50); pixSetPixel(pixt, 2, 0, 80); pixSetPixel(pixt, 3, 0, 50); pixSetPixel(pixt, 4, 0, 20); pixSetPixel(pixt, 0, 1, 80); pixSetPixel(pixt, 1, 1, 120); pixSetPixel(pixt, 2, 1, 180); pixSetPixel(pixt, 3, 1, 120); pixSetPixel(pixt, 4, 1, 80); pixSetPixel(pixt, 0, 0, 20); pixSetPixel(pixt, 1, 2, 50); pixSetPixel(pixt, 2, 2, 80); pixSetPixel(pixt, 3, 2, 50); pixSetPixel(pixt, 4, 2, 20); kel3 = kernelCreateFromPix(pixt, 1, 2); pixd = kernelDisplayInPix(kel3, 41, 2); pixSaveTiled(pixd, pixa, 1, 0, 20, 0); pixWrite("/tmp/ker2.png", pixd, IFF_PNG); regTestCheckFile(rp, "/tmp/ker2.png"); /* 5 */ pixDestroy(&pixd); pixDestroy(&pixt); kernelDestroy(&kel3); /* Test convolution with kel1 */ pixs = pixRead("test24.jpg"); pixg = pixScaleRGBToGrayFast(pixs, 3, COLOR_GREEN); pixSaveTiled(pixg, pixa, 1, 1, 20, 0); kel1 = kernelCreateFromString(5, 5, 2, 2, kdatastr); pixd = pixConvolve(pixg, kel1, 8, 1); pixSaveTiled(pixd, pixa, 1, 0, 20, 0); pixWrite("/tmp/ker3.png", pixd, IFF_PNG); regTestCheckFile(rp, "/tmp/ker3.png"); /* 6 */ pixDestroy(&pixs); pixDestroy(&pixg); pixDestroy(&pixd); kernelDestroy(&kel1); /* Test convolution with flat rectangular kel; also test * block convolution with tiling. */ pixs = pixRead("test24.jpg"); pixg = pixScaleRGBToGrayFast(pixs, 3, COLOR_GREEN); kel2 = makeFlatKernel(11, 11, 5, 5); pixd = pixConvolve(pixg, kel2, 8, 1); pixSaveTiled(pixd, pixa, 1, 1, 20, 0); pixWrite("/tmp/ker4.png", pixd, IFF_PNG); regTestCheckFile(rp, "/tmp/ker4.png"); /* 7 */ pixt = pixBlockconv(pixg, 5, 5); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixWrite("/tmp/ker5.png", pixt, IFF_PNG); regTestCheckFile(rp, "/tmp/ker5.png"); /* 8 */ if (rp->display) pixCompareGray(pixd, pixt, L_COMPARE_ABS_DIFF, GPLOT_X11, NULL, NULL, NULL, NULL); pixt2 = pixBlockconvTiled(pixg, 5, 5, 3, 6); pixSaveTiled(pixt2, pixa, 1, 0, 20, 0); pixWrite("/tmp/ker5a.png", pixt2, IFF_PNG); regTestCheckFile(rp, "/tmp/ker5a.png"); /* 9 */ pixDestroy(&pixt2); ok = TRUE; for (i = 1; i <= 7; i++) { for (j = 1; j <= 7; j++) { if (i == 1 && j == 1) continue; pixt2 = pixBlockconvTiled(pixg, 5, 5, j, i); pixEqual(pixt2, pixd, &same); if (!same) { fprintf(stderr," Error for nx = %d, ny = %d\n", j, i); ok = FALSE; } pixDestroy(&pixt2); } } if (ok) fprintf(stderr, "OK: Tiled results identical to pixConvolve()\n"); else fprintf(stderr, "ERROR: Tiled results not identical to pixConvolve()\n"); pixDestroy(&pixs); pixDestroy(&pixg); pixDestroy(&pixd); pixDestroy(&pixt); kernelDestroy(&kel2); /* Do another flat rectangular test; this time with white at edge. * About 1% of the pixels near the image edge differ by 1 between * the pixConvolve() and pixBlockconv(). For what it's worth, * pixConvolve() gives the more accurate result; namely, 255 for * pixels at the edge. */ pix = pixRead("pageseg1.tif"); box = boxCreate(100, 100, 2260, 3160); pixb = pixClipRectangle(pix, box, NULL); pixs = pixScaleToGray4(pixb); kel3 = makeFlatKernel(7, 7, 3, 3); startTimer(); pixt = pixConvolve(pixs, kel3, 8, 1); fprintf(stderr, "Generic convolution time: %5.3f sec\n", stopTimer()); pixSaveTiled(pixt, pixa, 1, 1, 20, 0); pixWrite("/tmp/conv1.png", pixt, IFF_PNG); regTestCheckFile(rp, "/tmp/conv1.png"); /* 10 */ startTimer(); pixt2 = pixBlockconv(pixs, 3, 3); fprintf(stderr, "Flat block convolution time: %5.3f sec\n", stopTimer()); pixSaveTiled(pixt2, pixa, 1, 0, 20, 0); pixWrite("/tmp/conv2.png", pixt2, IFF_PNG); /* ditto */ regTestCheckFile(rp, "/tmp/conv2.png"); /* 11 */ pixCompareGray(pixt, pixt2, L_COMPARE_ABS_DIFF, GPLOT_PNG, NULL, &avediff, &rmsdiff, NULL); #ifndef _WIN32 sleep(1); /* give gnuplot time to write out the file */ #else Sleep(1000); #endif /* _WIN32 */ pixp = pixRead("/tmp/grayroot.png"); pixSaveTiled(pixp, pixa, 1, 0, 20, 0); pixWrite("/tmp/conv3.png", pixp, IFF_PNG); regTestCheckFile(rp, "/tmp/conv3.png"); /* 12 */ fprintf(stderr, "Ave diff = %6.4f, RMS diff = %6.4f\n", avediff, rmsdiff); if (avediff <= 0.01) fprintf(stderr, "OK: avediff = %6.4f <= 0.01\n", avediff); else fprintf(stderr, "Bad?: avediff = %6.4f > 0.01\n", avediff); pixDestroy(&pixt); pixDestroy(&pixt2); pixDestroy(&pixs); pixDestroy(&pixp); pixDestroy(&pix); pixDestroy(&pixb); boxDestroy(&box); kernelDestroy(&kel3); /* Do yet another set of flat rectangular tests, this time * on an RGB image */ pixs = pixRead("test24.jpg"); kel4 = makeFlatKernel(7, 7, 3, 3); startTimer(); pixt1 = pixConvolveRGB(pixs, kel4); fprintf(stderr, "Time 7x7 non-separable: %7.3f sec\n", stopTimer()); pixWrite("/tmp/conv4.jpg", pixt1, IFF_JFIF_JPEG); regTestCheckFile(rp, "/tmp/conv4.jpg"); /* 13 */ kelx = makeFlatKernel(1, 7, 0, 3); kely = makeFlatKernel(7, 1, 3, 0); startTimer(); pixt2 = pixConvolveRGBSep(pixs, kelx, kely); fprintf(stderr, "Time 7x1,1x7 separable: %7.3f sec\n", stopTimer()); pixWrite("/tmp/conv5.jpg", pixt2, IFF_JFIF_JPEG); regTestCheckFile(rp, "/tmp/conv5.jpg"); /* 14 */ startTimer(); pixt3 = pixBlockconv(pixs, 3, 3); fprintf(stderr, "Time 7x7 blockconv: %7.3f sec\n", stopTimer()); pixWrite("/tmp/conv6.jpg", pixt3, IFF_JFIF_JPEG); regTestCheckFile(rp, "/tmp/conv6.jpg"); /* 15 */ regTestComparePix(rp, pixt1, pixt2); /* 16 */ regTestCompareSimilarPix(rp, pixt2, pixt3, 15, 0.0005, 0); /* 17 */ pixDestroy(&pixs); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); kernelDestroy(&kel4); kernelDestroy(&kelx); kernelDestroy(&kely); /* Test generation and convolution with gaussian kernel */ pixs = pixRead("test8.jpg"); pixSaveTiled(pixs, pixa, 1, 1, 20, 0); kel1 = makeGaussianKernel(5, 5, 3.0, 5.0); kernelGetSum(kel1, &sum); fprintf(stderr, "Sum for gaussian kernel = %f\n", sum); kernelWrite("/tmp/gauss.kel", kel1); pixt = pixConvolve(pixs, kel1, 8, 1); pixt2 = pixConvolve(pixs, kel1, 16, 0); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixSaveTiled(pixt2, pixa, 1, 0, 20, 0); pixWrite("/tmp/ker6.png", pixt, IFF_PNG); regTestCheckFile(rp, "/tmp/ker6.png"); /* 18 */ pixDestroy(&pixt); pixDestroy(&pixt2); pixt = kernelDisplayInPix(kel1, 25, 2); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixDestroy(&pixt); kernelDestroy(&kel1); pixDestroy(&pixs); /* Test generation and convolution with separable gaussian kernel */ pixs = pixRead("test8.jpg"); pixSaveTiled(pixs, pixa, 1, 1, 20, 0); makeGaussianKernelSep(5, 5, 3.0, 5.0, &kelx, &kely); kernelGetSum(kelx, &sum); fprintf(stderr, "Sum for x gaussian kernel = %f\n", sum); kernelGetSum(kely, &sum); fprintf(stderr, "Sum for y gaussian kernel = %f\n", sum); kernelWrite("/tmp/gauss.kelx", kelx); kernelWrite("/tmp/gauss.kely", kely); pixt = pixConvolveSep(pixs, kelx, kely, 8, 1); pixt2 = pixConvolveSep(pixs, kelx, kely, 16, 0); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixSaveTiled(pixt2, pixa, 1, 0, 20, 0); pixWrite("/tmp/ker7.png", pixt, IFF_PNG); regTestCheckFile(rp, "/tmp/ker7.png"); /* 19 */ pixDestroy(&pixt); pixDestroy(&pixt2); pixt = kernelDisplayInPix(kelx, 25, 2); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixDestroy(&pixt); pixt = kernelDisplayInPix(kely, 25, 2); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixDestroy(&pixt); kernelDestroy(&kelx); kernelDestroy(&kely); pixDestroy(&pixs); /* Test generation and convolution with diff of gaussians kernel */ /* pixt = pixRead("marge.jpg"); pixs = pixConvertRGBToLuminance(pixt); pixDestroy(&pixt); */ pixs = pixRead("test8.jpg"); pixSaveTiled(pixs, pixa, 1, 1, 20, 0); kel1 = makeDoGKernel(7, 7, 1.5, 2.7); kernelGetSum(kel1, &sum); fprintf(stderr, "Sum for DoG kernel = %f\n", sum); kernelWrite("/tmp/dog.kel", kel1); pixt = pixConvolve(pixs, kel1, 8, 0); /* pixInvert(pixt, pixt); */ pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixWrite("/tmp/ker8.png", pixt, IFF_PNG); regTestCheckFile(rp, "/tmp/ker8.png"); /* 20 */ pixDestroy(&pixt); pixt = kernelDisplayInPix(kel1, 20, 2); pixSaveTiled(pixt, pixa, 1, 0, 20, 0); pixDestroy(&pixt); kernelDestroy(&kel1); pixDestroy(&pixs); pixd = pixaDisplay(pixa, 0, 0); pixDisplayWithTitle(pixd, 100, 100, NULL, rp->display); pixWrite("/tmp/kernel.jpg", pixd, IFF_JFIF_JPEG); pixDestroy(&pixd); pixaDestroy(&pixa); regTestCleanup(rp); return 0; }
main(int argc, char **argv) { l_float32 sum, sumx, sumy, diff; L_DEWARP *dew; L_DEWARPA *dewa; FPIX *fpixs, *fpixs2, *fpixs3, *fpixs4, *fpixg, *fpixd; FPIX *fpix1, *fpix2, *fpixt1, *fpixt2; DPIX *dpix, *dpix2; L_KERNEL *kel, *kelx, *kely; PIX *pixs, *pixs2, *pixs3, *pixt, *pixd, *pixg, *pixb, *pixn; PIX *pixt1, *pixt2, *pixt3, *pixt4, *pixt5, *pixt6; PIXA *pixa; PTA *ptas, *ptad; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; pixa = pixaCreate(0); /* Gaussian kernel */ kel = makeGaussianKernel(5, 5, 3.0, 4.0); kernelGetSum(kel, &sum); if (rp->display) fprintf(stderr, "Sum for 2d gaussian kernel = %f\n", sum); pixt = kernelDisplayInPix(kel, 41, 2); regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 0 */ pixSaveTiled(pixt, pixa, 1, 1, 20, 8); pixDestroy(&pixt); /* Separable gaussian kernel */ makeGaussianKernelSep(5, 5, 3.0, 4.0, &kelx, &kely); kernelGetSum(kelx, &sumx); if (rp->display) fprintf(stderr, "Sum for x gaussian kernel = %f\n", sumx); kernelGetSum(kely, &sumy); if (rp->display) fprintf(stderr, "Sum for y gaussian kernel = %f\n", sumy); if (rp->display) fprintf(stderr, "Sum for x * y gaussian kernel = %f\n", sumx * sumy); pixt = kernelDisplayInPix(kelx, 41, 2); regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 1 */ pixSaveTiled(pixt, pixa, 1, 0, 20, 8); pixDestroy(&pixt); pixt = kernelDisplayInPix(kely, 41, 2); regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 2 */ pixSaveTiled(pixt, pixa, 1, 0, 20, 8); pixDestroy(&pixt); /* Use pixRasterop() to generate source image */ pixs = pixRead("test8.jpg"); pixs2 = pixRead("karen8.jpg"); pixRasterop(pixs, 150, 125, 150, 100, PIX_SRC, pixs2, 75, 100); regTestWritePixAndCheck(rp, pixs, IFF_JFIF_JPEG); /* 3 */ /* Convolution directly with pix */ pixt1 = pixConvolve(pixs, kel, 8, 1); regTestWritePixAndCheck(rp, pixt1, IFF_JFIF_JPEG); /* 4 */ pixSaveTiled(pixt1, pixa, 1, 1, 20, 8); pixt2 = pixConvolveSep(pixs, kelx, kely, 8, 1); regTestWritePixAndCheck(rp, pixt2, IFF_JFIF_JPEG); /* 5 */ pixSaveTiled(pixt2, pixa, 1, 0, 20, 8); /* Convolution indirectly with fpix, using fpixRasterop() * to generate the source image. */ fpixs = pixConvertToFPix(pixs, 3); fpixs2 = pixConvertToFPix(pixs2, 3); fpixRasterop(fpixs, 150, 125, 150, 100, fpixs2, 75, 100); fpixt1 = fpixConvolve(fpixs, kel, 1); pixt3 = fpixConvertToPix(fpixt1, 8, L_CLIP_TO_ZERO, 1); regTestWritePixAndCheck(rp, pixt3, IFF_JFIF_JPEG); /* 6 */ pixSaveTiled(pixt3, pixa, 1, 1, 20, 8); fpixt2 = fpixConvolveSep(fpixs, kelx, kely, 1); pixt4 = fpixConvertToPix(fpixt2, 8, L_CLIP_TO_ZERO, 1); regTestWritePixAndCheck(rp, pixt4, IFF_JFIF_JPEG); /* 7 */ pixSaveTiled(pixt4, pixa, 1, 0, 20, 8); pixDestroy(&pixs2); fpixDestroy(&fpixs2); fpixDestroy(&fpixt1); fpixDestroy(&fpixt2); /* Comparison of results */ pixCompareGray(pixt1, pixt2, L_COMPARE_ABS_DIFF, 0, NULL, &diff, NULL, NULL); if (rp->display) fprintf(stderr, "Ave diff of pixConvolve and pixConvolveSep: %f\n", diff); pixCompareGray(pixt3, pixt4, L_COMPARE_ABS_DIFF, 0, NULL, &diff, NULL, NULL); if (rp->display) fprintf(stderr, "Ave diff of fpixConvolve and fpixConvolveSep: %f\n", diff); pixCompareGray(pixt1, pixt3, L_COMPARE_ABS_DIFF, 0, NULL, &diff, NULL, NULL); if (rp->display) fprintf(stderr, "Ave diff of pixConvolve and fpixConvolve: %f\n", diff); pixCompareGray(pixt2, pixt4, L_COMPARE_ABS_DIFF, GPLOT_PNG, NULL, &diff, NULL, NULL); if (rp->display) fprintf(stderr, "Ave diff of pixConvolveSep and fpixConvolveSep: %f\n", diff); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); /* Test arithmetic operations; add in a fraction rotated by 180 */ pixs3 = pixRotate180(NULL, pixs); regTestWritePixAndCheck(rp, pixs3, IFF_JFIF_JPEG); /* 8 */ pixSaveTiled(pixs3, pixa, 1, 1, 20, 8); fpixs3 = pixConvertToFPix(pixs3, 3); fpixd = fpixLinearCombination(NULL, fpixs, fpixs3, 20.0, 5.0); fpixAddMultConstant(fpixd, 0.0, 23.174); /* multiply up in magnitude */ pixd = fpixDisplayMaxDynamicRange(fpixd); /* bring back to 8 bpp */ regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 9 */ pixSaveTiled(pixd, pixa, 1, 0, 20, 8); pixDestroy(&pixs3); fpixDestroy(&fpixs3); fpixDestroy(&fpixd); pixDestroy(&pixd); pixDestroy(&pixs); fpixDestroy(&fpixs); /* Save the comparison graph; gnuplot should have made it by now! */ #ifndef _WIN32 sleep(2); #else Sleep(2000); #endif /* _WIN32 */ pixt5 = pixRead("/tmp/grayroot.png"); regTestWritePixAndCheck(rp, pixt5, IFF_PNG); /* 10 */ pixSaveTiled(pixt5, pixa, 1, 1, 20, 8); pixDestroy(&pixt5); /* Display results */ pixd = pixaDisplay(pixa, 0, 0); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 11 */ pixDisplayWithTitle(pixd, 100, 100, NULL, rp->display); pixDestroy(&pixd); pixaDestroy(&pixa); /* Test some more convolutions, with sampled output. First on pix */ pixa = pixaCreate(0); pixs = pixRead("1555-7.jpg"); pixg = pixConvertTo8(pixs, 0); l_setConvolveSampling(5, 5); pixt1 = pixConvolve(pixg, kel, 8, 1); regTestWritePixAndCheck(rp, pixt1, IFF_JFIF_JPEG); /* 12 */ pixSaveTiled(pixt1, pixa, 1, 1, 20, 32); pixt2 = pixConvolveSep(pixg, kelx, kely, 8, 1); regTestWritePixAndCheck(rp, pixt2, IFF_JFIF_JPEG); /* 13 */ pixSaveTiled(pixt2, pixa, 1, 0, 20, 32); pixt3 = pixConvolveRGB(pixs, kel); regTestWritePixAndCheck(rp, pixt3, IFF_JFIF_JPEG); /* 14 */ pixSaveTiled(pixt3, pixa, 1, 0, 20, 32); pixt4 = pixConvolveRGBSep(pixs, kelx, kely); regTestWritePixAndCheck(rp, pixt4, IFF_JFIF_JPEG); /* 15 */ pixSaveTiled(pixt4, pixa, 1, 0, 20, 32); /* Then on fpix */ fpixg = pixConvertToFPix(pixg, 1); fpixt1 = fpixConvolve(fpixg, kel, 1); pixt5 = fpixConvertToPix(fpixt1, 8, L_CLIP_TO_ZERO, 0); regTestWritePixAndCheck(rp, pixt5, IFF_JFIF_JPEG); /* 16 */ pixSaveTiled(pixt5, pixa, 1, 1, 20, 32); fpixt2 = fpixConvolveSep(fpixg, kelx, kely, 1); pixt6 = fpixConvertToPix(fpixt2, 8, L_CLIP_TO_ZERO, 0); regTestWritePixAndCheck(rp, pixt6, IFF_JFIF_JPEG); /* 17 */ pixSaveTiled(pixt2, pixa, 1, 0, 20, 32); regTestCompareSimilarPix(rp, pixt1, pixt5, 2, 0.00, 0); /* 18 */ regTestCompareSimilarPix(rp, pixt2, pixt6, 2, 0.00, 0); /* 19 */ pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); pixDestroy(&pixt5); pixDestroy(&pixt6); fpixDestroy(&fpixg); fpixDestroy(&fpixt1); fpixDestroy(&fpixt2); pixd = pixaDisplay(pixa, 0, 0); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 20 */ pixDisplayWithTitle(pixd, 600, 100, NULL, rp->display); pixDestroy(&pixs); pixDestroy(&pixg); pixDestroy(&pixd); pixaDestroy(&pixa); /* Test extension (continued and slope). * First, build a smooth vertical disparity array; * then extend and show the contours. */ pixs = pixRead("cat-35.jpg"); pixn = pixBackgroundNormSimple(pixs, NULL, NULL); pixg = pixConvertRGBToGray(pixn, 0.5, 0.3, 0.2); pixb = pixThresholdToBinary(pixg, 130); dewa = dewarpaCreate(1, 30, 1, 15, 0); dew = dewarpCreate(pixb, 35); dewarpaInsertDewarp(dewa, dew); dewarpBuildModel(dew, NULL); dewarpPopulateFullRes(dew, NULL); fpixs = dew->fullvdispar; fpixs2 = fpixAddContinuedBorder(fpixs, 200, 200, 100, 300); fpixs3 = fpixAddSlopeBorder(fpixs, 200, 200, 100, 300); dpix = fpixConvertToDPix(fpixs3); fpixs4 = dpixConvertToFPix(dpix); pixt1 = fpixRenderContours(fpixs, 2.0, 0.2); pixt2 = fpixRenderContours(fpixs2, 2.0, 0.2); pixt3 = fpixRenderContours(fpixs3, 2.0, 0.2); pixt4 = fpixRenderContours(fpixs4, 2.0, 0.2); pixt5 = pixRead("karen8.jpg"); dpix2 = pixConvertToDPix(pixt5, 1); pixt6 = dpixConvertToPix(dpix2, 8, L_CLIP_TO_ZERO, 0); regTestWritePixAndCheck(rp, pixt1, IFF_PNG); /* 21 */ pixDisplayWithTitle(pixt1, 0, 100, NULL, rp->display); regTestWritePixAndCheck(rp, pixt2, IFF_PNG); /* 22 */ pixDisplayWithTitle(pixt2, 470, 100, NULL, rp->display); regTestWritePixAndCheck(rp, pixt3, IFF_PNG); /* 23 */ pixDisplayWithTitle(pixt3, 1035, 100, NULL, rp->display); regTestComparePix(rp, pixt3, pixt4); /* 24 */ regTestComparePix(rp, pixt5, pixt6); /* 25 */ pixDestroy(&pixs); pixDestroy(&pixn); pixDestroy(&pixg); pixDestroy(&pixb); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); pixDestroy(&pixt5); pixDestroy(&pixt6); fpixDestroy(&fpixs2); fpixDestroy(&fpixs3); fpixDestroy(&fpixs4); dpixDestroy(&dpix); dpixDestroy(&dpix2); /* Test affine and projective transforms on fpix */ fpixWrite("/tmp/fpix1.fp", dew->fullvdispar); fpix1 = fpixRead("/tmp/fpix1.fp"); pixt1 = fpixAutoRenderContours(fpix1, 40); regTestWritePixAndCheck(rp, pixt1, IFF_PNG); /* 26 */ pixDisplayWithTitle(pixt1, 0, 500, NULL, rp->display); pixDestroy(&pixt1); MakePtasAffine(1, &ptas, &ptad); fpix2 = fpixAffinePta(fpix1, ptad, ptas, 200, 0.0); pixt2 = fpixAutoRenderContours(fpix2, 40); regTestWritePixAndCheck(rp, pixt2, IFF_PNG); /* 27 */ pixDisplayWithTitle(pixt2, 400, 500, NULL, rp->display); fpixDestroy(&fpix2); pixDestroy(&pixt2); ptaDestroy(&ptas); ptaDestroy(&ptad); MakePtas(1, &ptas, &ptad); fpix2 = fpixProjectivePta(fpix1, ptad, ptas, 200, 0.0); pixt3 = fpixAutoRenderContours(fpix2, 40); regTestWritePixAndCheck(rp, pixt3, IFF_PNG); /* 28 */ pixDisplayWithTitle(pixt3, 400, 500, NULL, rp->display); fpixDestroy(&fpix2); pixDestroy(&pixt3); ptaDestroy(&ptas); ptaDestroy(&ptad); fpixDestroy(&fpix1); dewarpaDestroy(&dewa); kernelDestroy(&kel); kernelDestroy(&kelx); kernelDestroy(&kely); return regTestCleanup(rp); }
main(int argc, char **argv) { l_float32 sum, sumx, sumy, diff; FPIX *fpixs, *fpixs2, *fpixs3, *fpixt1, *fpixt2, *fpixg, *fpixd; L_KERNEL *kel, *kelx, *kely; PIX *pixs, *pixs2, *pixs3, *pixt, *pixd, *pixg; PIX *pixt1, *pixt2, *pixt3, *pixt4, *pixt5, *pixt6; PIXA *pixa; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; pixa = pixaCreate(0); /* Gaussian kernel */ kel = makeGaussianKernel(5, 5, 3.0, 4.0); kernelGetSum(kel, &sum); if (rp->display) fprintf(stderr, "Sum for 2d gaussian kernel = %f\n", sum); pixt = kernelDisplayInPix(kel, 41, 2); regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 0 */ pixSaveTiled(pixt, pixa, 1, 1, 20, 8); pixDestroy(&pixt); /* Separable gaussian kernel */ makeGaussianKernelSep(5, 5, 3.0, 4.0, &kelx, &kely); kernelGetSum(kelx, &sumx); if (rp->display) fprintf(stderr, "Sum for x gaussian kernel = %f\n", sumx); kernelGetSum(kely, &sumy); if (rp->display) fprintf(stderr, "Sum for y gaussian kernel = %f\n", sumy); if (rp->display) fprintf(stderr, "Sum for x * y gaussian kernel = %f\n", sumx * sumy); pixt = kernelDisplayInPix(kelx, 41, 2); regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 1 */ pixSaveTiled(pixt, pixa, 1, 0, 20, 8); pixDestroy(&pixt); pixt = kernelDisplayInPix(kely, 41, 2); regTestWritePixAndCheck(rp, pixt, IFF_PNG); /* 2 */ pixSaveTiled(pixt, pixa, 1, 0, 20, 8); pixDestroy(&pixt); /* Use pixRasterop() to generate source image */ pixs = pixRead("test8.jpg"); pixs2 = pixRead("karen8.jpg"); pixRasterop(pixs, 150, 125, 150, 100, PIX_SRC, pixs2, 75, 100); regTestWritePixAndCheck(rp, pixs, IFF_JFIF_JPEG); /* 3 */ /* Convolution directly with pix */ pixt1 = pixConvolve(pixs, kel, 8, 1); regTestWritePixAndCheck(rp, pixt1, IFF_JFIF_JPEG); /* 4 */ pixSaveTiled(pixt1, pixa, 1, 1, 20, 8); pixt2 = pixConvolveSep(pixs, kelx, kely, 8, 1); regTestWritePixAndCheck(rp, pixt2, IFF_JFIF_JPEG); /* 5 */ pixSaveTiled(pixt2, pixa, 1, 0, 20, 8); /* Convolution indirectly with fpix, using fpixRasterop() * to generate the source image. */ fpixs = pixConvertToFPix(pixs, 3); fpixs2 = pixConvertToFPix(pixs2, 3); fpixRasterop(fpixs, 150, 125, 150, 100, fpixs2, 75, 100); fpixt1 = fpixConvolve(fpixs, kel, 1); pixt3 = fpixConvertToPix(fpixt1, 8, L_CLIP_TO_ZERO, 1); regTestWritePixAndCheck(rp, pixt3, IFF_JFIF_JPEG); /* 6 */ pixSaveTiled(pixt3, pixa, 1, 1, 20, 8); fpixt2 = fpixConvolveSep(fpixs, kelx, kely, 1); pixt4 = fpixConvertToPix(fpixt2, 8, L_CLIP_TO_ZERO, 1); regTestWritePixAndCheck(rp, pixt4, IFF_JFIF_JPEG); /* 7 */ pixSaveTiled(pixt4, pixa, 1, 0, 20, 8); pixDestroy(&pixs2); fpixDestroy(&fpixs2); fpixDestroy(&fpixt1); fpixDestroy(&fpixt2); /* Comparison of results */ pixCompareGray(pixt1, pixt2, L_COMPARE_ABS_DIFF, 0, NULL, &diff, NULL, NULL); if (rp->display) fprintf(stderr, "Ave diff of pixConvolve and pixConvolveSep: %f\n", diff); pixCompareGray(pixt3, pixt4, L_COMPARE_ABS_DIFF, 0, NULL, &diff, NULL, NULL); if (rp->display) fprintf(stderr, "Ave diff of fpixConvolve and fpixConvolveSep: %f\n", diff); pixCompareGray(pixt1, pixt3, L_COMPARE_ABS_DIFF, 0, NULL, &diff, NULL, NULL); if (rp->display) fprintf(stderr, "Ave diff of pixConvolve and fpixConvolve: %f\n", diff); pixCompareGray(pixt2, pixt4, L_COMPARE_ABS_DIFF, GPLOT_PNG, NULL, &diff, NULL, NULL); if (rp->display) fprintf(stderr, "Ave diff of pixConvolveSep and fpixConvolveSep: %f\n", diff); pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); /* Test arithmetic operations; add in a fraction rotated by 180 */ pixs3 = pixRotate180(NULL, pixs); regTestWritePixAndCheck(rp, pixs3, IFF_JFIF_JPEG); /* 8 */ pixSaveTiled(pixs3, pixa, 1, 1, 20, 8); fpixs3 = pixConvertToFPix(pixs3, 3); fpixd = fpixLinearCombination(NULL, fpixs, fpixs3, 20.0, 5.0); fpixAddMultConstant(fpixd, 0.0, 23.174); /* multiply up in magnitude */ pixd = fpixDisplayMaxDynamicRange(fpixd); /* bring back to 8 bpp */ regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 9 */ pixSaveTiled(pixd, pixa, 1, 0, 20, 8); pixDestroy(&pixs3); fpixDestroy(&fpixs3); fpixDestroy(&fpixd); pixDestroy(&pixd); pixDestroy(&pixs); fpixDestroy(&fpixs); /* Save the comparison graph; gnuplot should have made it by now! */ #ifndef _WIN32 sleep(2); #else Sleep(2000); #endif /* _WIN32 */ pixt5 = pixRead("/tmp/grayroot.png"); regTestWritePixAndCheck(rp, pixt5, IFF_PNG); /* 10 */ pixSaveTiled(pixt5, pixa, 1, 1, 20, 8); pixDestroy(&pixt5); /* Display results */ pixd = pixaDisplay(pixa, 0, 0); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 11 */ pixDisplayWithTitle(pixd, 100, 100, NULL, rp->display); pixDestroy(&pixd); pixaDestroy(&pixa); /* Test some more convolutions, with sampled output. First on pix */ pixa = pixaCreate(0); pixs = pixRead("1555-7.jpg"); pixg = pixConvertTo8(pixs, 0); l_setConvolveSampling(5, 5); pixt1 = pixConvolve(pixg, kel, 8, 1); regTestWritePixAndCheck(rp, pixt1, IFF_JFIF_JPEG); /* 12 */ pixSaveTiled(pixt1, pixa, 1, 1, 20, 32); pixt2 = pixConvolveSep(pixg, kelx, kely, 8, 1); regTestWritePixAndCheck(rp, pixt2, IFF_JFIF_JPEG); /* 13 */ pixSaveTiled(pixt2, pixa, 1, 0, 20, 32); pixt3 = pixConvolveRGB(pixs, kel); regTestWritePixAndCheck(rp, pixt3, IFF_JFIF_JPEG); /* 14 */ pixSaveTiled(pixt3, pixa, 1, 0, 20, 32); pixt4 = pixConvolveRGBSep(pixs, kelx, kely); regTestWritePixAndCheck(rp, pixt4, IFF_JFIF_JPEG); /* 15 */ pixSaveTiled(pixt4, pixa, 1, 0, 20, 32); /* Then on fpix */ fpixg = pixConvertToFPix(pixg, 1); fpixt1 = fpixConvolve(fpixg, kel, 1); pixt5 = fpixConvertToPix(fpixt1, 8, L_CLIP_TO_ZERO, 0); regTestWritePixAndCheck(rp, pixt5, IFF_JFIF_JPEG); /* 16 */ pixSaveTiled(pixt5, pixa, 1, 1, 20, 32); fpixt2 = fpixConvolveSep(fpixg, kelx, kely, 1); pixt6 = fpixConvertToPix(fpixt2, 8, L_CLIP_TO_ZERO, 0); regTestWritePixAndCheck(rp, pixt6, IFF_JFIF_JPEG); /* 17 */ pixSaveTiled(pixt2, pixa, 1, 0, 20, 32); regTestCompareSimilarPix(rp, pixt1, pixt5, 2, 0.00, 0); /* 18 */ regTestCompareSimilarPix(rp, pixt2, pixt6, 2, 0.00, 0); /* 19 */ pixDestroy(&pixt1); pixDestroy(&pixt2); pixDestroy(&pixt3); pixDestroy(&pixt4); pixDestroy(&pixt5); pixDestroy(&pixt6); fpixDestroy(&fpixg); fpixDestroy(&fpixt1); fpixDestroy(&fpixt2); pixd = pixaDisplay(pixa, 0, 0); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 20 */ pixDisplayWithTitle(pixd, 600, 100, NULL, rp->display); pixDestroy(&pixd); pixaDestroy(&pixa); regTestCleanup(rp); pixDestroy(&pixs); pixDestroy(&pixg); kernelDestroy(&kel); kernelDestroy(&kelx); kernelDestroy(&kely); return 0; }