/*! * pixGetTextlineCenters() * * Input: pixs (1 bpp) * debugflag (1 for debug output) * Return: ptaa (of center values of textlines) * * Notes: * (1) This in general does not have a point for each value * of x, because there will be gaps between words. * It doesn't matter because we will fit a quadratic to the * points that we do have. */ PTAA * pixGetTextlineCenters(PIX *pixs, l_int32 debugflag) { l_int32 i, w, h, bx, by, nsegs; BOXA *boxa; PIX *pix, *pixt1, *pixt2, *pixt3; PIXA *pixa1, *pixa2; PTA *pta; PTAA *ptaa; PROCNAME("pixGetTextlineCenters"); if (!pixs || pixGetDepth(pixs) != 1) return (PTAA *)ERROR_PTR("pixs undefined or not 1 bpp", procName, NULL); pixGetDimensions(pixs, &w, &h, NULL); /* Filter to solidify the text lines within the x-height region, * and to remove most of the ascenders and descenders. */ pixt1 = pixMorphSequence(pixs, "c15.1 + o15.1 + c30.1", 0); pixDisplayWithTitle(pixt1, 0, 800, "pix1", debugflag); /* Get the 8-connected components ... */ boxa = pixConnComp(pixt1, &pixa1, 8); pixDestroy(&pixt1); boxaDestroy(&boxa); if (pixaGetCount(pixa1) == 0) { pixaDestroy(&pixa1); return NULL; } /* ... and remove the short and thin c.c */ pixa2 = pixaSelectBySize(pixa1, 100, 4, L_SELECT_IF_BOTH, L_SELECT_IF_GT, 0); if ((nsegs = pixaGetCount(pixa2)) == 0) { pixaDestroy(&pixa2); return NULL; } if (debugflag) { pixt2 = pixaDisplay(pixa2, w, h); pixDisplayWithTitle(pixt2, 800, 800, "pix2", 1); pixDestroy(&pixt2); } /* For each c.c., get the weighted center of each vertical column. * The result is a set of points going approximately through * the center of the x-height part of the text line. */ ptaa = ptaaCreate(nsegs); for (i = 0; i < nsegs; i++) { pixaGetBoxGeometry(pixa2, i, &bx, &by, NULL, NULL); pix = pixaGetPix(pixa2, i, L_CLONE); pta = pixGetMeanVerticals(pix, bx, by); ptaaAddPta(ptaa, pta, L_INSERT); pixDestroy(&pix); } if (debugflag) { pixt3 = pixCreateTemplate(pixt2); pix = pixDisplayPtaa(pixt3, ptaa); pixDisplayWithTitle(pix, 0, 1400, "pix3", 1); pixDestroy(&pix); pixDestroy(&pixt3); } pixaDestroy(&pixa1); pixaDestroy(&pixa2); return ptaa; }
/*! * pixSelectBySize() * * Input: pixs (1 bpp) * width, height (threshold dimensions) * connectivity (4 or 8) * type (L_SELECT_WIDTH, L_SELECT_HEIGHT, * L_SELECT_IF_EITHER, L_SELECT_IF_BOTH) * relation (L_SELECT_IF_LT, L_SELECT_IF_GT, * L_SELECT_IF_LTE, L_SELECT_IF_GTE) * &changed (<optional return> 1 if changed; 0 otherwise) * Return: filtered pixd, or null on error * * Notes: * (1) The args specify constraints on the size of the * components that are kept. * (2) If unchanged, returns a copy of pixs. Otherwise, * returns a new pix with the filtered components. * (3) If the selection type is L_SELECT_WIDTH, the input * height is ignored, and v.v. * (4) To keep small components, use relation = L_SELECT_IF_LT or * L_SELECT_IF_LTE. * To keep large components, use relation = L_SELECT_IF_GT or * L_SELECT_IF_GTE. */ PIX * pixSelectBySize(PIX *pixs, l_int32 width, l_int32 height, l_int32 connectivity, l_int32 type, l_int32 relation, l_int32 *pchanged) { l_int32 w, h, empty, changed, count; BOXA *boxa; PIX *pixd; PIXA *pixas, *pixad; PROCNAME("pixSelectBySize"); if (!pixs) return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); if (connectivity != 4 && connectivity != 8) return (PIX *)ERROR_PTR("connectivity not 4 or 8", procName, NULL); if (type != L_SELECT_WIDTH && type != L_SELECT_HEIGHT && type != L_SELECT_IF_EITHER && type != L_SELECT_IF_BOTH) return (PIX *)ERROR_PTR("invalid type", procName, NULL); if (relation != L_SELECT_IF_LT && relation != L_SELECT_IF_GT && relation != L_SELECT_IF_LTE && relation != L_SELECT_IF_GTE) return (PIX *)ERROR_PTR("invalid relation", procName, NULL); if (pchanged) *pchanged = FALSE; /* Check if any components exist */ pixZero(pixs, &empty); if (empty) return pixCopy(NULL, pixs); /* Identify and select the components */ boxa = pixConnComp(pixs, &pixas, connectivity); pixad = pixaSelectBySize(pixas, width, height, type, relation, &changed); boxaDestroy(&boxa); pixaDestroy(&pixas); /* Render the result */ if (!changed) { pixaDestroy(&pixad); return pixCopy(NULL, pixs); } else { if (pchanged) *pchanged = TRUE; pixGetDimensions(pixs, &w, &h, NULL); count = pixaGetCount(pixad); if (count == 0) /* return empty pix */ pixd = pixCreateTemplate(pixs); else { pixd = pixaDisplay(pixad, w, h); pixCopyResolution(pixd, pixs); pixCopyColormap(pixd, pixs); pixCopyText(pixd, pixs); pixCopyInputFormat(pixd, pixs); } pixaDestroy(&pixad); return pixd; } }
main(int argc, char **argv) { l_int32 ws, hs; BOX *box; BOXA *boxa; PIX *pixs, *pixc, *pix32, *pixt, *pixd; PIXA *pixat, *pixas, *pixac; static char mainName[] = "pixadisp_reg"; if (argc != 1) exit(ERROR_INT(" Syntax: pixadisp_reg", mainName, 1)); if ((pixs = pixRead("feyn.tif")) == NULL) exit(ERROR_INT("pixs not made", mainName, 1)); box = boxCreate(683, 799, 970, 479); pixc = pixClipRectangle(pixs, box, NULL); boxDestroy(&box); pixDisplayWrite(pixc, 1); if ((pix32 = pixRead("marge.jpg")) == NULL) exit(ERROR_INT("pix32 not made", mainName, 1)); /* Generate pixas from pixs and pixac from pixc */ boxa = pixConnComp(pixs, &pixat, 8); pixas = pixaSelectBySize(pixat, 60, 60, L_SELECT_IF_BOTH, L_SELECT_IF_LTE, NULL); pixaDestroy(&pixat); boxaDestroy(&boxa); boxa = pixConnComp(pixc, &pixac, 8); boxaDestroy(&boxa); /* pixaDisplay() */ pixGetDimensions(pixs, &ws, &hs, NULL); pixd = pixaDisplay(pixas, ws, hs); pixDisplayWrite(pixd, 1); pixDestroy(&pixd); /* pixaDisplayRandomCmap() */ pixd = pixaDisplayRandomCmap(pixas, ws, hs); /* black bg */ pixDisplayWrite(pixd, 1); pixcmapResetColor(pixGetColormap(pixd), 0, 255, 255, 255); /* white bg */ pixDisplayWrite(pixd, 1); pixDestroy(&pixd); /* pixaDisplayOnLattice() */ pixd = pixaDisplayOnLattice(pixac, 50, 50); pixDisplayWrite(pixd, 1); pixDestroy(&pixd); /* pixaDisplayUnsplit() */ pixat = pixaSplitPix(pix32, 5, 7, 10, 0x0000ff00); pixd = pixaDisplayUnsplit(pixat, 5, 7, 10, 0x00ff0000); pixDisplayWrite(pixd, 1); pixaDestroy(&pixat); pixDestroy(&pixd); /* pixaDisplayTiled() */ pixd = pixaDisplayTiled(pixac, 1000, 0, 10); pixDisplayWrite(pixd, 1); pixDestroy(&pixd); /* pixaDisplayTiledInRows() */ pixd = pixaDisplayTiledInRows(pixac, 1, 1000, 1.0, 0, 10, 2); pixDisplayWrite(pixd, 1); pixDestroy(&pixd); /* pixaDisplayTiledAndScaled() */ pixd = pixaDisplayTiledAndScaled(pixac, 1, 25, 20, 0, 5, 0); pixDisplayWrite(pixd, 1); pixDestroy(&pixd); pixat = pixaCreate(10); pixd = pixRankFilter(pix32, 8, 8, 0.5); pixaAddPix(pixat, pixd, L_INSERT); pixt = pixScale(pix32, 0.5, 0.5); pixd = pixRankFilter(pixt, 8, 8, 0.5); pixaAddPix(pixat, pixd, L_INSERT); pixDestroy(&pixt); pixt = pixScale(pix32, 0.25, 0.25); pixd = pixRankFilter(pixt, 8, 8, 0.5); pixaAddPix(pixat, pixd, L_INSERT); pixDestroy(&pixt); pixd = pixaDisplayTiledAndScaled(pixat, 32, 500, 1, 0, 25, 0); pixDisplayWrite(pixd, 1); pixaDestroy(&pixat); pixDestroy(&pixd); pixaDestroy(&pixas); pixaDestroy(&pixac); pixDestroy(&pixs); pixDestroy(&pixc); pixDestroy(&pix32); pixDisplayMultiple("/tmp/junk_write_display*"); return 0; }