Esempio n. 1
0
int main_find_pattern(int    argc,
	char **argv)
{
	char        *filein, *fileout, *patternfile;
	l_int32      w, h, i, n;
	BOX         *box, *boxe;
	BOXA        *boxa1, *boxa2;
	PIX         *pixs, *pixp, *pixpe;
	PIX         *pixd, *pixt1, *pixt2, *pixhmt;
	SEL         *sel_2h, *sel;
	static char  mainName[] = "findpattern1";

	filein = "feyn.tif";
	patternfile = "char.tif";
	fileout = "result.findpattern1";

	if ((pixs = pixRead(filein)) == NULL)
		printf("pixs not made\n");
	if ((pixp = pixRead(patternfile)) == NULL)
		printf("pixp not made\n");
	w = pixGetWidth(pixp);
	h = pixGetHeight(pixp);

	/* generate the hit-miss Sel with runs */
	sel = pixGenerateSelWithRuns(pixp, NumHorLines, NumVertLines, 0,
		MinRunlength, 7, 7, 0, 0, &pixpe);

	/* display the Sel two ways */
	selWriteStream(stderr, sel);
	pixt1 = pixDisplayHitMissSel(pixpe, sel, 9, HitColor, MissColor);
	pixDisplay(pixt1, 200, 200);
	pixWrite("junkpixt", pixt1, IFF_PNG);

	/* use the Sel to find all instances in the page */
	startTimer();
	pixhmt = pixHMT(NULL, pixs, sel);
	fprintf(stderr, "Time to find patterns = %7.3f\n", stopTimer());

	/* small erosion to remove noise; typically not necessary if
	* there are enough elements in the Sel */
	sel_2h = selCreateBrick(1, 2, 0, 0, SEL_HIT);
	pixt2 = pixErode(NULL, pixhmt, sel_2h);

	/* display the result visually by placing the Sel at each
	* location found */
	pixd = pixDilate(NULL, pixt2, sel);
	pixWrite(fileout, pixd, IFF_TIFF_G4);

	/* display outut with an outline around each located pattern */
	boxa1 = pixConnCompBB(pixt2, 8);
	n = boxaGetCount(boxa1);
	boxa2 = boxaCreate(n);
	for (i = 0; i < n; i++) {
		box = boxaGetBox(boxa1, i, L_COPY);
		boxe = boxCreate(box->x - w / 2, box->y - h / 2, w + 4, h + 4);
		boxaAddBox(boxa2, boxe, L_INSERT);
		pixRenderBox(pixs, boxe, 4, L_FLIP_PIXELS);
		boxDestroy(&box);
	}
	pixWrite("junkoutline", pixs, IFF_TIFF_G4);
	//boxaWriteStream(stderr, boxa2); //TODO ???

	pixDestroy(&pixs);
	pixDestroy(&pixp);
	pixDestroy(&pixpe);
	pixDestroy(&pixt1);
	pixDestroy(&pixt2);
	pixDestroy(&pixhmt);
	pixDestroy(&pixd);
	selDestroy(&sel);
	selDestroy(&sel_2h);
	boxaDestroy(&boxa1);
	boxaDestroy(&boxa2);

	printf("\n---\nEND\n");
	getchar();
	return 0;
}
Esempio n. 2
0
main(int    argc,
     char **argv)
{
BOX         *box;
PIX         *pixs, *pixc, *pixp, *pixsel, *pixhmt;
PIX         *pixd1, *pixd2, *pixd3;
SEL         *selhm;
static char  mainName[] = "findpattern3";

    if (argc != 1)
	exit(ERROR_INT(" Syntax:  findpattern3", mainName, 1));

    if ((pixs = pixRead("feyn.tif")) == NULL)
	exit(ERROR_INT("pixs not made", mainName, 1));

        /* -------------------------------------------- *
         * Extract the pattern for a single character   *
         * ---------------------------------------------*/
    box = boxCreate(599, 1055, 18, 23);
    pixc = pixClipRectangle(pixs, box, NULL);

        /* Make a hit-miss sel */
    selhm = pixGenerateSelBoundary(pixc, 1, 2, 2, 2, 1, 1, 0, 0, &pixp);

        /* Display the sel */
    pixsel = pixDisplayHitMissSel(pixp, selhm, 7, HitColor, MissColor);
    pixDisplay(pixsel, 200, 200);
    pixWrite("/tmp/junkpixsel1", pixsel, IFF_PNG);

        /* Use the Sel to find all instances in the page */
    startTimer();
    pixhmt = pixHMT(NULL, pixs, selhm);
    fprintf(stderr, "Time to find patterns = %7.3f\n", stopTimer());

        /* Color each instance at full res */
    pixd1 = pixDisplayMatchedPattern(pixs, pixp, pixhmt, selhm->cx,
                                     selhm->cy, 0x0000ff00, 1.0, 5);
    pixWrite("/tmp/junkpixd11", pixd1, IFF_PNG);

        /* Color each instance at 0.3 scale */
    pixd2 = pixDisplayMatchedPattern(pixs, pixp, pixhmt, selhm->cx,
                                     selhm->cy, 0x0000ff00, 0.5, 5);
    pixWrite("/tmp/junkpixd12", pixd2, IFF_PNG);

        /* Remove each instance from the input image */
    pixd3 = pixCopy(NULL, pixs);
    pixRemoveMatchedPattern(pixd3, pixp, pixhmt, selhm->cx,
                                    selhm->cy, 1);
    pixWrite("/tmp/junkpixr1", pixd3, IFF_PNG);

    boxDestroy(&box);
    selDestroy(&selhm);
    pixDestroy(&pixc);
    pixDestroy(&pixp);
    pixDestroy(&pixsel);
    pixDestroy(&pixhmt);
    pixDestroy(&pixd1);
    pixDestroy(&pixd2);
    pixDestroy(&pixd3);


        /* -------------------------------------------- *
         *        Extract the pattern for a word        *
         * ---------------------------------------------*/
    box = boxCreate(208, 872, 130, 35);
    pixc = pixClipRectangle(pixs, box, NULL);

        /* Make a hit-miss sel */
    selhm = pixGenerateSelBoundary(pixc, 2, 2, 1, 4, 1, 1, 0, 0, &pixp);

        /* Display the sel */
    pixsel = pixDisplayHitMissSel(pixp, selhm, 7, HitColor, MissColor);
    pixDisplay(pixsel, 200, 200);
    pixWrite("/tmp/junkpixsel2", pixsel, IFF_PNG);

        /* Use the Sel to find all instances in the page */
    startTimer();
    pixhmt = pixHMT(NULL, pixs, selhm);
    fprintf(stderr, "Time to find word patterns = %7.3f\n", stopTimer());

        /* Color each instance at full res */
    pixd1 = pixDisplayMatchedPattern(pixs, pixp, pixhmt, selhm->cx,
                                     selhm->cy, 0x0000ff00, 1.0, 5);
    pixWrite("/tmp/junkpixd21", pixd1, IFF_PNG);

        /* Color each instance at 0.3 scale */
    pixd2 = pixDisplayMatchedPattern(pixs, pixp, pixhmt, selhm->cx,
                                     selhm->cy, 0x0000ff00, 0.5, 5);
    pixWrite("/tmp/junkpixd22", pixd2, IFF_PNG);

        /* Remove each instance from the input image */
    pixd3 = pixCopy(NULL, pixs);
    pixRemoveMatchedPattern(pixd3, pixp, pixhmt, selhm->cx,
                                    selhm->cy, 1);
    pixWrite("/tmp/junkpixr2", pixd3, IFF_PNG);

    selDestroy(&selhm);
    boxDestroy(&box);
    pixDestroy(&pixc);
    pixDestroy(&pixp);
    pixDestroy(&pixsel);
    pixDestroy(&pixhmt);
    pixDestroy(&pixd1);
    pixDestroy(&pixd2);
    pixDestroy(&pixd3);
    pixDestroy(&pixs);
    return 0;
}
l_int32
GeneratePattern(l_int32       patno,
                l_int32       red,
                L_REGPARAMS  *rp)
{
l_int32  width, cx, cy;
PIX     *pixs, *pixt, *pix, *pixr, *pixp, *pixsel, *pixhmt;
PIX     *pixc1, *pixc2, *pixc3, *pixd;
PIXA    *pixa;
SEL     *selhm;

    PROCNAME("GeneratePattern");

    if ((pixs = pixRead(patname[patno])) == NULL) {
        rp->success = FALSE;
	return ERROR_INT("pixs not made", procName, 1);
    }

        /* Make a hit-miss sel at specified reduction factor */
    if (red == 4) {
        pixt = pixReduceRankBinaryCascade(pixs, 4, 4, 0, 0);
        selhm = pixGenerateSelBoundary(pixt, 2, 2, 20, 30, 1, 1, 0, 0, &pixp);
    }
    else if (red == 8) {
        pixt = pixReduceRankBinaryCascade(pixs, 4, 4, 2, 0);
        selhm = pixGenerateSelBoundary(pixt, 1, 2, 6, 12, 1, 1, 0, 0, &pixp);
    }
    else {  /*  red == 16 */
        pixt = pixReduceRankBinaryCascade(pixs, 4, 4, 2, 2);
        selhm = pixGenerateSelBoundary(pixt, 1, 1, 4, 8, 0, 0, 0, 0, &pixp);
    }
    pixDestroy(&pixt);

        /* Display the sel */
    pixsel = pixDisplayHitMissSel(pixp, selhm, 7, HitColor, MissColor);
    pixa = pixaCreate(2);
    pixaAddPix(pixa, pixs, L_CLONE);
    pixaAddPix(pixa, pixsel, L_CLONE);
    width = (patno == 0) ? 1200 : 400;
    pixd = pixaDisplayTiledAndScaled(pixa, 32, width, 2, 0, 30, 2);
    regTestWritePixAndCheck(rp, pixd, IFF_PNG);
    pixDisplayWithTitle(pixd, 100, 100 + 100 * (3 * patno + red / 4),
                        NULL, rp->display);
    pixaDestroy(&pixa);
    pixDestroy(&pixd);

        /* Use the sel to find all instances in the page */
    pix = pixRead("tribune-page-4x.png");  /* 4x reduced */
    if (red == 4)
        pixr = pixClone(pix);
    else if (red == 8)
        pixr = pixReduceRankBinaryCascade(pix, 2, 0, 0, 0);
    else if (red == 16)
        pixr = pixReduceRankBinaryCascade(pix, 2, 2, 0, 0);
    pixDestroy(&pix);

    startTimer();
    pixhmt = pixHMT(NULL, pixr, selhm);
    fprintf(stderr, "Time to find patterns = %7.3f\n", stopTimer());

        /* Color each instance at full res */
    selGetParameters(selhm, NULL, NULL, &cy, &cx);
    pixc1 = pixDisplayMatchedPattern(pixr, pixp, pixhmt,
                                     cx, cy, 0x0000ff00, 1.0, 5);
    regTestWritePixAndCheck(rp, pixc1, IFF_PNG);
    pixDisplayWithTitle(pixc1, 500, 100, NULL, rp->display);
    
        /* Color each instance at 0.5 scale */
    pixc2 = pixDisplayMatchedPattern(pixr, pixp, pixhmt,
                                     cx, cy, 0x0000ff00, 0.5, 5);
    regTestWritePixAndCheck(rp, pixc2, IFF_PNG);

        /* Remove each instance from the input image */
    pixc3 = pixCopy(NULL, pixr);
    pixRemoveMatchedPattern(pixc3, pixp, pixhmt, cx, cy, 1);
    regTestWritePixAndCheck(rp, pixc3, IFF_PNG);

    selDestroy(&selhm);
    pixDestroy(&pixp);
    pixDestroy(&pixsel);
    pixDestroy(&pixhmt);
    pixDestroy(&pixc1);
    pixDestroy(&pixc2);
    pixDestroy(&pixc3);
    pixDestroy(&pixd);
    pixDestroy(&pixr);
    pixDestroy(&pixs);
    return 0;
}