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

Linux Cross Reference
Tina4/src/vision/hough/hough1.c

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

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

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