~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina6/tina-libs/tina/medical/medSroi_sample.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.