/*! * \brief pixModifyStrokeWidth() * * \param[in] pixa of 1 bpp pix * \param[in] width measured average stroke width * \param[in] targetw desired stroke width * \return pix with modified stroke width, or NULL on error */ PIX * pixModifyStrokeWidth(PIX *pixs, l_float32 width, l_float32 targetw) { char buf[16]; l_int32 diff, size; PROCNAME("pixModifyStrokeWidth"); if (!pixs || (pixGetDepth(pixs) != 1)) return (PIX *)ERROR_PTR("pixs undefined or not 1 bpp", procName, NULL); if (targetw < 1) return (PIX *)ERROR_PTR("target width < 1", procName, NULL); diff = lept_roundftoi(targetw - width); if (diff == 0) return pixCopy(NULL, pixs); size = L_ABS(diff) + 1; if (diff < 0) /* erode */ snprintf(buf, sizeof(buf), "e%d.%d", size, size); else /* diff > 0; dilate */ snprintf(buf, sizeof(buf), "d%d.%d", size, size); return pixMorphSequence(pixs, buf, 0); }
l_int32 main(int argc, char **argv) { char buf[512]; l_int32 delx, dely, etransx, etransy, w, h, area1, area2; l_int32 *stab, *ctab; l_float32 cx1, cy1, cx2, cy2, score; PIX *pix0, *pix1, *pix2; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; /* ------------ Test of pixBestCorrelation() --------------- */ pix0 = pixRead("harmoniam100-11.png"); pix1 = pixConvertTo1(pix0, 160); pixGetDimensions(pix1, &w, &h, NULL); /* Now make a smaller image, translated by (-32, -12) * Except for the resizing, this is equivalent to * pix2 = pixTranslate(NULL, pix1, -32, -12, L_BRING_IN_WHITE); */ pix2 = pixCreate(w - 10, h, 1); pixRasterop(pix2, 0, 0, w, h, PIX_SRC, pix1, 32, 12); /* Get the number of FG pixels and the centroid locations */ stab = makePixelSumTab8(); ctab = makePixelCentroidTab8(); pixCountPixels(pix1, &area1, stab); pixCountPixels(pix2, &area2, stab); pixCentroid(pix1, ctab, stab, &cx1, &cy1); pixCentroid(pix2, ctab, stab, &cx2, &cy2); etransx = lept_roundftoi(cx1 - cx2); etransy = lept_roundftoi(cy1 - cy2); fprintf(stderr, "delta cx = %d, delta cy = %d\n", etransx, etransy); /* Get the best correlation, searching around the translation * where the centroids coincide */ pixBestCorrelation(pix1, pix2, area1, area2, etransx, etransy, 4, stab, &delx, &dely, &score, 5); fprintf(stderr, "delx = %d, dely = %d, score = %7.4f\n", delx, dely, score); regTestCompareValues(rp, 32, delx, 0); /* 0 */ regTestCompareValues(rp, 12, dely, 0); /* 1 */ regTestCheckFile(rp, "/tmp/junkcorrel_5.png"); /* 2 */ lept_rm(NULL, "junkcorrel_5.png"); FREE(stab); FREE(ctab); pixDestroy(&pix0); pixDestroy(&pix1); pixDestroy(&pix2); /* ------------ Test of pixCompareWithTranslation() ------------ */ /* Now use the pyramid to get the result. Do a translation * to remove pixels at the bottom from pix2, so that the * centroids are initially far apart. */ pix1 = pixRead("harmoniam-11.tif"); pix2 = pixTranslate(NULL, pix1, -45, 25, L_BRING_IN_WHITE); l_pdfSetDateAndVersion(0); pixCompareWithTranslation(pix1, pix2, 160, &delx, &dely, &score, 1); pixDestroy(&pix1); pixDestroy(&pix2); fprintf(stderr, "delx = %d, dely = %d\n", delx, dely); regTestCompareValues(rp, 45, delx, 0); /* 3 */ regTestCompareValues(rp, -25, dely, 0); /* 4 */ regTestCheckFile(rp, "/tmp/junkcmp.pdf"); /* 5 */ regTestCheckFile(rp, "/tmp/junkcorrel.pdf"); /* 6 */ return regTestCleanup(rp); }
l_int32 main(int argc, char **argv) { l_int32 irval, igval, ibval; l_float32 rval, gval, bval, fract, fgfract; L_BMF *bmf; BOX *box; BOXA *boxa; FPIX *fpix; PIX *pixs, *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7; PIX *pix8, *pix9, *pix10, *pix11, *pix12, *pix13, *pix14, *pix15; PIXA *pixa; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; pixa = pixaCreate(0); pixs = pixRead("breviar38.150.jpg"); /* pixs = pixRead("breviar32.150.jpg"); */ pixaAddPix(pixa, pixs, L_CLONE); regTestWritePixAndCheck(rp, pixs, IFF_JFIF_JPEG); /* 0 */ pixDisplayWithTitle(pixs, 0, 0, "Input image", rp->display); /* Extract the blue component, which is small in all the text * regions, including in the highlight color region */ pix1 = pixGetRGBComponent(pixs, COLOR_BLUE); pixaAddPix(pixa, pix1, L_CLONE); regTestWritePixAndCheck(rp, pix1, IFF_JFIF_JPEG); /* 1 */ pixDisplayWithTitle(pix1, 200, 0, "Blue component", rp->display); /* Do a background normalization, with the background set to * approximately 200 */ pix2 = pixBackgroundNormSimple(pix1, NULL, NULL); pixaAddPix(pixa, pix2, L_COPY); regTestWritePixAndCheck(rp, pix2, IFF_JFIF_JPEG); /* 2 */ pixDisplayWithTitle(pix2, 400, 0, "BG normalized to 200", rp->display); /* Do a linear transform on the gray pixels, with 50 going to * black and 160 going to white. 50 is sufficiently low to * make both the red and black print quite dark. Quantize * to a few equally spaced gray levels. This is the image * to which highlight color will be applied. */ pixGammaTRC(pix2, pix2, 1.0, 50, 160); pix3 = pixThresholdOn8bpp(pix2, 7, 1); pixaAddPix(pixa, pix3, L_CLONE); regTestWritePixAndCheck(rp, pix3, IFF_JFIF_JPEG); /* 3 */ pixDisplayWithTitle(pix3, 600, 0, "Basic quantized with white bg", rp->display); /* Identify the regions of red text. First, make a mask * consisting of all pixels such that (R-B)/B is larger * than 2.0. This will have all the red, plus a lot of * the dark pixels. */ fpix = pixComponentFunction(pixs, 1.0, 0.0, -1.0, 0.0, 0.0, 1.0); pix4 = fpixThresholdToPix(fpix, 2.0); pixInvert(pix4, pix4); /* red plus some dark text */ pixaAddPix(pixa, pix4, L_CLONE); regTestWritePixAndCheck(rp, pix4, IFF_PNG); /* 4 */ pixDisplayWithTitle(pix4, 800, 0, "Red plus dark pixels", rp->display); /* Make a mask consisting of all the red and background pixels */ pix5 = pixGetRGBComponent(pixs, COLOR_RED); pix6 = pixThresholdToBinary(pix5, 128); pixInvert(pix6, pix6); /* red plus background (white) */ /* Intersect the two masks to get a mask consisting of pixels * that are almost certainly red. This is the seed. */ pix7 = pixAnd(NULL, pix4, pix6); /* red only (seed) */ pixaAddPix(pixa, pix7, L_COPY); regTestWritePixAndCheck(rp, pix7, IFF_PNG); /* 5 */ pixDisplayWithTitle(pix7, 0, 600, "Seed for red color", rp->display); /* Make the clipping mask by thresholding the image with * the background cleaned to white. */ pix8 = pixThresholdToBinary(pix2, 230); /* mask */ pixaAddPix(pixa, pix8, L_CLONE); regTestWritePixAndCheck(rp, pix8, IFF_PNG); /* 6 */ pixDisplayWithTitle(pix8, 200, 600, "Clipping mask for red components", rp->display); /* Fill into the mask from the seed */ pixSeedfillBinary(pix7, pix7, pix8, 8); /* filled: red plus touching */ regTestWritePixAndCheck(rp, pix7, IFF_PNG); /* 7 */ pixDisplayWithTitle(pix7, 400, 600, "Red component mask filled", rp->display); /* Remove long horizontal and vertical lines from the filled result */ pix9 = pixMorphSequence(pix7, "o40.1", 0); pixSubtract(pix7, pix7, pix9); /* remove long horizontal lines */ pixDestroy(&pix9); pix9 = pixMorphSequence(pix7, "o1.40", 0); pixSubtract(pix7, pix7, pix9); /* remove long vertical lines */ /* Close the regions to be colored */ pix10 = pixMorphSequence(pix7, "c5.1", 0); pixaAddPix(pixa, pix10, L_CLONE); regTestWritePixAndCheck(rp, pix10, IFF_PNG); /* 8 */ pixDisplayWithTitle(pix10, 600, 600, "Components defining regions allowing coloring", rp->display); /* Sanity check on amount to be colored. Only accept images * with less than 10% of all the pixels with highlight color */ pixForegroundFraction(pix10, &fgfract); if (fgfract >= 0.10) { L_INFO("too much highlighting: fract = %6.3f; removing it\n", rp->testname, fgfract); pixClearAll(pix10); pixSetPixel(pix10, 0, 0, 1); } /* Get the bounding boxes of the regions to be colored */ boxa = pixConnCompBB(pix10, 8); /* Get a color to paint that is representative of the * actual highlight color in the image. Scale each * color component up from the average by an amount necessary * to saturate the red. Then divide the green and * blue components by 2.0. */ pixGetAverageMaskedRGB(pixs, pix7, 0, 0, 1, L_MEAN_ABSVAL, &rval, &gval, &bval); fract = 255.0 / rval; irval = lept_roundftoi(fract * rval); igval = lept_roundftoi(fract * gval / 2.0); ibval = lept_roundftoi(fract * bval / 2.0); fprintf(stderr, "(r,g,b) = (%d,%d,%d)\n", irval, igval, ibval); /* Color the quantized gray version in the selected regions */ pix11 = pixColorGrayRegions(pix3, boxa, L_PAINT_DARK, 220, irval, igval, ibval); pixaAddPix(pixa, pix11, L_CLONE); regTestWritePixAndCheck(rp, pix11, IFF_PNG); /* 9 */ pixDisplayWithTitle(pix11, 800, 600, "Final colored result", rp->display); pixaAddPix(pixa, pixs, L_CLONE); /* Test colorization on gray and cmapped gray */ pix12 = pixColorGrayRegions(pix2, boxa, L_PAINT_DARK, 220, 0, 255, 0); pixaAddPix(pixa, pix12, L_CLONE); regTestWritePixAndCheck(rp, pix12, IFF_PNG); /* 10 */ pixDisplayWithTitle(pix12, 900, 600, "Colorizing boxa gray", rp->display); box = boxCreate(200, 200, 250, 350); pix13 = pixCopy(NULL, pix2); pixColorGray(pix13, box, L_PAINT_DARK, 220, 0, 0, 255); pixaAddPix(pixa, pix13, L_CLONE); regTestWritePixAndCheck(rp, pix13, IFF_PNG); /* 11 */ pixDisplayWithTitle(pix13, 1000, 600, "Colorizing box gray", rp->display); pix14 = pixThresholdTo4bpp(pix2, 6, 1); pix15 = pixColorGrayRegions(pix14, boxa, L_PAINT_DARK, 220, 0, 0, 255); pixaAddPix(pixa, pix15, L_CLONE); regTestWritePixAndCheck(rp, pix15, IFF_PNG); /* 12 */ pixDisplayWithTitle(pix15, 1100, 600, "Colorizing boxa cmap", rp->display); pixColorGrayCmap(pix14, box, L_PAINT_DARK, 0, 255, 255); pixaAddPix(pixa, pix14, L_CLONE); regTestWritePixAndCheck(rp, pix14, IFF_PNG); /* 13 */ pixDisplayWithTitle(pix14, 1200, 600, "Colorizing box cmap", rp->display); boxDestroy(&box); /* Generate a pdf of the intermediate results */ lept_mkdir("lept"); L_INFO("Writing to /tmp/lept/colorize.pdf\n", rp->testname); pixaConvertToPdf(pixa, 90, 1.0, 0, 0, "Colorizing highlighted text", "/tmp/lept/colorize.pdf"); pixaDestroy(&pixa); fpixDestroy(&fpix); boxDestroy(&box); boxaDestroy(&boxa); pixDestroy(&pixs); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); pixDestroy(&pix6); pixDestroy(&pix7); pixDestroy(&pix8); pixDestroy(&pix9); pixDestroy(&pix10); pixDestroy(&pix11); pixDestroy(&pix12); pixDestroy(&pix13); pixDestroy(&pix14); pixDestroy(&pix15); /* Test the color detector */ pixa = pixaCreate(7); bmf = bmfCreate("./fonts", 4); pix1 = TestForRedColor(rp, "brev06.75.jpg", 1, bmf); /* 14 */ pixaAddPix(pixa, pix1, L_INSERT); pix1 = TestForRedColor(rp, "brev10.75.jpg", 0, bmf); /* 15 */ pixaAddPix(pixa, pix1, L_INSERT); pix1 = TestForRedColor(rp, "brev14.75.jpg", 1, bmf); /* 16 */ pixaAddPix(pixa, pix1, L_INSERT); pix1 = TestForRedColor(rp, "brev20.75.jpg", 1, bmf); /* 17 */ pixaAddPix(pixa, pix1, L_INSERT); pix1 = TestForRedColor(rp, "brev36.75.jpg", 0, bmf); /* 18 */ pixaAddPix(pixa, pix1, L_INSERT); pix1 = TestForRedColor(rp, "brev53.75.jpg", 1, bmf); /* 19 */ pixaAddPix(pixa, pix1, L_INSERT); pix1 = TestForRedColor(rp, "brev56.75.jpg", 1, bmf); /* 20 */ pixaAddPix(pixa, pix1, L_INSERT); /* Generate a pdf of the color detector results */ L_INFO("Writing to /tmp/lept/colordetect.pdf\n", rp->testname); pixaConvertToPdf(pixa, 45, 1.0, 0, 0, "Color detection", "/tmp/lept/colordetect.pdf"); pixaDestroy(&pixa); bmfDestroy(&bmf); return regTestCleanup(rp); }
l_int32 main(int argc, char **argv) { l_int32 delx, dely, etransx, etransy, w, h, area1, area2; l_int32 *stab, *ctab; l_float32 cx1, cy1, cx2, cy2, score, fract; PIX *pix0, *pix1, *pix2, *pix3, *pix4, *pix5; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; /* ------------ Test of pixBestCorrelation() --------------- */ pix0 = pixRead("harmoniam100-11.png"); pix1 = pixConvertTo1(pix0, 160); pixGetDimensions(pix1, &w, &h, NULL); /* Now make a smaller image, translated by (-32, -12) * Except for the resizing, this is equivalent to * pix2 = pixTranslate(NULL, pix1, -32, -12, L_BRING_IN_WHITE); */ pix2 = pixCreate(w - 10, h, 1); pixRasterop(pix2, 0, 0, w, h, PIX_SRC, pix1, 32, 12); /* Get the number of FG pixels and the centroid locations */ stab = makePixelSumTab8(); ctab = makePixelCentroidTab8(); pixCountPixels(pix1, &area1, stab); pixCountPixels(pix2, &area2, stab); pixCentroid(pix1, ctab, stab, &cx1, &cy1); pixCentroid(pix2, ctab, stab, &cx2, &cy2); etransx = lept_roundftoi(cx1 - cx2); etransy = lept_roundftoi(cy1 - cy2); fprintf(stderr, "delta cx = %d, delta cy = %d\n", etransx, etransy); /* Get the best correlation, searching around the translation * where the centroids coincide */ pixBestCorrelation(pix1, pix2, area1, area2, etransx, etransy, 4, stab, &delx, &dely, &score, 5); fprintf(stderr, "delx = %d, dely = %d, score = %7.4f\n", delx, dely, score); regTestCompareValues(rp, 32, delx, 0); /* 0 */ regTestCompareValues(rp, 12, dely, 0); /* 1 */ lept_mv("/tmp/lept/correl_5.png", "regout", NULL, NULL); regTestCheckFile(rp, "/tmp/regout/correl_5.png"); /* 2 */ lept_free(stab); lept_free(ctab); pixDestroy(&pix0); pixDestroy(&pix1); pixDestroy(&pix2); /* ------------ Test of pixCompareWithTranslation() ------------ */ /* Now use the pyramid to get the result. Do a translation * to remove pixels at the bottom from pix2, so that the * centroids are initially far apart. */ pix1 = pixRead("harmoniam-11.tif"); pix2 = pixTranslate(NULL, pix1, -45, 25, L_BRING_IN_WHITE); l_pdfSetDateAndVersion(0); pixCompareWithTranslation(pix1, pix2, 160, &delx, &dely, &score, 1); pixDestroy(&pix1); pixDestroy(&pix2); fprintf(stderr, "delx = %d, dely = %d\n", delx, dely); regTestCompareValues(rp, 45, delx, 0); /* 3 */ regTestCompareValues(rp, -25, dely, 0); /* 4 */ lept_mv("/tmp/lept/correl.pdf", "regout", NULL, NULL); lept_mv("/tmp/lept/compare.pdf", "regout", NULL, NULL); regTestCheckFile(rp, "/tmp/regout/compare.pdf"); /* 5 */ regTestCheckFile(rp, "/tmp/regout/correl.pdf"); /* 6 */ /* ------------ Test of pixGetPerceptualDiff() --------------- */ pix0 = pixRead("greencover.jpg"); pix1 = pixRead("redcover.jpg"); /* pre-scaled to the same size */ /* Apply directly to the color images */ pixGetPerceptualDiff(pix0, pix1, 1, 3, 20, &fract, &pix2, &pix3); fprintf(stderr, "Fraction of color pixels = %f\n", fract); regTestCompareValues(rp, 0.061252, fract, 0.01); /* 7 */ regTestWritePixAndCheck(rp, pix2, IFF_JFIF_JPEG); /* 8 */ regTestWritePixAndCheck(rp, pix3, IFF_TIFF_G4); /* 9 */ pixDestroy(&pix2); pixDestroy(&pix3); /* Apply to grayscale images */ pix2 = pixConvertTo8(pix0, 0); pix3 = pixConvertTo8(pix1, 0); pixGetPerceptualDiff(pix2, pix3, 1, 3, 20, &fract, &pix4, &pix5); fprintf(stderr, "Fraction of grayscale pixels = %f\n", fract); regTestCompareValues(rp, 0.046928, fract, 0.0002); /* 10 */ regTestWritePixAndCheck(rp, pix4, IFF_JFIF_JPEG); /* 11 */ regTestWritePixAndCheck(rp, pix5, IFF_TIFF_G4); /* 12 */ pixDestroy(&pix0); pixDestroy(&pix1); pixDestroy(&pix2); pixDestroy(&pix3); pixDestroy(&pix4); pixDestroy(&pix5); return regTestCleanup(rp); }