Leptonica 1.68
C Image Processing Library

findpattern1.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  * findpattern1.c
00018  *
00019  *    findpattern1 filein patternfile fileout
00020  *
00021  *    This is setup with input parameters to generate a hit-miss
00022  *    Sel from the instance char.tif of a "c" bitmap, from
00023  *    the page image feyn.tif, scanned at 300 ppi:
00024  *
00025  *       findpattern1 feyn.tif char.tif junkcharout
00026  *
00027  *    It shows a number of different outputs, including a magnified
00028  *    image of the Sel superimposed on the "c" bitmap.
00029  */
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include "allheaders.h"
00034 
00035     /* for pixGenerateSelWithRuns() */
00036 static const l_int32  NumHorLines = 11;
00037 static const l_int32  NumVertLines = 8;
00038 static const l_int32  MinRunlength = 1;
00039 
00040     /* for pixDisplayHitMissSel() */
00041 static const l_uint32  HitColor = 0xff880000;
00042 static const l_uint32  MissColor = 0x00ff8800;
00043 
00044 
00045 main(int    argc,
00046      char **argv)
00047 {
00048 char        *filein, *fileout, *patternfile;
00049 l_int32      w, h, i, n;
00050 BOX         *box, *boxe;
00051 BOXA        *boxa1, *boxa2;
00052 PIX         *pixs, *pixp, *pixpe;
00053 PIX         *pixd, *pixt1, *pixt2, *pixhmt;
00054 SEL         *sel_2h, *sel;
00055 static char  mainName[] = "findpattern1";
00056 
00057     if (argc != 4)
00058         exit(ERROR_INT(" Syntax:  findpattern1 filein patternfile fileout",
00059             mainName, 1));
00060 
00061     filein = argv[1];
00062     patternfile = argv[2];
00063     fileout = argv[3];
00064 
00065     if ((pixs = pixRead(filein)) == NULL)
00066         exit(ERROR_INT("pixs not made", mainName, 1));
00067     if ((pixp = pixRead(patternfile)) == NULL)
00068         exit(ERROR_INT("pixp not made", mainName, 1));
00069     w = pixGetWidth(pixp);
00070     h = pixGetHeight(pixp);
00071     
00072         /* generate the hit-miss Sel with runs */
00073     sel = pixGenerateSelWithRuns(pixp, NumHorLines, NumVertLines, 0, 
00074                                 MinRunlength, 7, 7, 0, 0, &pixpe);
00075 
00076         /* display the Sel two ways */
00077     selWriteStream(stderr, sel);
00078     pixt1 = pixDisplayHitMissSel(pixpe, sel, 9, HitColor, MissColor);
00079     pixDisplay(pixt1, 200, 200);
00080     pixWrite("/tmp/junkpixt", pixt1, IFF_PNG);
00081 
00082         /* use the Sel to find all instances in the page */
00083     startTimer();
00084     pixhmt = pixHMT(NULL, pixs, sel);
00085     fprintf(stderr, "Time to find patterns = %7.3f\n", stopTimer());
00086 
00087         /* small erosion to remove noise; typically not necessary if
00088          * there are enough elements in the Sel */
00089     sel_2h = selCreateBrick(1, 2, 0, 0, SEL_HIT);
00090     pixt2 = pixErode(NULL, pixhmt, sel_2h);
00091 
00092         /* display the result visually by placing the Sel at each
00093          * location found */
00094     pixd = pixDilate(NULL, pixt2, sel);
00095     pixWrite(fileout, pixd, IFF_TIFF_G4);
00096     
00097         /* display outut with an outline around each located pattern */
00098     boxa1 = pixConnCompBB(pixt2, 8);
00099     n = boxaGetCount(boxa1);
00100     boxa2 = boxaCreate(n);
00101     for (i = 0; i < n; i++) {
00102         box = boxaGetBox(boxa1, i, L_COPY);
00103         boxe = boxCreate(box->x - w / 2, box->y - h / 2, w + 4, h + 4);
00104         boxaAddBox(boxa2, boxe, L_INSERT);
00105         pixRenderBox(pixs, boxe, 4, L_FLIP_PIXELS);
00106         boxDestroy(&box);
00107     }
00108     pixWrite("/tmp/junkoutline", pixs, IFF_TIFF_G4);
00109     boxaWriteStream(stderr, boxa2);
00110 
00111     pixDestroy(&pixs);
00112     pixDestroy(&pixp);
00113     pixDestroy(&pixpe);
00114     pixDestroy(&pixt1);
00115     pixDestroy(&pixt2);
00116     pixDestroy(&pixhmt);
00117     pixDestroy(&pixd);
00118     selDestroy(&sel);
00119     selDestroy(&sel_2h);
00120     boxaDestroy(&boxa1);
00121     boxaDestroy(&boxa2);
00122     return 0;
00123 }
00124 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines