Leptonica 1.68
C Image Processing Library

livre_hmt.c

Go to the documentation of this file.
00001 /*====================================================================*
00002  -  Copyright (C) 2001 Leptonica.  All rights reserved.
00003  -  This software is distributed in the hope that it will be
00004  -  useful, but with NO WARRANTY OF ANY KIND.
00005  -  No author or distributor accepts responsibility to anyone for the
00006  -  consequences of using this software, or for whether it serves any
00007  -  particular purpose or works at all, unless he or she says so in
00008  -  writing.  Everyone is granted permission to copy, modify and
00009  -  redistribute this source code, for commercial or non-commercial
00010  -  purposes, with the following restrictions: (1) the origin of this
00011  -  source code must not be misrepresented; (2) modified versions must
00012  -  be plainly marked as such; and (3) this notice may not be removed
00013  -  or altered from any source or modified source distribution.
00014  *====================================================================*/
00015 
00016 /*
00017  * livre_hmt.c
00018  *
00019  *    This demonstrates use of pixGenerateSelBoundary() to
00020  *    generate a hit-miss Sel.
00021  *
00022  *    (1) The Sel is displayed with the hit and miss elements in color.
00023  *
00024  *    (2) We produce several 4 bpp colormapped renditions,
00025  *        with the matched pattern either hightlighted or removed.
00026  *
00027  *    (3) For figures in the Document Image Applications chapter:
00028  *           fig 7:  livre_hmt 1 8
00029  *           fig 8:  livre_hmt 2 4
00030  */
00031 
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include "allheaders.h"
00035 
00036     /* for pixDisplayHitMissSel() */
00037 static const l_uint32  HitColor = 0x33aa4400;
00038 static const l_uint32  MissColor = 0xaa44bb00;
00039 
00040     /* Patterns at full resolution */
00041 static const char *patname[3] = {
00042     "",
00043     "tribune-word.png",   /* patno = 1 */
00044     "tribune-t.png"};     /* patno = 2 */
00045 
00046 
00047 main(int    argc,
00048      char **argv)
00049 {
00050 l_int32      patno, reduction, width, cols, cx, cy;
00051 BOX         *box;
00052 PIX         *pixs, *pixt, *pix, *pixr, *pixp, *pixsel, *pixhmt;
00053 PIX         *pixd1, *pixd2, *pixd3, *pixd;
00054 PIXA        *pixa;
00055 SEL         *selhm;
00056 static char  mainName[] = "livre_hmt";
00057 
00058     if (argc != 3)
00059         exit(ERROR_INT(" Syntax:  livre_hmt pattern reduction", mainName, 1));
00060     patno = atoi(argv[1]);
00061     reduction = atoi(argv[2]);
00062 
00063     if ((pixs = pixRead(patname[patno])) == NULL)
00064         exit(ERROR_INT("pixs not made", mainName, 1));
00065     if (reduction != 4 && reduction != 8 && reduction != 16)
00066         exit(ERROR_INT("reduction not 4, 8 or 16", mainName, 1));
00067 
00068     if (reduction == 4)
00069         pixt = pixReduceRankBinaryCascade(pixs, 4, 4, 0, 0);
00070     else if (reduction == 8)
00071         pixt = pixReduceRankBinaryCascade(pixs, 4, 4, 2, 0);
00072     else if (reduction == 16)
00073         pixt = pixReduceRankBinaryCascade(pixs, 4, 4, 2, 2);
00074 
00075         /* Make a hit-miss sel */
00076     if (reduction == 4)
00077         selhm = pixGenerateSelBoundary(pixt, 2, 2, 20, 30, 1, 1, 0, 0, &pixp);
00078     else if (reduction == 8)
00079         selhm = pixGenerateSelBoundary(pixt, 1, 2, 6, 12, 1, 1, 0, 0, &pixp);
00080     else if (reduction == 16)
00081         selhm = pixGenerateSelBoundary(pixt, 1, 1, 4, 8, 0, 0, 0, 0, &pixp);
00082 
00083         /* Display the sel */
00084     pixsel = pixDisplayHitMissSel(pixp, selhm, 7, HitColor, MissColor);
00085     pixDisplay(pixsel, 200, 200);
00086     pixWrite("/tmp/pixsel1", pixsel, IFF_PNG);
00087 
00088         /* Use the Sel to find all instances in the page */
00089     pix = pixRead("tribune-page-4x.png");  /* 4x reduced */
00090     if (reduction == 4)
00091         pixr = pixClone(pix);
00092     else if (reduction == 8)
00093         pixr = pixReduceRankBinaryCascade(pix, 2, 0, 0, 0);
00094     else if (reduction == 16)
00095         pixr = pixReduceRankBinaryCascade(pix, 2, 2, 0, 0);
00096 
00097     startTimer();
00098     pixhmt = pixHMT(NULL, pixr, selhm);
00099     fprintf(stderr, "Time to find patterns = %7.3f\n", stopTimer());
00100 
00101         /* Color each instance at full res */
00102     selGetParameters(selhm, NULL, NULL, &cy, &cx);
00103     pixd1 = pixDisplayMatchedPattern(pixr, pixp, pixhmt,
00104                                      cx, cy, 0x0000ff00, 1.0, 5);
00105     pixWrite("/tmp/pixd11", pixd1, IFF_PNG);
00106 
00107         /* Color each instance at 0.5 scale */
00108     pixd2 = pixDisplayMatchedPattern(pixr, pixp, pixhmt,
00109                                      cx, cy, 0x0000ff00, 0.5, 5);
00110     pixWrite("/tmp/pixd12", pixd2, IFF_PNG);
00111 
00112         /* Remove each instance from the input image */
00113     pixd3 = pixCopy(NULL, pixr);
00114     pixRemoveMatchedPattern(pixd3, pixp, pixhmt, cx, cy, 1);
00115     pixWrite("/tmp/pixr1", pixd3, IFF_PNG);
00116 
00117     pixa = pixaCreate(2);
00118     pixaAddPix(pixa, pixs, L_CLONE);
00119     pixaAddPix(pixa, pixsel, L_CLONE);
00120     cols = (patno == 1) ? 1 : 2;
00121     width = (patno == 1) ? 800 : 400;
00122     pixd = pixaDisplayTiledAndScaled(pixa, 32, width, cols, 0, 30, 2);
00123     pixWrite("/tmp/hmt.png", pixd, IFF_PNG);
00124     pixDisplay(pixd, 0, 300);
00125 
00126     selDestroy(&selhm);
00127     pixDestroy(&pixp);
00128     pixDestroy(&pixsel);
00129     pixDestroy(&pixhmt);
00130     pixDestroy(&pixd1);
00131     pixDestroy(&pixd2);
00132     pixDestroy(&pixd3);
00133     pixDestroy(&pixs);
00134     pixDestroy(&pix);
00135     pixDestroy(&pixt);
00136     return 0;
00137 }
00138 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines