/*! * pixBilateral() * * Input: pixs (8 bpp gray or 32 bpp rgb, no colormap) * spatial_stdev (of gaussian kernel; in pixels, > 0.5) * range_stdev (of gaussian range kernel; > 5.0; typ. 50.0) * ncomps (number of intermediate sums J(k,x); in [4 ... 30]) * reduction (1, 2 or 4) * Return: pixd (bilateral filtered image), or null on error * * Notes: * (1) This performs a relatively fast, separable bilateral * filtering operation. The time is proportional to ncomps * and varies inversely approximately as the cube of the * reduction factor. See bilateral.h for algorithm details. * (2) We impose minimum values for range_stdev and ncomps to * avoid nasty artifacts when either are too small. We also * impose a constraint on their product: * ncomps * range_stdev >= 100. * So for values of range_stdev >= 25, ncomps can be as small as 4. * Here is a qualitative, intuitive explanation for this constraint. * Call the difference in k values between the J(k) == 'delta', where * 'delta' ~ 200 / ncomps * Then this constraint is roughly equivalent to the condition: * 'delta' < 2 * range_stdev * Note that at an intensity difference of (2 * range_stdev), the * range part of the kernel reduces the effect by the factor 0.14. * This constraint requires that we have a sufficient number of * PCBs (i.e, a small enough 'delta'), so that for any value of * image intensity I, there exists a k (and a PCB, J(k), such that * |I - k| < range_stdev * Any fewer PCBs and we don't have enough to support this condition. * (3) The upper limit of 30 on ncomps is imposed because the * gain in accuracy is not worth the extra computation. * (4) The size of the gaussian kernel is twice the spatial_stdev * on each side of the origin. The minimum value of * spatial_stdev, 0.5, is required to have a finite sized * spatial kernel. In practice, a much larger value is used. * (5) Computation of the intermediate images goes inversely * as the cube of the reduction factor. If you can use a * reduction of 2 or 4, it is well-advised. * (6) The range kernel is defined over the absolute value of pixel * grayscale differences, and hence must have size 256 x 1. * Values in the array represent the multiplying weight * depending on the absolute gray value difference between * the source pixel and the neighboring pixel, and should * be monotonically decreasing. * (7) Interesting observation. Run this on prog/fish24.jpg, with * range_stdev = 60, ncomps = 6, and spatial_dev = {10, 30, 50}. * As spatial_dev gets larger, we get the counter-intuitive * result that the body of the red fish becomes less blurry. */ PIX * pixBilateral(PIX *pixs, l_float32 spatial_stdev, l_float32 range_stdev, l_int32 ncomps, l_int32 reduction) { l_int32 d; l_float32 sstdev; /* scaled spatial stdev */ PIX *pixt, *pixr, *pixg, *pixb, *pixd; PROCNAME("pixBilateral"); if (!pixs || pixGetColormap(pixs)) return (PIX *)ERROR_PTR("pixs not defined or cmapped", procName, NULL); d = pixGetDepth(pixs); if (d != 8 && d != 32) return (PIX *)ERROR_PTR("pixs not 8 or 32 bpp", procName, NULL); if (reduction != 1 && reduction != 2 && reduction != 4) return (PIX *)ERROR_PTR("reduction invalid", procName, NULL); sstdev = spatial_stdev / (l_float32)reduction; /* reduced spat. stdev */ if (sstdev < 0.5) return (PIX *)ERROR_PTR("sstdev < 0.5", procName, NULL); if (range_stdev <= 5.0) return (PIX *)ERROR_PTR("range_stdev <= 5.0", procName, NULL); if (ncomps < 4 || ncomps > 30) return (PIX *)ERROR_PTR("ncomps not in [4 ... 30]", procName, NULL); if (ncomps * range_stdev < 100.0) return (PIX *)ERROR_PTR("ncomps * range_stdev < 100.0", procName, NULL); if (d == 8) return pixBilateralGray(pixs, spatial_stdev, range_stdev, ncomps, reduction); pixt = pixGetRGBComponent(pixs, COLOR_RED); pixr = pixBilateralGray(pixt, spatial_stdev, range_stdev, ncomps, reduction); pixDestroy(&pixt); pixt = pixGetRGBComponent(pixs, COLOR_GREEN); pixg = pixBilateralGray(pixt, spatial_stdev, range_stdev, ncomps, reduction); pixDestroy(&pixt); pixt = pixGetRGBComponent(pixs, COLOR_BLUE); pixb = pixBilateralGray(pixt, spatial_stdev, range_stdev, ncomps, reduction); pixDestroy(&pixt); pixd = pixCreateRGBImage(pixr, pixg, pixb); pixDestroy(&pixr); pixDestroy(&pixg); pixDestroy(&pixb); return pixd; }
/*! * pixApplyFilter() * * Input: pix (8 or 32 bpp; or 2, 4 or 8 bpp with colormap) * dpix (filter) * outflag (L_CLIP_TO_ZERO, L_TAKE_ABSVAL, * L_THRESH_NEG_TO_BLACK or L_THRESH_NEG_TO_WHITE) * Return: pixd, or null on error * * Notes: * (1) Apply the input filter to pix by multiplying it for the * Fourier transform of pix. The inverse Fourier transform * is then used on the result to return the filtered image. * (2) If pix is 32 bpp RGB, the filter is applied to each color * channel separately. * (3) If colormapped, remove to grayscale. */ PIX * pixApplyFilter(PIX *pixs, DPIX *dpix, l_int32 outflag) { l_int32 w, h, d; PIX *pixt, *pixd, *pixr, *pixrc, *pixg, *pixgc, *pixb, *pixbc; PROCNAME("pixApplyFilter"); if (!pixs && !dpix) return (PIX *)ERROR_PTR("pixs or dpix not defined", procName, NULL); /* Remove colormap if necessary */ pixGetDimensions(pixs, &w, &h, &d); if ((d == 2 || d == 4 || d == 8) && pixGetColormap(pixs)) { L_WARNING("pix has colormap; removing", procName); pixt = pixRemoveColormap(pixs, REMOVE_CMAP_BASED_ON_SRC); d = pixGetDepth(pixt); } else pixt = pixClone(pixs); if (d != 8 && d != 32) { pixDestroy(&pixt); return (PIX *)ERROR_PTR("depth not 8 or 32 bpp", procName, NULL); } if (d == 8) { pixd = pixApplyFilterGray(pixt, dpix, outflag); } else { /* d == 32 */ pixr = pixGetRGBComponent(pixt, COLOR_RED); pixrc = pixApplyFilterGray(pixr, dpix, outflag); pixDestroy(&pixr); pixg = pixGetRGBComponent(pixt, COLOR_GREEN); pixgc = pixApplyFilterGray(pixg, dpix, outflag); pixDestroy(&pixg); pixb = pixGetRGBComponent(pixt, COLOR_BLUE); pixbc = pixApplyFilterGray(pixb, dpix, outflag); pixDestroy(&pixb); pixd = pixCreateRGBImage(pixrc, pixgc, pixbc); pixDestroy(&pixrc); pixDestroy(&pixgc); pixDestroy(&pixbc); } pixDestroy(&pixt); return pixd; }
/*! * pixBilateralExact() * * Input: pixs (8 bpp gray or 32 bpp rgb) * spatial_kel (gaussian kernel) * range_kel (<optional> 256 x 1, monotonically decreasing) * Return: pixd (8 bpp bilateral filtered image) * * Notes: * (1) The spatial_kel is a conventional smoothing kernel, typically a * 2-d Gaussian kernel or other block kernel. It can be either * normalized or not, but must be everywhere positive. * (2) The range_kel is defined over the absolute value of pixel * grayscale differences, and hence must have size 256 x 1. * Values in the array represent the multiplying weight for each * gray value difference between the target pixel and center of the * kernel, and should be monotonically decreasing. * (3) If range_kel == NULL, a constant weight is applied regardless * of the range value difference. This degenerates to a regular * pixConvolve() with a normalized kernel. */ PIX * pixBilateralExact(PIX *pixs, L_KERNEL *spatial_kel, L_KERNEL *range_kel) { l_int32 d; PIX *pixt, *pixr, *pixg, *pixb, *pixd; PROCNAME("pixBilateralExact"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (pixGetColormap(pixs) != NULL) return (PIX *)ERROR_PTR("pixs is cmapped", procName, NULL); d = pixGetDepth(pixs); if (d != 8 && d != 32) return (PIX *)ERROR_PTR("pixs not 8 or 32 bpp", procName, NULL); if (!spatial_kel) return (PIX *)ERROR_PTR("spatial_ke not defined", procName, NULL); if (d == 8) { return pixBilateralGrayExact(pixs, spatial_kel, range_kel); } else { /* d == 32 */ pixt = pixGetRGBComponent(pixs, COLOR_RED); pixr = pixBilateralGrayExact(pixt, spatial_kel, range_kel); pixDestroy(&pixt); pixt = pixGetRGBComponent(pixs, COLOR_GREEN); pixg = pixBilateralGrayExact(pixt, spatial_kel, range_kel); pixDestroy(&pixt); pixt = pixGetRGBComponent(pixs, COLOR_BLUE); pixb = pixBilateralGrayExact(pixt, spatial_kel, range_kel); pixDestroy(&pixt); pixd = pixCreateRGBImage(pixr, pixg, pixb); pixDestroy(&pixr); pixDestroy(&pixg); pixDestroy(&pixb); return pixd; } }
/*! * pixRankFilterRGB() * * Input: pixs (32 bpp) * wf, hf (width and height of filter; each is >= 1) * rank (in [0.0 ... 1.0]) * Return: pixd (of rank values), or null on error * * Notes: * (1) This defines, for each pixel in pixs, a neighborhood of * pixels given by a rectangle "centered" on the pixel. * This set of wf*hf pixels has a distribution of values. * For each component, if the values are sorted in increasing * order, we choose the component such that rank*(wf*hf-1) * pixels have a lower or equal value and * (1-rank)*(wf*hf-1) pixels have an equal or greater value. * (2) Apply gray rank filtering to each component independently. * (3) See notes in pixRankFilterGray() for further details. */ PIX * pixRankFilterRGB(PIX *pixs, l_int32 wf, l_int32 hf, l_float32 rank) { PIX *pixr, *pixg, *pixb, *pixrf, *pixgf, *pixbf, *pixd; PROCNAME("pixRankFilterRGB"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (pixGetDepth(pixs) != 32) return (PIX *)ERROR_PTR("pixs not 32 bpp", procName, NULL); if (wf < 1 || hf < 1) return (PIX *)ERROR_PTR("wf < 1 || hf < 1", procName, NULL); if (rank < 0.0 || rank > 1.0) return (PIX *)ERROR_PTR("rank must be in [0.0, 1.0]", procName, NULL); if (wf == 1 && hf == 1) /* no-op */ return pixCopy(NULL, pixs); pixr = pixGetRGBComponent(pixs, COLOR_RED); pixg = pixGetRGBComponent(pixs, COLOR_GREEN); pixb = pixGetRGBComponent(pixs, COLOR_BLUE); pixrf = pixRankFilterGray(pixr, wf, hf, rank); pixgf = pixRankFilterGray(pixg, wf, hf, rank); pixbf = pixRankFilterGray(pixb, wf, hf, rank); pixd = pixCreateRGBImage(pixrf, pixgf, pixbf); pixDestroy(&pixr); pixDestroy(&pixg); pixDestroy(&pixb); pixDestroy(&pixrf); pixDestroy(&pixgf); pixDestroy(&pixbf); return pixd; }
/*! * pixBilinearColor() * * Input: pixs (32 bpp) * vc (vector of 8 coefficients for bilinear transformation) * colorval (e.g., 0 to bring in BLACK, 0xffffff00 for WHITE) * Return: pixd, or null on error */ PIX * pixBilinearColor(PIX *pixs, l_float32 *vc, l_uint32 colorval) { l_int32 i, j, w, h, d, wpls, wpld; l_uint32 val; l_uint32 *datas, *datad, *lined; l_float32 x, y; PIX *pix1, *pix2, *pixd; PROCNAME("pixBilinearColor"); if (!pixs) return (PIX *) ERROR_PTR("pixs not defined", procName, NULL); pixGetDimensions(pixs, &w, &h, &d); if (d != 32) return (PIX *) ERROR_PTR("pixs must be 32 bpp", procName, NULL); if (!vc) return (PIX *) ERROR_PTR("vc not defined", procName, NULL); datas = pixGetData(pixs); wpls = pixGetWpl(pixs); pixd = pixCreateTemplate(pixs); pixSetAllArbitrary(pixd, colorval); datad = pixGetData(pixd); wpld = pixGetWpl(pixd); /* Iterate over destination pixels */ for (i = 0; i < h; i++) { lined = datad + i * wpld; for (j = 0; j < w; j++) { /* Compute float src pixel location corresponding to (i,j) */ bilinearXformPt(vc, j, i, &x, &y); linearInterpolatePixelColor(datas, wpls, w, h, x, y, colorval, &val); *(lined + j) = val; } } /* If rgba, transform the pixs alpha channel and insert in pixd */ if (pixGetSpp(pixs) == 4) { pix1 = pixGetRGBComponent(pixs, L_ALPHA_CHANNEL); pix2 = pixBilinearGray(pix1, vc, 255); /* bring in opaque */ pixSetRGBComponent(pixd, pix2, L_ALPHA_CHANNEL); pixDestroy(&pix1); pixDestroy(&pix2); } return pixd; }
/*! * \brief pixRotate3Shear() * * \param[in] pixs * \param[in] xcen, ycen center of rotation * \param[in] angle radians * \param[in] incolor L_BRING_IN_WHITE, L_BRING_IN_BLACK; * \return pixd, or NULL on error. * * <pre> * Notes: * (1) This rotates the image about the given point, using the 3-shear * method. It should only be used for angles smaller than * LIMIT_SHEAR_ANGLE. For larger angles, a warning is issued. * (2) A positive angle gives a clockwise rotation. * (3) 3-shear rotation by a specified angle is equivalent * to the sequential transformations * y' = y + tan(angle/2) * (x - xcen) for first y-shear * x' = x + sin(angle) * (y - ycen) for x-shear * y' = y + tan(angle/2) * (x - xcen) for second y-shear * (4) Computation of tan(angle) is performed in the shear operations. * (5) This brings in 'incolor' pixels from outside the image. * (6) If the image has an alpha layer, it is rotated separately by * two shears. * (7) The algorithm was published by Alan Paeth: "A Fast Algorithm * for General Raster Rotation," Graphics Interface '86, * pp. 77-81, May 1986. A description of the method, along with * an implementation, can be found in Graphics Gems, p. 179, * edited by Andrew Glassner, published by Academic Press, 1990. * </pre> */ PIX * pixRotate3Shear(PIX *pixs, l_int32 xcen, l_int32 ycen, l_float32 angle, l_int32 incolor) { l_float32 hangle; PIX *pix1, *pix2, *pixd; PROCNAME("pixRotate3Shear"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (incolor != L_BRING_IN_WHITE && incolor != L_BRING_IN_BLACK) return (PIX *)(PIX *)ERROR_PTR("invalid incolor value", procName, NULL); if (L_ABS(angle) < MIN_ANGLE_TO_ROTATE) return pixClone(pixs); if (L_ABS(angle) > LIMIT_SHEAR_ANGLE) { L_WARNING("%6.2f radians; large angle for 3-shear rotation\n", procName, L_ABS(angle)); } hangle = atan(sin(angle)); if ((pixd = pixVShear(NULL, pixs, xcen, angle / 2., incolor)) == NULL) return (PIX *)ERROR_PTR("pixd not made", procName, NULL); if ((pix1 = pixHShear(NULL, pixd, ycen, hangle, incolor)) == NULL) { pixDestroy(&pixd); return (PIX *)ERROR_PTR("pix1 not made", procName, NULL); } pixVShear(pixd, pix1, xcen, angle / 2., incolor); pixDestroy(&pix1); if (pixGetDepth(pixs) == 32 && pixGetSpp(pixs) == 4) { pix1 = pixGetRGBComponent(pixs, L_ALPHA_CHANNEL); /* L_BRING_IN_WHITE brings in opaque for the alpha component */ pix2 = pixRotate3Shear(pix1, xcen, ycen, angle, L_BRING_IN_WHITE); pixSetRGBComponent(pixd, pix2, L_ALPHA_CHANNEL); pixDestroy(&pix1); pixDestroy(&pix2); } return pixd; }
/*! * pixRotateAMColorCorner() * * Input: pixs * angle (radians; clockwise is positive) * colorval (e.g., 0 to bring in BLACK, 0xffffff00 for WHITE) * Return: pixd, or null on error * * Notes: * (1) Rotates the image about the UL corner. * (2) A positive angle gives a clockwise rotation. * (3) Specify the color to be brought in from outside the image. */ PIX * pixRotateAMColorCorner(PIX *pixs, l_float32 angle, l_uint32 fillval) { l_int32 w, h, wpls, wpld; l_uint32 *datas, *datad; PIX *pix1, *pix2, *pixd; PROCNAME("pixRotateAMColorCorner"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (pixGetDepth(pixs) != 32) return (PIX *)ERROR_PTR("pixs must be 32 bpp", procName, NULL); if (L_ABS(angle) < MIN_ANGLE_TO_ROTATE) return pixClone(pixs); pixGetDimensions(pixs, &w, &h, NULL); datas = pixGetData(pixs); wpls = pixGetWpl(pixs); pixd = pixCreateTemplate(pixs); datad = pixGetData(pixd); wpld = pixGetWpl(pixd); rotateAMColorCornerLow(datad, w, h, wpld, datas, wpls, angle, fillval); if (pixGetSpp(pixs) == 4) { pix1 = pixGetRGBComponent(pixs, L_ALPHA_CHANNEL); pix2 = pixRotateAMGrayCorner(pix1, angle, 255); /* bring in opaque */ pixSetRGBComponent(pixd, pix2, L_ALPHA_CHANNEL); pixDestroy(&pix1); pixDestroy(&pix2); } return pixd; }
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); }
main(int argc, char **argv) { l_int32 i, j, w1, h1, w2, h2, w, h, same; BOX *box1, *box2; PIX *pixs, *pixs1, *pixs2, *pix1, *pix2; PIX *pixg, *pixg1, *pixg2, *pixc2, *pixbl, *pixd; PIXA *pixa; static char mainName[] = "blend2_reg"; /* --- Set up the 8 bpp blending image --- */ pixg = pixCreate(660, 500, 8); for (i = 0; i < 500; i++) for (j = 0; j < 660; j++) pixSetPixel(pixg, j, i, (l_int32)(0.775 * j) % 256); /* --- Set up the initial color images to be blended together --- */ pixs1 = pixRead("wyom.jpg"); pixs2 = pixRead("fish24.jpg"); pixGetDimensions(pixs1, &w1, &h1, NULL); pixGetDimensions(pixs2, &w2, &h2, NULL); h = L_MIN(h1, h2); w = L_MIN(w1, w2); box1 = boxCreate(0, 0, w1, h1); box2 = boxCreate(0, 300, 660, 500); pix1 = pixClipRectangle(pixs1, box1, NULL); pix2 = pixClipRectangle(pixs2, box2, NULL); pixDestroy(&pixs1); pixDestroy(&pixs2); boxDestroy(&box1); boxDestroy(&box2); /* --- Blend 2 rgb images --- */ pixa = pixaCreate(0); pixSaveTiled(pixg, pixa, 1, 1, 40, 32); pixd = pixBlendWithGrayMask(pix1, pix2, pixg, 50, 50); pixSaveTiled(pix1, pixa, 1, 1, 40, 32); pixSaveTiled(pix2, pixa, 1, 0, 40, 32); pixSaveTiled(pixd, pixa, 1, 0, 40, 32); pixDestroy(&pixd); /* --- Blend 2 grayscale images --- */ pixg1 = pixConvertRGBToLuminance(pix1); pixg2 = pixConvertRGBToLuminance(pix2); pixd = pixBlendWithGrayMask(pixg1, pixg2, pixg, 50, 50); pixSaveTiled(pixg1, pixa, 1, 1, 40, 32); pixSaveTiled(pixg2, pixa, 1, 0, 40, 32); pixSaveTiled(pixd, pixa, 1, 0, 40, 32); pixDestroy(&pixg1); pixDestroy(&pixg2); pixDestroy(&pixd); /* --- Blend a colormap image and an rgb image --- */ pixc2 = pixFixedOctcubeQuantGenRGB(pix2, 2); pixd = pixBlendWithGrayMask(pix1, pixc2, pixg, 50, 50); pixSaveTiled(pix1, pixa, 1, 1, 40, 32); pixSaveTiled(pixc2, pixa, 1, 0, 40, 32); pixSaveTiled(pixd, pixa, 1, 0, 40, 32); pixDestroy(&pixc2); pixDestroy(&pixd); /* --- Blend a colormap image and a grayscale image --- */ pixg1 = pixConvertRGBToLuminance(pix1); pixc2 = pixFixedOctcubeQuantGenRGB(pix2, 2); pixd = pixBlendWithGrayMask(pixg1, pixc2, pixg, 50, 50); pixSaveTiled(pixg1, pixa, 1, 1, 40, 32); pixSaveTiled(pixc2, pixa, 1, 0, 40, 32); pixSaveTiled(pixd, pixa, 1, 0, 40, 32); pixDestroy(&pixd); pixd = pixBlendWithGrayMask(pixg1, pixc2, pixg, -100, -100); pixSaveTiled(pixg1, pixa, 1, 1, 40, 32); pixSaveTiled(pixc2, pixa, 1, 0, 40, 32); pixSaveTiled(pixd, pixa, 1, 0, 40, 32); pixDestroy(&pixd); pixDestroy(&pixg1); pixDestroy(&pixc2); /* --- Test png read/write with alpha channel --- */ /* First make pixs1, using pixg as the alpha channel */ pixs = pixRead("fish24.jpg"); box1 = boxCreate(0, 300, 660, 500); pixs1 = pixClipRectangle(pixs, box1, NULL); pixSaveTiled(pixs1, pixa, 1, 1, 40, 32); pixSetRGBComponent(pixs1, pixg, L_ALPHA_CHANNEL); /* To see the alpha channel, blend with a black image */ pixbl = pixCreate(660, 500, 32); pixd = pixBlendWithGrayMask(pixbl, pixs1, NULL, 0, 0); pixSaveTiled(pixd, pixa, 1, 0, 40, 32); pixDestroy(&pixd); /* Write out the RGBA image and read it back */ l_pngSetWriteAlpha(1); pixWrite("/tmp/junkpixs1.png", pixs1, IFF_PNG); l_pngSetStripAlpha(0); pixs2 = pixRead("/tmp/junkpixs1.png"); /* Make sure that the alpha channel image hasn't changed */ pixg2 = pixGetRGBComponent(pixs2, L_ALPHA_CHANNEL); pixEqual(pixg, pixg2, &same); if (same) fprintf(stderr, "PNG with alpha read/write OK\n"); else fprintf(stderr, "PNG with alpha read/write failed\n"); /* Blend again with a black image */ pixd = pixBlendWithGrayMask(pixbl, pixs2, NULL, 0, 0); pixSaveTiled(pixd, pixa, 1, 0, 40, 32); pixDestroy(&pixd); /* Blend with a white image */ pixSetAll(pixbl); pixd = pixBlendWithGrayMask(pixbl, pixs2, NULL, 0, 0); pixSaveTiled(pixd, pixa, 1, 0, 40, 32); pixDestroy(&pixd); l_pngSetWriteAlpha(0); /* reset to default */ l_pngSetStripAlpha(1); /* reset to default */ pixDestroy(&pixbl); pixDestroy(&pixs); pixDestroy(&pixs1); pixDestroy(&pixs2); pixDestroy(&pixg2); boxDestroy(&box1); /* --- Display results --- */ pixd = pixaDisplay(pixa, 0, 0); pixDisplay(pixd, 100, 100); pixWrite("/tmp/junkblend2.jpg", pixd, IFF_JFIF_JPEG); pixDestroy(&pixd); pixaDestroy(&pixa); pixDestroy(&pixg); pixDestroy(&pix1); pixDestroy(&pix2); return 0; }
/*! * pixColorMorph() * * Input: pixs * type (L_MORPH_DILATE, L_MORPH_ERODE, L_MORPH_OPEN, * or L_MORPH_CLOSE) * hsize (of Sel; must be odd; origin implicitly in center) * vsize (ditto) * Return: pixd * * Notes: * (1) This does the morph operation on each component separately, * and recombines the result. * (2) Sel is a brick with all elements being hits. * (3) If hsize = vsize = 1, just returns a copy. */ PIX * pixColorMorph(PIX *pixs, l_int32 type, l_int32 hsize, l_int32 vsize) { PIX *pixr, *pixg, *pixb, *pixrm, *pixgm, *pixbm, *pixd; PROCNAME("pixColorMorph"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (pixGetDepth(pixs) != 32) return (PIX *)ERROR_PTR("pixs not 32 bpp", procName, NULL); if (type != L_MORPH_DILATE && type != L_MORPH_ERODE && type != L_MORPH_OPEN && type != L_MORPH_CLOSE) return (PIX *)ERROR_PTR("invalid morph type", procName, NULL); if (hsize < 1 || vsize < 1) return (PIX *)ERROR_PTR("hsize or vsize < 1", procName, NULL); if ((hsize & 1) == 0 ) { L_WARNING("horiz sel size must be odd; increasing by 1", procName); hsize++; } if ((vsize & 1) == 0 ) { L_WARNING("vert sel size must be odd; increasing by 1", procName); vsize++; } if (hsize == 1 && vsize == 1) return pixCopy(NULL, pixs); pixr = pixGetRGBComponent(pixs, COLOR_RED); pixg = pixGetRGBComponent(pixs, COLOR_GREEN); pixb = pixGetRGBComponent(pixs, COLOR_BLUE); if (type == L_MORPH_DILATE) { pixrm = pixDilateGray(pixr, hsize, vsize); pixgm = pixDilateGray(pixg, hsize, vsize); pixbm = pixDilateGray(pixb, hsize, vsize); } else if (type == L_MORPH_ERODE) { pixrm = pixErodeGray(pixr, hsize, vsize); pixgm = pixErodeGray(pixg, hsize, vsize); pixbm = pixErodeGray(pixb, hsize, vsize); } else if (type == L_MORPH_OPEN) { pixrm = pixOpenGray(pixr, hsize, vsize); pixgm = pixOpenGray(pixg, hsize, vsize); pixbm = pixOpenGray(pixb, hsize, vsize); } else { /* type == L_MORPH_CLOSE */ pixrm = pixCloseGray(pixr, hsize, vsize); pixgm = pixCloseGray(pixg, hsize, vsize); pixbm = pixCloseGray(pixb, hsize, vsize); } pixd = pixCreateRGBImage(pixrm, pixgm, pixbm); pixDestroy(&pixr); pixDestroy(&pixrm); pixDestroy(&pixg); pixDestroy(&pixgm); pixDestroy(&pixb); pixDestroy(&pixbm); return pixd; }