main(int argc, char **argv) { l_int32 i, index; l_float32 cputime, epo; char *filein, *fileout; PIX *pixs, *pixd; SEL *sel; SELA *sela; static char mainName[] = "morphtest1"; if (argc != 3) exit(ERROR_INT(" Syntax: morphtest1 filein fileout", mainName, 1)); filein = argv[1]; fileout = argv[2]; if ((pixs = pixRead(filein)) == NULL) exit(ERROR_INT("pix not made", mainName, 1)); sela = selaAddBasic(NULL); /* ------------------------ Timing -------------------------------*/ #if 1 selaFindSelByName(sela, "sel_9h", &index, &sel); selWriteStream(stderr, sel); pixd = pixCreateTemplate(pixs); startTimer(); for (i = 0; i < NTIMES; i++) { pixDilate(pixd, pixs, sel); /* if ((i % 10) == 0) fprintf(stderr, "%d iters\n", i); */ } cputime = stopTimer(); /* Get the elementary pixel operations/sec */ epo = BASIC_OPS * SEL_SIZE * NTIMES * IMAGE_SIZE /(cputime * CPU_SPEED); fprintf(stderr, "Time: %7.3f sec\n", cputime); fprintf(stderr, "Speed: %7.3f epo/cycle\n", epo); pixWrite(fileout, pixd, IFF_PNG); pixDestroy(&pixd); #endif /* ------------------ Example operation from repository --------------*/ #if 1 /* Select a structuring element */ selaFindSelByName(sela, "sel_50h", &index, &sel); selWriteStream(stderr, sel); /* Do these operations. See below for other ops * that can be substituted here. */ pixd = pixOpen(NULL, pixs, sel); pixXor(pixd, pixd, pixs); pixWrite(fileout, pixd, IFF_PNG); pixDestroy(&pixd); #endif pixDestroy(&pixs); exit(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; }