File: morphtest1.c

package info (click to toggle)
leptonlib 1.57-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 9,724 kB
  • ctags: 3,900
  • sloc: ansic: 89,715; sh: 3,516; makefile: 718
file content (129 lines) | stat: -rw-r--r-- 4,119 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*====================================================================*
 -  Copyright (C) 2001 Leptonica.  All rights reserved.
 -  This software is distributed in the hope that it will be
 -  useful, but with NO WARRANTY OF ANY KIND.
 -  No author or distributor accepts responsibility to anyone for the
 -  consequences of using this software, or for whether it serves any
 -  particular purpose or works at all, unless he or she says so in
 -  writing.  Everyone is granted permission to copy, modify and
 -  redistribute this source code, for commercial or non-commercial
 -  purposes, with the following restrictions: (1) the origin of this
 -  source code must not be misrepresented; (2) modified versions must
 -  be plainly marked as such; and (3) this notice may not be removed
 -  or altered from any source or modified source distribution.
 *====================================================================*/

/*
 * morphtest1.c
 *
 *   - Timing test for rasterop-based morphological operations
 *   - Example repository of binary morph operations
 */

#include <stdio.h>
#include <stdlib.h>
#include "allheaders.h"

#define   NTIMES         100
#define   IMAGE_SIZE     8.     /* megapixels */
#define   SEL_SIZE       9
#define   BASIC_OPS      1.     /* 1 for erosion/dilation; 2 for open/close */
#define   CPU_SPEED      866.   /* MHz: set it for the machine you're using */



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);
}


/* ==================================================================== */

/* -------------------------------------------------------------------- *
 *                 Repository for selecting various operations          *
 *                              that might be used                      *
 * -------------------------------------------------------------------- */
#if 0
    pixd = pixCreateTemplate(pixs);

    pixd = pixDilate(NULL, pixs, sel);
    pixd = pixErode(NULL, pixs, sel);
    pixd = pixOpen(NULL, pixs, sel);
    pixd = pixClose(NULL, pixs, sel);

    pixDilate(pixd, pixs, sel);
    pixErode(pixd, pixs, sel);
    pixOpen(pixd, pixs, sel);
    pixClose(pixd, pixs, sel);

    pixAnd(pixd, pixd, pixs);
    pixOr(pixd, pixd, pixs);
    pixXor(pixd, pixd, pixs);
    pixSubtract(pixd, pixd, pixs);
    pixInvert(pixd, pixs);

    pixd = pixAnd(NULL, pixd, pixs);
    pixd = pixOr(NULL, pixd, pixs);
    pixd = pixXor(NULL, pixd, pixs);
    pixd = pixSubtract(NULL, pixd, pixs);
    pixd = pixInvert(NULL, pixs);

    pixInvert(pixs, pixs);
#endif  /* 0 */