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_int32 i, j, wc, hc, d; L_KERNEL *kel1, *kel2; PIX *pixs, *pixg, *pixacc, *pixd, *pixt; char *filein, *fileout; static char mainName[] = "convolvetest"; if (argc != 5) exit(ERROR_INT(" Syntax: convolvetest filein wc hc fileout", mainName, 1)); filein = argv[1]; wc = atoi(argv[2]); hc = atoi(argv[3]); fileout = argv[4]; if ((pixs = pixRead(filein)) == NULL) exit(ERROR_INT("pix not made", mainName, 1)); #if 0 /* Measure speed */ pixacc = pixBlockconvAccum(pixs); for (i = 0; i < NTIMES; i++) { pixd = pixBlockconvGray(pixs, pixacc, wc, hc); if ((i+1) % 10 == 0) fprintf(stderr, "%d iters\n", i + 1); pixDestroy(&pixd); } pixd = pixBlockconvGray(pixs, pixacc, wc, hc); pixWrite(fileout, pixd, IFF_JFIF_JPEG); pixDestroy(&pixacc); #endif #if 0 /* Test pixBlockconvGray() */ pixacc = pixBlockconvAccum(pixs); pixd = pixBlockconvGray(pixs, pixacc, wc, hc); pixWrite(fileout, pixd, IFF_JFIF_JPEG); pixDestroy(&pixacc); #endif #if 0 /* Test pixBlockconv() */ pixd = pixBlockconv(pixs, wc, hc); pixWrite(fileout, pixd, IFF_JFIF_JPEG); #endif #if 0 /* Test pixBlockrank() */ pixacc = pixBlockconvAccum(pixs); pixd = pixBlockrank(pixs, pixacc, wc, hc, 0.5); pixWrite(fileout, pixd, IFF_TIFF_G4); pixDestroy(&pixacc); #endif #if 0 /* Test pixBlocksum() */ pixacc = pixBlockconvAccum(pixs); pixd = pixBlocksum(pixs, pixacc, wc, hc); pixInvert(pixd, pixd); pixWrite(fileout, pixd, IFF_JFIF_JPEG); pixDestroy(&pixacc); #endif #if 0 /* Test pixCensusTransform() */ d = pixGetDepth(pixs); if (d == 32) pixt = pixConvertRGBToLuminance(pixs); else pixt = pixClone(pixs); pixacc = pixBlockconvAccum(pixt); pixd = pixCensusTransform(pixt, wc, NULL); pixDestroy(&pixt); pixDestroy(&pixacc); pixWrite(fileout, pixd, IFF_PNG); #endif #if 1 /* Test generic convolution with kel1 */ if (pixGetDepth(pixs) == 32) pixg = pixScaleRGBToGrayFast(pixs, 2, COLOR_GREEN); else pixg = pixScale(pixs, 0.5, 0.5); pixDisplay(pixg, 0, 600); kel1 = kernelCreateFromString(5, 5, 2, 2, kdatastr); pixd = pixConvolve(pixg, kel1, 8, 1); pixDisplay(pixd, 700, 0); pixWrite("/tmp/junkpixd4.bmp", pixd, IFF_BMP); pixDestroy(&pixd); kernelDestroy(&kel1); /* Test convolution with flat rectangular kel */ kel2 = kernelCreate(11, 11); kernelSetOrigin(kel2, 5, 5); for (i = 0; i < 11; i++) { for (j = 0; j < 11; j++) kernelSetElement(kel2, i, j, 1); } startTimer(); pixd = pixConvolve(pixg, kel2, 8, 1); fprintf(stderr, "Generic convolution: %7.3f sec\n", stopTimer()); pixDisplay(pixd, 1200, 0); pixWrite("/tmp/junkpixd5.bmp", pixd, IFF_BMP); startTimer(); pixt = pixBlockconv(pixg, 5, 5); fprintf(stderr, "Block convolution: %7.3f sec\n", stopTimer()); pixDisplay(pixd, 1200, 600); pixWrite("/tmp/junkpixd6.bmp", pixt, IFF_BMP); pixCompareGray(pixd, pixt, L_COMPARE_ABS_DIFF, GPLOT_X11, NULL, NULL, NULL, NULL); pixDestroy(&pixg); pixDestroy(&pixt); kernelDestroy(&kel2); #endif pixDestroy(&pixs); pixDestroy(&pixd); return 0; }
int main(int argc, char **argv) { l_int32 i, j, sizex, sizey, bias; FPIX *fpixv, *fpixrv; L_KERNEL *kel1, *kel2, *kel3x, *kel3y; PIX *pixs, *pixacc, *pixg, *pixt, *pixd; PIX *pixb, *pixm, *pixms, *pixrv, *pix1, *pix2, *pix3, *pix4; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; /* Test pixBlockconvGray() on 8 bpp */ pixs = pixRead("test8.jpg"); pixacc = pixBlockconvAccum(pixs); pixd = pixBlockconvGray(pixs, pixacc, 3, 5); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 0 */ pixDisplayWithTitle(pixd, 100, 0, NULL, rp->display); pixDestroy(&pixacc); pixDestroy(&pixd); /* Test pixBlockconv() on 8 bpp */ pixd = pixBlockconv(pixs, 9, 8); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 1 */ pixDisplayWithTitle(pixd, 200, 0, NULL, rp->display); pixDestroy(&pixd); pixDestroy(&pixs); /* Test pixBlockrank() on 1 bpp */ pixs = pixRead("test1.png"); pixacc = pixBlockconvAccum(pixs); for (i = 0; i < 3; i++) { pixd = pixBlockrank(pixs, pixacc, 4, 4, 0.25 + 0.25 * i); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 2 - 4 */ pixDisplayWithTitle(pixd, 300 + 100 * i, 0, NULL, rp->display); pixDestroy(&pixd); } /* Test pixBlocksum() on 1 bpp */ pixd = pixBlocksum(pixs, pixacc, 16, 16); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 5 */ pixDisplayWithTitle(pixd, 700, 0, NULL, rp->display); pixDestroy(&pixd); pixDestroy(&pixacc); pixDestroy(&pixs); /* Test pixCensusTransform() */ pixs = pixRead("test24.jpg"); pixg = pixScaleRGBToGrayFast(pixs, 2, COLOR_GREEN); pixd = pixCensusTransform(pixg, 10, NULL); regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 6 */ pixDisplayWithTitle(pixd, 800, 0, NULL, rp->display); pixDestroy(&pixd); /* Test generic convolution with kel1 */ kel1 = kernelCreateFromString(5, 5, 2, 2, kel1str); pixd = pixConvolve(pixg, kel1, 8, 1); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 7 */ pixDisplayWithTitle(pixd, 100, 500, NULL, rp->display); pixDestroy(&pixd); /* Test convolution with flat rectangular kel */ kel2 = kernelCreate(11, 11); kernelSetOrigin(kel2, 5, 5); for (i = 0; i < 11; i++) { for (j = 0; j < 11; j++) kernelSetElement(kel2, i, j, 1); } pixd = pixConvolve(pixg, kel2, 8, 1); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 8 */ pixDisplayWithTitle(pixd, 200, 500, NULL, rp->display); pixDestroy(&pixd); kernelDestroy(&kel1); kernelDestroy(&kel2); /* Test pixBlockconv() on 32 bpp */ pixt = pixScaleBySampling(pixs, 0.5, 0.5); pixd = pixBlockconv(pixt, 4, 6); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 9 */ pixDisplayWithTitle(pixd, 300, 500, NULL, rp->display); pixDestroy(&pixt); pixDestroy(&pixs); pixDestroy(&pixg); pixDestroy(&pixd); /* Test bias convolution non-separable with kel2 */ pixs = pixRead("marge.jpg"); pixg = pixScaleRGBToGrayFast(pixs, 2, COLOR_GREEN); kel2 = kernelCreateFromString(5, 5, 2, 2, kel2str); pixd = pixConvolveWithBias(pixg, kel2, NULL, TRUE, &bias); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 10 */ pixDisplayWithTitle(pixd, 400, 500, NULL, rp->display); fprintf(stderr, "bias = %d\n", bias); kernelDestroy(&kel2); pixDestroy(&pixd); /* Test bias convolution separable with kel3x and kel3y */ kel3x = kernelCreateFromString(1, 5, 0, 2, kel3xstr); kel3y = kernelCreateFromString(7, 1, 3, 0, kel3ystr); pixd = pixConvolveWithBias(pixg, kel3x, kel3y, TRUE, &bias); regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG); /* 11 */ pixDisplayWithTitle(pixd, 500, 500, NULL, rp->display); fprintf(stderr, "bias = %d\n", bias); kernelDestroy(&kel3x); kernelDestroy(&kel3y); pixDestroy(&pixd); pixDestroy(&pixs); pixDestroy(&pixg); /* Test pixWindowedMean() and pixWindowedMeanSquare() on 8 bpp */ pixs = pixRead("feyn-fract2.tif"); pixg = pixConvertTo8(pixs, 0); sizex = 5; sizey = 20; pixb = pixAddBorderGeneral(pixg, sizex + 1, sizex + 1, sizey + 1, sizey + 1, 0); pixm = pixWindowedMean(pixb, sizex, sizey, 1, 1); pixms = pixWindowedMeanSquare(pixb, sizex, sizey, 1); regTestWritePixAndCheck(rp, pixm, IFF_JFIF_JPEG); /* 12 */ pixDisplayWithTitle(pixm, 100, 0, NULL, rp->display); pixDestroy(&pixs); pixDestroy(&pixb); /* Test pixWindowedVariance() on 8 bpp */ pixWindowedVariance(pixm, pixms, &fpixv, &fpixrv); pixrv = fpixConvertToPix(fpixrv, 8, L_CLIP_TO_ZERO, 1); regTestWritePixAndCheck(rp, pixrv, IFF_JFIF_JPEG); /* 13 */ pixDisplayWithTitle(pixrv, 100, 250, NULL, rp->display); pix1 = fpixDisplayMaxDynamicRange(fpixv); pix2 = fpixDisplayMaxDynamicRange(fpixrv); pixDisplayWithTitle(pix1, 100, 500, "Variance", rp->display); pixDisplayWithTitle(pix2, 100, 750, "RMS deviation", rp->display); regTestWritePixAndCheck(rp, pix1, IFF_JFIF_JPEG); /* 14 */ regTestWritePixAndCheck(rp, pix2, IFF_JFIF_JPEG); /* 15 */ fpixDestroy(&fpixv); fpixDestroy(&fpixrv); pixDestroy(&pixm); pixDestroy(&pixms); pixDestroy(&pixrv); /* Test again all windowed functions with simpler interface */ pixWindowedStats(pixg, sizex, sizey, 0, NULL, NULL, &fpixv, &fpixrv); pix3 = fpixDisplayMaxDynamicRange(fpixv); pix4 = fpixDisplayMaxDynamicRange(fpixrv); regTestComparePix(rp, pix1, pix3); /* 16 */ regTestComparePix(rp, pix2, pix4); /* 17 */ pixDestroy(&pixg); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); fpixDestroy(&fpixv); fpixDestroy(&fpixrv); return regTestCleanup(rp); }
int main(int argc, char **argv) { l_int32 i, j, wc, hc, d, bias; L_KERNEL *kel1, *kel2, *kel3x, *kel3y; PIX *pix, *pixs, *pixg, *pixacc, *pixd, *pixt; char *filein, *fileout; static char mainName[] = "convolvetest"; if (argc != 5) return ERROR_INT(" Syntax: convolvetest filein wc hc fileout", mainName, 1); filein = argv[1]; wc = atoi(argv[2]); hc = atoi(argv[3]); fileout = argv[4]; if ((pix = pixRead(filein)) == NULL) return ERROR_INT("pix not made", mainName, 1); d = pixGetDepth(pix); if (d != 1 && d != 8 && d != 32) pixs = pixConvertTo8(pix, 0); else pixs = pixClone(pix); pixDestroy(&pix); d = pixGetDepth(pixs); if (d == 8 && (ALL || 0)) { /* Measure speed */ pixacc = pixBlockconvAccum(pixs); for (i = 0; i < NTIMES; i++) { pixd = pixBlockconvGray(pixs, pixacc, wc, hc); if ((i+1) % 10 == 0) fprintf(stderr, "%d iters\n", i + 1); pixDestroy(&pixd); } pixd = pixBlockconvGray(pixs, pixacc, wc, hc); pixWrite(fileout, pixd, IFF_JFIF_JPEG); pixDestroy(&pixacc); } if (d == 8 && (ALL || 0)) { /* Test pixBlockconvGray() */ pixacc = pixBlockconvAccum(pixs); pixd = pixBlockconvGray(pixs, pixacc, wc, hc); pixWrite(fileout, pixd, IFF_JFIF_JPEG); pixDestroy(&pixacc); } if (ALL || 0) { /* Test pixBlockconv() */ pixd = pixBlockconv(pixs, wc, hc); pixWrite(fileout, pixd, IFF_JFIF_JPEG); } if (d == 1 && (ALL || 0)) { /* Test pixBlockrank() */ pixacc = pixBlockconvAccum(pixs); pixd = pixBlockrank(pixs, pixacc, wc, hc, 0.5); pixWrite(fileout, pixd, IFF_TIFF_G4); pixDestroy(&pixacc); } if (d == 1 && (ALL || 0)) { /* Test pixBlocksum() */ pixacc = pixBlockconvAccum(pixs); pixd = pixBlocksum(pixs, pixacc, wc, hc); pixInvert(pixd, pixd); pixWrite(fileout, pixd, IFF_JFIF_JPEG); pixDestroy(&pixacc); } if (ALL || 0) { /* Test pixCensusTransform() */ d = pixGetDepth(pixs); if (d == 32) pixt = pixConvertRGBToLuminance(pixs); else pixt = pixClone(pixs); pixacc = pixBlockconvAccum(pixt); pixd = pixCensusTransform(pixt, wc, NULL); pixDestroy(&pixt); pixDestroy(&pixacc); pixWrite(fileout, pixd, IFF_PNG); } if (ALL || 0) { /* Test generic convolution with kel1 */ lept_mkdir("lept"); if (pixGetDepth(pixs) == 32) pixg = pixScaleRGBToGrayFast(pixs, 2, COLOR_GREEN); else pixg = pixScale(pixs, 0.5, 0.5); pixDisplay(pixg, 0, 600); kel1 = kernelCreateFromString(5, 5, 2, 2, kel1str); pixd = pixConvolve(pixg, kel1, 8, 1); pixDisplay(pixd, 700, 0); pixWrite("/tmp/lept/convol_d4.bmp", pixd, IFF_BMP); pixDestroy(&pixd); kernelDestroy(&kel1); /* Test convolution with flat rectangular kel */ kel2 = kernelCreate(11, 11); kernelSetOrigin(kel2, 5, 5); for (i = 0; i < 11; i++) { for (j = 0; j < 11; j++) kernelSetElement(kel2, i, j, 1); } startTimer(); pixd = pixConvolve(pixg, kel2, 8, 1); fprintf(stderr, "Generic convolution: %7.3f sec\n", stopTimer()); pixDisplay(pixd, 1200, 0); pixWrite("/tmp/lept/convol_d5.bmp", pixd, IFF_BMP); startTimer(); pixt = pixBlockconv(pixg, 5, 5); fprintf(stderr, "Block convolution: %7.3f sec\n", stopTimer()); pixDisplay(pixd, 1200, 600); pixWrite("/tmp/lept/convol_d6.bmp", pixt, IFF_BMP); pixCompareGray(pixd, pixt, L_COMPARE_ABS_DIFF, GPLOT_X11, NULL, NULL, NULL, NULL); pixDestroy(&pixg); pixDestroy(&pixt); kernelDestroy(&kel2); } if (ALL || 0) { /* Test bias convolution with kel2 */ if (pixGetDepth(pixs) == 32) pixg = pixScaleRGBToGrayFast(pixs, 2, COLOR_GREEN); else pixg = pixScale(pixs, 0.5, 0.5); pixDisplay(pixg, 0, 600); kel2 = kernelCreateFromString(5, 5, 2, 2, kel2str); pixd = pixConvolveWithBias(pixg, kel2, NULL, TRUE, &bias); pixDisplay(pixd, 700, 0); fprintf(stderr, "bias = %d\n", bias); pixWrite("/tmp/lept/convol_d6.png", pixd, IFF_PNG); pixDestroy(&pixg); kernelDestroy(&kel2); pixDestroy(&pixd); } if (ALL || 1) { /* Test separable bias convolution with kel3x, kel3y */ if (pixGetDepth(pixs) == 32) pixg = pixScaleRGBToGrayFast(pixs, 2, COLOR_GREEN); else pixg = pixScale(pixs, 0.5, 0.5); pixDisplay(pixg, 0, 600); kel3x = kernelCreateFromString(1, 5, 0, 2, kel3xstr); kel3y = kernelCreateFromString(7, 1, 3, 0, kel3ystr); pixd = pixConvolveWithBias(pixg, kel3x, kel3y, TRUE, &bias); pixDisplay(pixd, 700, 0); fprintf(stderr, "bias = %d\n", bias); pixWrite("/tmp/lept/convol_d7.png", pixd, IFF_PNG); pixDestroy(&pixg); kernelDestroy(&kel3x); kernelDestroy(&kel3y); pixDestroy(&pixd); } pixDestroy(&pixs); return 0; }