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; }