1 /**********
2 *
3 * This file is part of the TINA Open Source Image Analysis Environment
4 * henceforth known as TINA
5 *
6 * TINA is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation.
9 *
10 * TINA is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with TINA; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 **********
20 *
21 * Program : TINA
22 * File : $Source: /home/tina/cvs/tina-libs/tina/medical/medSroi_sample.c,v $
23 * Date : $Date: 2005/06/13 19:33:40 $
24 * Version : $Revision: 1.7 $
25 * CVS Id : $Id: medSroi_sample.c,v 1.7 2005/06/13 19:33:40 paul Exp $
26 *
27 * Author : Legacy TINA converted by NAT
28 *
29 */
30 /**
31 * @file
32 * @brief Routines for the sampling of image data for use in building an active shape model.
33 * Greylevel values are sampled perpendicularly to the boundary profile at a specified
34 * number of locations on a structure with both inner and outer boundaries at angles specified
35 * by the angle_points routine.
36 * The profile at each location is centred at the sample point. Co-ordinates are generated
37 * relative to the orientation and scale of the baseline using the position and i_position routines.
38 */
39
40 #if HAVE_CONFIG_H
41 # include <config.h>
42 #endif
43
44 #include "med_SroiDef.h"
45 #include "medSroi_sample.h"
46
47 #include <stdio.h>
48 #include <limits.h>
49 #include <math.h>
50 #include <tina/sys/sysDef.h>
51 #include <tina/sys/sysPro.h>
52 #include <tina/math/mathDef.h>
53 #include <tina/math/mathPro.h>
54 #include <tina/image/imgDef.h>
55 #include <tina/image/imgPro.h>
56 #include <tina/geometry/geomDef.h>
57 #include <tina/geometry/geomPro.h>
58 #include <tina/medical/medSroi_alloc.h>
59
60
61 void asm_position(Model *model)
62 {
63 Mat2 m;
64 Vec2 v;
65 double **el, **ev, x, y;
66 int i, j;
67
68 if (model == NULL)
69 return;
70
71 el = model->m->el.double_v;
72 ev = model->M_Evec->el.double_v;
73 m = rot2(-model->theta);
74 for (i = 0; i < model->c; i++)
75 {
76 x = el[2][i];
77 y = el[3][i];
78 for (j = 0; j < model->m_modes; j++)
79 {
80 x += ev[model->c + i][j] * ((double *) (model->m_weight->data))[j];
81 y += ev[i][j] * ((double *) (model->m_weight->data))[j];
82 }
83 v = mat2_vprod(m, vec2(x, y));
84 el[0][i] = (v.el[0] * model->s) + model->tx;
85 el[1][i] = (v.el[1] * model->s) + model->ty;
86 }
87 }
88
89
90 void asm_iposition(Model *model)
91 {
92 Mat2 m;
93 Vec2 v;
94 double **el;
95 int i;
96
97 if (model == NULL)
98 return;
99
100 el = model->m->el.double_v;
101 m = rot2(model->theta);
102 for (i = 0; i < model->c; i++)
103 {
104 v = mat2_vprod(m, vec2(el[0][i] - model->tx,
105 el[1][i] - model->ty));
106 el[2][i] = v.el[0] / model->s;
107 el[3][i] = v.el[1] / model->s;
108 }
109 }
110
111 void asm_angle_points(Model *model)
112 {
113 double dx, dy;
114 double *a, **el;
115 int nm1, i;
116
117 if (model == NULL)
118 return;
119
120 el = model->m->el.double_v;
121 a = (double *) model->alpha->data;
122 nm1 = model->o - 1;
123 i = 0;
124 dx = el[0][nm1] - el[0][i + 1];
125 dy = el[1][i + 1] - el[1][nm1];
126 a[i] = atan2(dx, dy);
127 for (i = 1; i < nm1; i++)
128 {
129 dx = el[0][i - 1] - el[0][i + 1];
130 dy = el[1][i + 1] - el[1][i - 1];
131 a[i] = atan2(dx, dy);
132 }
133 dx = el[0][i - 1] - el[0][0];
134 dy = el[1][0] - el[1][i - 1];
135 a[i] = atan2(dx, dy);
136 if (model->c > model->o)
137 {
138 nm1 = model->c - 1;
139 i = model->o;
140 dx = el[0][nm1] - el[0][i + 1];
141 dy = el[1][i + 1] - el[1][nm1];
142 a[i] = atan2(dx, dy);
143 for (i = model->o + 1; i < nm1; i++)
144 {
145 dx = el[0][i - 1] - el[0][i + 1];
146 dy = el[1][i + 1] - el[1][i - 1];
147 a[i] = atan2(dx, dy);
148 }
149 dx = el[0][i - 1] - el[0][model->o];
150 dy = el[1][model->o] - el[1][i - 1];
151 a[i] = atan2(dx, dy);
152 }
153 }
154
155 void asm_sample_profile(Imrect *im, Model *model)
156 /* store the grey level values from all profiles in the 2D array model->profile */
157 {
158 /* Tv *tv = NULL;
159 */
160 double *a, **el, x0, y0, x, y;
161 float **p;
162 int i, j, middle, length;
163
164 if (model == NULL)
165 return;
166
167 el = model->m->el.double_v;
168 a = (double *) model->alpha->data;
169 if (a==NULL)
170 format("error: no orientation information in template model \n");
171 p = model->profile->el.float_v;
172 middle = model->vsize;
173 length = 2 * model->vsize - 1;
174
175 for (i = 0; i < model->c; i++)
176 {
177 x0 = cos(a[i]) * (double) (middle - 1);
178 y0 = sin(a[i]) * (double) (middle - 1);
179 for (j = 0; j < length; j++)
180 {
181 x = cos(a[i]) * (double) j - x0;
182 y = sin(a[i]) * (double) j - y0;
183 p[i][j] = im_sub_pixf(im, (el[1][i] + y), (el[0][i] + x));
184 }
185 }
186 }
187
188
189 Model *sample_markup(List *poly, Sroi_dparams *params)
190 {
191 Model *mdl = NULL;
192 List *lptr = NULL;
193 Vec2 *p, *p1=NULL, *p2=NULL;
194 double dx, dy;
195 int length = 0, count = 0;
196
197 if ((lptr = poly) == NULL)
198 return(NULL);
199
200 for (lptr = poly; lptr != NULL; lptr = lptr->next)
201 length++;
202
203 if ((length < 1 ) || (params->rows < 1) || (params->outer < 1) ||
204 (params->vsize < 1))
205 return(NULL);
206
207 if (params->outer > length)
208 params->outer = length;
209
210 if ((mdl = model_alloc(params->rows, length, params->outer,
211 params->vsize)) == NULL)
212 return(NULL);
213
214 for (lptr = poly; lptr != NULL; lptr = lptr->next)
215 {
216 if ((p = lptr->to) == NULL)
217 continue;
218
219 if (count == 0)
220 p1 = lptr->to;
221 else if (!(length%2) && (count == (int) ((length / 2))))
222 p2 = lptr->to;
223 else if ((length%2) && (count == (int) (length / 2)))
224 p2 = lptr->to;
225
226 matrix_put(vec2_x(*p), mdl->m, 0, count);
227 matrix_put(vec2_y(*p), mdl->m, 1, count);
228 count++;
229 }
230
231 if (!p1 && !p2)
232 {
233 /* warning allocated model should have been deleted NAT */
234 return(NULL);
235 }
236
237 dx = vec2_x(*p1) - vec2_x(*p2);
238 dy = vec2_y(*p1) - vec2_y(*p2);
239 mdl->tx = (vec2_x(*p1) + vec2_x(*p2)) / 2.0;
240 mdl->ty = (vec2_y(*p1) + vec2_y(*p2)) / 2.0;
241 mdl->theta = atan2(dy, dx);
242 mdl->s = sqrt(dx * dx + dy * dy);
243
244 return(mdl);
245 }
246
247
248 List *export_outer(Model *mdl)
249 {
250 List *poly = NULL;
251 Vec2 v;
252 double **el;
253 int i;
254
255 if (mdl == NULL)
256 return(NULL);
257
258 if ((el = mdl->m->el.double_v) == NULL)
259 return(NULL);
260
261 vec2_x(v) = el[0][0];
262 vec2_y(v) = el[1][0];
263 poly = dd_ref_addtostart(poly, (void *)vec2_copy(&v), VEC2);
264 for(i = 1; i < mdl->o; i++)
265 {
266 vec2_x(v) = el[0][i];
267 vec2_y(v) = el[1][i];
268 poly = dd_ref_addtoend(poly, (void *)vec2_copy(&v), VEC2);
269 }
270
271 /*
272 tv_poly_set(poly);
273 */
274 return(poly);
275 }
276
277
278 List *export_inner(Model *mdl)
279 {
280 List *poly = NULL;
281 Vec2 v;
282 double **el;
283 int i, start;
284
285 if (mdl == NULL)
286 return(NULL);
287
288 start = mdl->o;
289 if (mdl->c == start)
290 return(NULL);
291
292 if ((el = mdl->m->el.double_v) == NULL)
293 return(NULL);
294
295 vec2_x(v) = el[0][start];
296 vec2_y(v) = el[1][start];
297 poly = dd_ref_addtostart(poly, (void *)vec2_copy(&v), VEC2);
298 for(i = ++start; i < mdl->c; i++)
299 {
300 vec2_x(v) = el[0][i];
301 vec2_y(v) = el[1][i];
302 poly = dd_ref_addtoend(poly, (void *)vec2_copy(&v), VEC2);
303 }
304
305 /*
306 tv_poly_set(poly);
307 */
308 return(poly);
309 }
310
311
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.