1 /**********
2 *
3 * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
4 * University of Manchester, UK. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
8 *
9 * . Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * . Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * . Neither the name of the University of Manchester nor the names of its
17 * contributors may be used to endorse or promote products derived from this
18 * software without specific prior written permission.
19 *
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 **********
34 *
35 * Program : TINA
36 * File : $Source: /home/tina/cvs/tina-libs/tina/image/imgPrc_morph.c,v $
37 * Date : $Date: 2003/09/22 16:09:02 $
38 * Version : $Revision: 1.4 $
39 * CVS Id : $Id: imgPrc_morph.c,v 1.4 2003/09/22 16:09:02 tony Exp $
40 *
41 * Author : Legacy TINA
42 */
43
44 /**
45 * @file
46 * @brief Morphological dilation and erosion by arbitrary structuring element.
47 *
48 */
49
50 #include "imgPrc_morph.h"
51
52 #if HAVE_CONFIG_H
53 #include <config.h>
54 #endif
55
56 #include <math.h>
57 #include <tina/sys/sysDef.h>
58 #include <tina/sys/sysPro.h>
59 #include <tina/math/mathDef.h>
60 #include <tina/math/mathPro.h>
61 #include <tina/image/img_GenDef.h>
62 #include <tina/image/img_GenPro.h>
63
64 void morph_spere(double r, Imrect ** el_val)
65 /* radius about origin */
66 {
67 int lx, ly, ux, uy;
68 int i, j;
69 double rsq;
70 Imregion *roi;
71
72 lx = ly = (int) -r;
73 ux = uy = (int) (r + 1);
74 roi = roi_alloc(lx, ly, ux, uy);
75
76 *el_val = im_alloc(uy, ux, roi, float_v);
77 rfree(roi);
78
79 rsq = r * r;
80
81 for (i = ly; i < uy; ++i)
82 for (j = lx; j < ux; ++j)
83 {
84 int d = i * i + j * j;
85
86 if (d > rsq)
87 {
88 IM_FLOAT(*el_val, i, j) = (float) 0.0;
89 continue;
90 }
91 IM_FLOAT(*el_val, i, j) = (float) sqrt(rsq - d);
92 }
93 }
94
95 Imrect *imf_dilate(Imrect * im1, Imrect * el_val)
96 /* image to be dilated */
97 /* value of dilation mask zero value external to the mask */
98 {
99 Imrect *im2;
100 Vartype vtype;
101 Imregion *roi;
102 double val;
103 int lx, ux, ly, uy;
104 int m_lx, m_ux, m_ly, m_uy;
105 int i, j;
106
107 if (im1 == NULL || el_val == NULL)
108 return (NULL);
109
110 roi = el_val->region;
111 if (roi == NULL)
112 return (NULL);
113 m_lx = roi->lx;
114 m_ux = roi->ux;
115 m_ly = roi->ly;
116 m_uy = roi->uy;
117
118 roi = im1->region;
119 if (roi == NULL)
120 return (NULL);
121 lx = roi->lx;
122 ux = roi->ux;
123 ly = roi->ly;
124 uy = roi->uy;
125
126 vtype = (el_val == NULL) ? im1->vtype : (Vartype) MAX((int) im1->vtype, (int) el_val->vtype);
127 im2 = im_alloc(im1->height, im1->width, roi, vtype);
128
129 for (i = ly; i < uy; ++i)
130 for (j = lx; j < ux; ++j)
131 {
132 double gl, max_gl = 0;
133 int ii, jj;
134 int not_set = 1;
135
136 for (ii = m_ly; ii < m_uy; ++ii)
137 for (jj = m_lx; jj < m_ux; ++jj)
138 {
139 int x = j + jj, y = i + ii;
140
141 if (x >= lx && x < ux && y >= ly && y < uy) /* in image */
142 {
143 IM_PIX_GET(el_val, ii, jj, val);
144 if (val == 0.0)
145 continue;
146 IM_PIX_GET(im1, y, x, gl);
147
148 gl += val;
149 if (not_set || gl > max_gl)
150 {
151 max_gl = gl;
152 not_set = 0;
153 }
154 }
155 }
156
157 IM_PIX_SET(im2, i, j, max_gl);
158 }
159
160 return (im2);
161 }
162
163 Imrect *imf_erode(Imrect * im1, Imrect * el_val)
164 /* image to be eroded */
165 /* value of dilation mask non-zero for active region */
166 {
167 Imrect *im2;
168 Vartype vtype;
169 Imregion *roi;
170 double val;
171 int lx, ux, ly, uy;
172 int m_lx, m_ux, m_ly, m_uy;
173 int i, j;
174
175 if (im1 == NULL || el_val == NULL)
176 return (NULL);
177
178 roi = el_val->region;
179 if (roi == NULL)
180 return (NULL);
181 m_lx = roi->lx;
182 m_ux = roi->ux;
183 m_ly = roi->ly;
184 m_uy = roi->uy;
185
186 roi = im1->region;
187 if (roi == NULL)
188 return (NULL);
189 lx = roi->lx;
190 ux = roi->ux;
191 ly = roi->ly;
192 uy = roi->uy;
193
194 vtype = (el_val == NULL) ? im1->vtype : (Vartype) MAX((int) im1->vtype, (int) el_val->vtype);
195 im2 = im_alloc(im1->height, im1->width, roi, vtype);
196
197 for (i = ly; i < uy; ++i)
198 for (j = lx; j < ux; ++j)
199 {
200 double gl, min_gl = 0;
201 int ii, jj;
202 int not_set = 1;
203
204 for (ii = m_ly; ii < m_uy; ++ii)
205 for (jj = m_lx; jj < m_ux; ++jj)
206 {
207 int x = j + jj, y = i + ii;
208
209 if (x >= lx && x < ux && y >= ly && y < uy) /* in image */
210 {
211 IM_PIX_GET(el_val, ii, jj, val);
212 if (val == 0.0)
213 continue;
214 IM_PIX_GET(im1, y, x, gl);
215 gl -= val;
216 if (not_set || gl < min_gl)
217 {
218 min_gl = gl;
219 not_set = 0;
220 }
221 }
222 }
223
224 IM_PIX_SET(im2, i, j, min_gl);
225 }
226
227 return (im2);
228 }
229
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.