GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r72846
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
findzc.c
Go to the documentation of this file.
1 
2 /**
3  * \file findzc.c
4  *
5  * \brief Zero Crossing functions.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or (at
10  * your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * \author GRASS GIS Development Team
22  * \author Brad Douglas - rez at touchofmadness com
23  *
24  * \date 2006
25  */
26 
27 #include <stdio.h>
28 #include <math.h>
29 
30 
31 /** \def TINY Defined as 1.0e-3 */
32 #define TINY 1.0e-3
33 
34 
35 /**
36  * \fn int G_math_findzc (double conv[], int size, double zc[], double thresh, int num_orients)
37  *
38  * \brief Finds locations and orientations of zero crossings.
39  *
40  * Finds the locations and orientations of zero crossings in the input
41  * array <b>conv</b>, which is the result of the convolution of the
42  * Marr-Hildreth operator with the image. The output array is <b>zc</b>,
43  * which is non-zero only at zero crossing pixels. At those pixels, the
44  * value is 1 + (orientation), where orientation is a value from 0 to
45  * <b>num_orients</b>.
46  *
47  * \param[in] conv input
48  * \param[in] size size of largest matrix column or row
49  * \param[out] zc output
50  * \param[in] thresh magnitude threshold
51  * \param[in] num_orients
52  * \return int always returns 0
53  */
54 
55 int
56 G_math_findzc(double conv[], int size, double zc[], double thresh,
57  int num_orients)
58 {
59  int i, j, p;
60 
61  /* go through entire conv image - but skip border rows and cols */
62  for (i = 1; i < size - 1; i++) {
63  for (p = i * size + 1, j = 1; j < size - 1; j++, p++) {
64  int nbr[4];
65  int ni;
66 
67  /* examine the 4-neighbors of position p */
68  nbr[0] = p - 1; /* left */
69  nbr[1] = p + 1; /* right */
70  nbr[2] = p - size; /* up */
71  nbr[3] = p + size; /* down */
72 
73  zc[p] = 0;
74 
75  for (ni = 0; ni < 4; ni++) {
76  /* condition for a zc: sign is different than a neighbor
77  * and the absolute value is less than that neighbor.
78  * Also, threshold magnitudes to eliminate noise
79  */
80  if ((((conv[p] > 0) && (conv[nbr[ni]] < 0)) ||
81  ((conv[p] < 0) && (conv[nbr[ni]] > 0))) &&
82  (fabs(conv[p]) < fabs(conv[nbr[ni]])) &&
83  (fabs(conv[p] - conv[nbr[ni]]) > thresh)) {
84  double ang;
85  int dir;
86 
87  /* found a zc here, get angle of gradient */
88  if (fabs(conv[nbr[1]] - conv[nbr[0]]) < TINY) {
89  ang = M_PI_2;
90 
91  if (conv[nbr[2]] - conv[nbr[3]] < 0)
92  ang = -ang;
93  }
94  else
95  ang = atan2(conv[nbr[2]] - conv[nbr[3]],
96  conv[nbr[1]] - conv[nbr[0]]);
97 
98  /* scale -PI..PI to 0..num_orients - 1 */
99  dir =
100  num_orients * ((ang + M_PI) / (M_PI * 2.0)) + 0.4999;
101 
102  /* shift scale so that 0 (not 8) is straight down */
103  dir = (3 * num_orients / 4 + dir) % num_orients;
104 
105  /* add to differentiate between no zc and an orientation */
106  zc[p] = 1 + dir;
107  break; /* quit looking at neighbors */
108  }
109  } /* for ni */
110  } /* for p */
111  }
112 
113  return 0;
114 }
int G_math_findzc(double conv[], int size, double zc[], double thresh, int num_orients)
Finds locations and orientations of zero crossings.
Definition: findzc.c:56
#define M_PI
Definition: gis.h:115
#define M_PI_2
Definition: gis.h:118
#define TINY
Definition: findzc.c:32