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

Linux Cross Reference
Tina5/tina-libs/tina/vision/visShape_hough1.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 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/vision/visShape_hough1.c,v $
 23  * Date    :  $Date: 2008/12/02 22:03:07 $
 24  * Version :  $Revision: 1.5 $
 25  * CVS Id  :  $Id: visShape_hough1.c,v 1.5 2008/12/02 22:03:07 paul Exp $
 26  *
 27  * Author  : Legacy TINA
 28  *
 29  * Notes :
 30  *  if the view tool is included and used then
 31  *  the view tool sets a pointer to a refresh
 32  *  function that is used to tell the user which
 33  *  tv tool is the current
 34  *  similarly for the sroi tool
 35  *
 36  *********
 37 */
 38 
 39 #include "visShape_hough1.h"
 40 
 41 #if HAVE_CONFIG_H
 42   #include <config.h>
 43 #endif
 44 
 45 #include <stdio.h>
 46 #include <stdlib.h>
 47 #include <math.h>
 48 #include <tina/sys/sysDef.h>
 49 #include <tina/sys/sysPro.h>
 50 #include <tina/math/mathDef.h>
 51 #include <tina/image/imgDef.h>
 52 #include <tina/image/imgPro.h>
 53 #include <tina/geometry/geomDef.h>
 54 #include <tina/geometry/geomPro.h>
 55 #include <tina/vision/vis_ShapeDef.h>
 56 
 57 /* Function prototypes from other files */
 58 
 59 
 60 /* ---------- Functions ---------- */
 61 
 62 float  chi_2(double p, float pos, float delta_theta)
 63 {
 64        float chi_sq;
 65 
 66        chi_sq = (float)pow(((p - pos)/(delta_theta)),2.0);
 67 
 68        return(chi_sq);
 69 }
 70 
 71 Imrect *hough1_alloc(double minn, double maxx, double binsize, Vartype vtype)
 72 {
 73     Imrect       *hough1;
 74     Imregion      roi;
 75     Hough_region *hough1_roi;
 76 
 77 /* Define the physical size of the hough space aray. Need to use the 'ceil'
 78    function because the hough space may not be exactly divisable by the bin
 79    size. */
 80 
 81     roi.lx = 0;
 82     roi.ux = (int)ceil((maxx-minn)/binsize);
 83     roi.ly = 0;
 84     roi.uy = 1; /* The 1D hough data is stored in a 1 bin wide array */
 85 
 86     hough1 = im_alloc(roi.uy, roi.ux, &roi, vtype);
 87 
 88 /* Add the logical hough coords to the props list */
 89 
 90     hough1_roi = ralloc(sizeof(Hough_region));
 91 
 92     if (hough1_roi!=NULL)
 93     {
 94         hough1_roi->lx = minn;
 95         hough1_roi->ly = 0.0;
 96         hough1_roi->ux = maxx;
 97         hough1_roi->uy = binsize; /* TEMP use of this variable */
 98 
 99         hough1->props = proplist_add(hough1->props, hough1_roi, HOUGH1_ROI_TYPE,
100                                                                          rfree);
101     }
102 
103     return (hough1);
104 
105 }
106 
107 /* ---------- */
108 
109 Hough1_peak hough1_locate_max_peak(Imrect *hough1, double thres, Vartype vtype)
110 {
111     Hough1_peak peak;
112 
113     switch(vtype)
114     {
115     case char_v:   peak = hough1_locate_max_peak_char(hough1, thres);
116                    break;
117     case float_v:  peak = hough1_locate_max_peak_float(hough1, thres);
118                    break;
119     default:       format("hough1_locate_max_peak: image vtype not supported\n");
120     }
121 
122     return(peak);
123 }
124 
125 /* ---------- */
126 
127 List *hough1_locate_peaks(Imrect *hough1, double thres, Vartype vtype)
128 {
129     List *peaks=NULL;
130 
131     switch(vtype)
132     {
133     case char_v:   peaks = hough1_locate_peaks_char(hough1, thres);
134                    break;
135     case float_v:  peaks = hough1_locate_peaks_float(hough1, thres);
136                    break;
137     default:       format("hough1_locate_peaks: image vtype not supported\n");
138     }
139     return(peaks);
140 }
141 
142 /* ---------- */
143 
144 void hough1_plot_point_char(double p, Imrect *hough1)
145 {
146     int            x;
147     char          *im_data;
148     double         minn, maxx, binsize;
149     Hough_region  *hough1_roi;    
150 
151 
152         /* (1) Transform from logical coords to array indices.
153            (2) Plot the point. */
154 
155 /* Extract the logical coords from the props list */
156 
157     hough1_roi = prop_get(hough1->props, HOUGH1_ROI_TYPE);
158 
159     if (hough1_roi==NULL)
160     {
161         format("Error: hough1_roi not set.\n");
162         return ;
163     }
164 
165     minn = hough1_roi->lx;
166     maxx = hough1_roi->ux;
167     binsize = hough1_roi->uy; /* TEMP */
168 
169 /* The array index can now be determined */
170 
171     x = (int)((p-minn)/binsize);  
172 
173     if ( (x<0) || (x>hough1->region->ux) )
174     {
175         return;
176     }
177 
178 /* Access the image data */ /* Assume char_v images */
179 
180     im_data = IM_ROW(hough1,0);
181 
182 /* Now plot the point */
183 
184     im_data[x] = im_data[x]+1;
185 
186 }
187 
188 /* ----------- */
189 
190 Hough1_peak hough1_locate_max_peak_char(Imrect *hough1, double thres)
191 {
192     List        *peaks_list=NULL, *peak_ptr=NULL;
193     Hough1_peak  *peak=NULL, max_peak;
194 
195     max_peak.height = 0.0;
196 
197     /* Added to prevent compiler warnings PAB 02/12/2008 */
198 
199     max_peak.x = 0.0;
200     max_peak.label = 0;
201     max_peak.type = 0;
202     max_peak.props = NULL;
203 
204     peaks_list = hough1_locate_peaks(hough1, thres, hough1->vtype);
205 
206     for(peak_ptr=peaks_list;peak_ptr!=NULL;peak_ptr=peak_ptr->next)
207     {
208         peak=peak_ptr->to;
209         if (peak->height>max_peak.height)
210             max_peak=*peak;
211     }
212  
213     list_rm(peaks_list, rfree);
214     
215     return (max_peak);
216 }
217 
218 /* ---------- */
219 
220 List *hough1_locate_peaks_char(Imrect *hough1, double thres)
221 {
222     int           x0, x1;
223     int           i;
224     char         *im_data;
225     char          pixel_intensity;
226     List         *peaks_list=NULL;
227     double        minn, maxx;
228     double        binsize;
229     Hough1_peak  *peak;
230     Hough_region *hough1_roi;
231     int           h, j;
232 
233     if (hough1==NULL)
234     {
235         format("Error: hough1 not defined.\n");
236         return NULL;
237     }
238 
239 /* Extract the physical coords of the hough space */
240 
241     x0 = hough1->region->lx;
242     x1 = hough1->region->ux;
243 
244 /* Extract the logical coords of the hough space */
245 
246     if ((hough1_roi = prop_get(hough1->props, HOUGH1_ROI_TYPE)) == NULL)
247     {
248         format("Error: hough1_roi not defined.\n");
249         return NULL;
250     }
251 
252     minn = hough1_roi->lx;
253     maxx = hough1_roi->ux;
254     binsize = hough1_roi->uy; /* TEMP */
255 
256 /* if the hough1 is less than 3 bins wide then cannot find peaks */
257 
258     if (x1-x0 < 3)
259     {
260         format("Error: hough1 too small to find peaks.\n");
261         return NULL;
262     }
263 
264 /* Access the image data */ /* Assume char_v images */
265 
266     im_data = IM_ROW(hough1,0);
267 
268 /* Pass a 3 bin wide window over the hough1 */
269     for(i=x0;i<x1;i++)
270     {
271         pixel_intensity = im_data[i];
272 
273         if ((double)pixel_intensity>=thres)
274         {
275             if (i-1 < x0) h = x1-1; else h = i-1; /* Wrap around */
276             if (i+1 >= x1) j = x0; else j = i+1;
277 
278             if ((pixel_intensity>im_data[h]) &&
279                 (pixel_intensity>im_data[j]))
280             {
281                 peak = ralloc(sizeof(Hough1_peak));
282                 peak->x = ((double)i+0.5)*binsize+minn;
283                 peak->height = (double)pixel_intensity;
284                 peaks_list = ref_addtostart(peaks_list, peak, HOUGH1_PEAK); 
285             }
286         }
287     }/*endfor(i)*/
288 
289     return (peaks_list);
290 
291 }
292 
293 /* ---------- */
294 
295 void hough1_plot_log_gauss(Imrect *hough1, double mean, double sigma)
296 {
297     int            x, point;
298     float         *im_data;
299     double         minn, maxx, binsize;
300     Hough_region  *hough1_roi; 
301     float          pos, cons, temp;
302 
303         /* (1) Transform from logical coords to array indices.
304            (2) Blur the point with chi_squared
305            (3) Plot the point. */
306 
307 /* Extract the logical coords from the props list */
308 
309     hough1_roi = prop_get(hough1->props, HOUGH1_ROI_TYPE);
310 
311     if (hough1_roi==NULL)
312     {
313         format("Error: hough1_roi not set.\n");
314         return ;
315     }
316 
317     minn = hough1_roi->lx;
318     maxx = hough1_roi->ux;
319     binsize = hough1_roi->uy; /* TEMP */
320 
321 /* The array index can now be determined */
322 
323     x = (int)((mean-minn)/binsize); 
324 
325     if ( (x<0) || (x>hough1->region->ux) )
326     {
327         return;
328     }
329 
330 /* Access the image data */ /* Assume float_v images */
331 
332     im_data = IM_ROW(hough1,0);
333 
334 /* Now blur the point */
335 
336 /* The standard deviation of the error ellipse on the line segmentation is one hence ignore*/
337 
338 cons = (float)9.0;
339 
340 for(point=hough1->region->lx;point<hough1->region->ux;point++)
341 {
342      pos = (float)(((point+0.5)*binsize) + minn); /* =angle at centre of bin numbered point */
343      temp = (float)(cons - chi_2(mean, pos, (float)sigma));
344      if(temp > 0)
345           im_data[point] = im_data[point] + temp;
346 }
347 
348 }
349 
350 /* ---------- */
351 
352 List *hough1_locate_peaks_float(Imrect *hough1, double thres)
353 {
354     int           x0, x1;
355     int           i;
356     float        *im_data;
357     float         pixel_intensity;
358     List         *peaks_list=NULL;
359     double        minn, maxx;
360     double        binsize;
361     Hough1_peak  *peak;
362     Hough_region *hough1_roi;
363 
364     if (hough1==NULL)
365     {
366         format("Error: hough1 not defined.\n");
367         return NULL;
368     }
369 
370 /* Extract the physical coords of the hough space */
371 
372     x0 = hough1->region->lx;
373     x1 = hough1->region->ux;
374 
375 /* Extract the logical coords of the hough space */
376 
377     if ((hough1_roi = prop_get(hough1->props, HOUGH1_ROI_TYPE)) == NULL)
378     {
379         format("Error: hough1_roi not defined.\n");
380         return NULL;
381     }
382 
383     minn = hough1_roi->lx;
384     maxx = hough1_roi->ux;
385     binsize = hough1_roi->uy; /* TEMP */
386 
387 /* if the hough1 is less than 3 bins wide then cannot find peaks */
388 
389     if (x1-x0 < 3)
390     {
391         format("Error: hough1 too small to find peaks.\n");
392         return NULL;
393     }
394 
395 /* Access the image data */ /* Assume float_v images */
396 
397     im_data = IM_ROW(hough1,0);
398 
399 /* Pass a 3 bin wide window over the hough1 */
400 
401     for(i=(x0+1);i<(x1-1);i++)
402     {
403         pixel_intensity = im_data[i];
404 
405         if ((double)pixel_intensity>=thres)
406             if ((pixel_intensity>im_data[i-1]) &&
407                 (pixel_intensity>im_data[i+1]))
408             {
409                 peak = ralloc(sizeof(Hough1_peak));
410                 peak->x = (((hough1_quad_fit_float(&i, &pixel_intensity, im_data)+i+0.5)*binsize)
411                                                                                             +minn);
412                 peak->height = (double)pixel_intensity;
413                 peaks_list = ref_addtostart(peaks_list, peak, HOUGH1_PEAK); 
414             }
415     }/*endfor(i)*/
416 
417     return (peaks_list);
418 
419 }
420 
421 /* ---------- */
422 
423 double hough1_pw; /* Last peak width made accessible */
424 
425 double hough1_quad_fit_float(int *location, float *Y2, float* im_data)
426 {
427      float  Y1, Y3, A, B;
428      double x_coord;
429 
430 /* Get the values in the hough space at the three points */
431 
432      Y1 = im_data[*location+1];
433      Y3 = im_data[*location-1];
434 
435 /* Compute the values of the coefficients of the quadratic, ignore C 'cos not used */
436      
437      B = (float)(-(Y3 - Y1)/2.0);
438      A = (float)(-((Y3/2.0) - *Y2 + (Y1/2.0)));
439 
440 /* Find x_coord at which derivative = 0 */
441 
442      x_coord = B/(2.0*A);
443 
444     /* TEMP */
445     hough1_pw = sqrt(1/A);
446 
447 return(x_coord); 
448 }
449 
450 /* ---------- */
451 
452 Hough1_peak hough1_locate_max_peak_float(Imrect *hough1, double thres)
453 {
454     List         *peaks_list=NULL, *peak_ptr=NULL;
455     Hough1_peak  *peak=NULL, max_peak;
456 
457     max_peak.height = 0.0;
458 
459     /* Added to prevent compiler warnings PAB 02/12/2008 */
460 
461     max_peak.x = 0.0;
462     max_peak.label = 0;
463     max_peak.type = 0;
464     max_peak.props = NULL;
465 
466     peaks_list = hough1_locate_peaks_float(hough1, thres);
467 
468     for(peak_ptr=peaks_list;peak_ptr!=NULL;peak_ptr=peak_ptr->next)
469     {
470         peak=peak_ptr->to;
471         if (peak->height>max_peak.height)
472             max_peak=*peak;
473     }
474  
475     list_rm(peaks_list, rfree);
476     
477     return (max_peak);
478 }
479 
480 /* ---------- */
481 
482 
483 

~ [ 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.