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

Linux Cross Reference
Tina4/src/vision/improc/im_scale.c

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

  1 /**@(#)
  2 **/
  3 #include <math.h>
  4 #include <values.h>
  5 #include <tina/sys.h>
  6 #include <tina/sysfuncs.h>
  7 
  8 /** finding min and max and scaling images **/
  9 
 10 void imf_minmax(Imrect * im, float *pmin, float *pmax)
 11 {
 12     Imregion *roi;
 13     float *row, min, max;
 14     int lx, ux, ly, uy;
 15     int i, j;
 16 
 17     if (im == NULL)
 18     {
 19         *pmin = (float)0.0;
 20         *pmax = (float)0.0;
 21         return;
 22     }
 23     roi = im->region;
 24     if (roi == NULL)
 25     {
 26         *pmin = (float)0.0;
 27         *pmax = (float)0.0;
 28         return;
 29     }
 30     lx = roi->lx;
 31     ux = roi->ux;
 32     ly = roi->ly;
 33     uy = roi->uy;
 34 
 35     row = fvector_alloc(lx, ux);
 36     min = max = (float)im_get_pixf(im, ly, lx);
 37     for (i = ly; i < uy; ++i)
 38     {
 39         im_get_rowf(row, im, i, lx, ux);
 40         for (j = lx; j < ux; ++j)
 41         {
 42             if (min > row[j])
 43                 min = row[j];
 44             if (max < row[j])
 45                 max = row[j];
 46         }
 47     }
 48 
 49     *pmin = min;
 50     *pmax = max;
 51     fvector_free((void *) row, lx);
 52 }
 53 
 54 void imf_minmax_nzero(Imrect * im, float *pmin, float *pmax)
 55 {
 56     Imregion *roi;
 57     float *row, min, max;
 58     int lx, ux, ly, uy;
 59     int i, j;
 60  
 61     if (im == NULL)
 62     {
 63         *pmin = (float)0.0;
 64         *pmax = (float)0.0;
 65         return;
 66     }
 67     roi = im->region;
 68     if (roi == NULL)
 69     {
 70         *pmin = (float)0.0;
 71         *pmax = (float)0.0;
 72         return;
 73     }
 74     lx = roi->lx;
 75     ux = roi->ux;
 76     ly = roi->ly;
 77     uy = roi->uy;
 78  
 79     row = fvector_alloc(lx, ux);
 80     min = MAXFLOAT;
 81     max =-MAXFLOAT;
 82     for (i = ly; i < uy; ++i)
 83     {
 84         im_get_rowf(row, im, i, lx, ux);
 85         for (j = lx; j < ux; ++j)
 86         {
 87             if (min > row[j] && row[j] !=0.0)
 88                 min = row[j];
 89             if (max < row[j] && row[j] !=0.0)
 90                 max = row[j];
 91         }
 92     }
 93  
 94     *pmin = min;
 95     *pmax = max;
 96     fvector_free((void *) row, lx);
 97 }
 98 
 99 
100 double imf_min(Imrect * im, int *y, int *x)
101 {
102     Imregion *roi;
103     float *row, min;
104     int lx, ux, ly, uy;
105     int i, j;
106 
107     if (im == NULL)
108         return (0.0);
109 
110     roi = im->region;
111     if (roi == NULL)
112         return (0.0);
113 
114     lx = roi->lx;
115     ux = roi->ux;
116     ly = roi->ly;
117     uy = roi->uy;
118 
119     row = fvector_alloc(lx, ux);
120     min = (float)im_get_pixf(im, ly, lx);
121     if (y != NULL)
122         *y = 0;
123     if (x != NULL)
124         *x = 0;
125     for (i = ly; i < uy; ++i)
126     {
127         im_get_rowf(row, im, i, lx, ux);
128         for (j = lx; j < ux; ++j)
129             if (min > row[j])
130             {
131                 min = row[j];
132                 if (y != NULL)
133                     *y = i;
134                 if (x != NULL)
135                     *x = j;
136             }
137     }
138 
139     fvector_free((void *) row, lx);
140     return (min);
141 }
142 
143 double imf_max(Imrect * im, int *y, int *x)
144 {
145     Imregion *roi;
146     float *row, max;
147     int lx, ux, ly, uy;
148     int i, j;
149 
150     if (im == NULL)
151         return (0.0);
152 
153     roi = im->region;
154     if (roi == NULL)
155         return (0.0);
156 
157     lx = roi->lx;
158     ux = roi->ux;
159     ly = roi->ly;
160     uy = roi->uy;
161 
162     row = fvector_alloc(lx, ux);
163     max = (float)im_get_pixf(im, ly, lx);
164     if (y != NULL)
165         *y = 0;
166     if (x != NULL)
167         *x = 0;
168     for (i = ly; i < uy; ++i)
169     {
170         im_get_rowf(row, im, i, lx, ux);
171         for (j = lx; j < ux; ++j)
172             if (max < row[j])
173             {
174                 max = row[j];
175                 if (y != NULL)
176                     *y = i;
177                 if (x != NULL)
178                     *x = j;
179             }
180     }
181 
182     fvector_free((void *) row, lx);
183     return (max);
184 }
185 
186 Imrect *imf_scale(Imrect * im1, double low, double high)
187 {
188     Imrect *im2;
189     Imregion *roi;
190     float *row1, *row2;
191     int lx, ux, ly, uy;
192     int i, j;
193     float min, max, scale;
194 
195     if (im1 == NULL)
196         return (NULL);
197 
198     roi = im1->region;
199     if (roi == NULL)
200         return (NULL);
201 
202     imf_minmax(im1, &min, &max);
203     if (max == min)
204         max = (float)(min + 1.0);
205 
206     scale = (float)((high - low) / (max - min));
207     low -= scale * min;
208 
209     lx = roi->lx;
210     ux = roi->ux;
211     ly = roi->ly;
212     uy = roi->uy;
213 
214     im2 = im_alloc(im1->height, im1->width, roi, float_v);
215     row1 = fvector_alloc(lx, ux);
216     row2 = fvector_alloc(lx, ux);
217 
218     for (i = ly; i < uy; ++i)
219     {
220         im_get_rowf(row1, im1, i, lx, ux);
221         for (j = lx; j < ux; ++j)
222             row2[j] = (float)(low + scale * row1[j]);
223         im_put_rowf(row2, im2, i, lx, ux);
224     }
225 
226     fvector_free((void *) row1, lx);
227     fvector_free((void *) row2, lx);
228     return (im2);
229 }
230 
231 Imrect *imf_scale_nzero(Imrect * im1, double low, double high)
232 {
233     Imrect *im2;
234     Imregion *roi;
235     float *row1, *row2;
236     int lx, ux, ly, uy;
237     int i, j;
238     float min, max, scale;
239  
240     if (im1 == NULL)
241         return (NULL);
242  
243     roi = im1->region;
244     if (roi == NULL)
245         return (NULL);
246  
247     imf_minmax_nzero(im1, &min, &max);
248     if (max == min)
249         if (min < 0.0) max = 0.0;
250         else if (max > 0.0) min = 0.0;
251         else min = (float)(max - 1.0);
252  
253     scale = (float)((high - low) / (max - min));
254     low -= scale * min;
255  
256     lx = roi->lx;
257     ux = roi->ux;
258     ly = roi->ly;
259     uy = roi->uy;
260  
261     im2 = im_alloc(im1->height, im1->width, roi, float_v);
262     row1 = fvector_alloc(lx, ux);
263     row2 = fvector_alloc(lx, ux);
264  
265     for (i = ly; i < uy; ++i)
266     {
267         im_get_rowf(row1, im1, i, lx, ux);
268         for (j = lx; j < ux; ++j)
269             row2[j] = (float)(low + scale * row1[j]);
270         im_put_rowf(row2, im2, i, lx, ux);
271     }
272  
273     fvector_free((void *) row1, lx);
274     fvector_free((void *) row2, lx);
275     return (im2);
276 }
277 
278 /* gamma scale the range oldlow, oldhigh in image im using newlow,
279  * newhigh, thresholding at threslow, threshigh */
280 void im_gamma_scale_range_inplace(Imrect * im,
281                                   double gamma,
282                                   double oldlow, double oldhigh,
283                                   double newlow, double newhigh,
284                                   double threslow, double threshigh)
285 {
286     Imregion *roi;
287     float *row1, *row2;
288     int lx, ux, ly, uy;
289     double scale, base, range;
290     int i, j;
291 
292     if (im == NULL)
293         return;
294 
295     roi = im->region;
296     if (roi == NULL)
297         return;
298     lx = roi->lx;
299     ux = roi->ux;
300     ly = roi->ly;
301     uy = roi->uy;
302 
303     if (oldlow == oldhigh)
304         scale = 1.0;
305     else
306         scale = 1.0 / (oldhigh - oldlow);
307     base = newlow;
308     range = newhigh - oldlow;
309 
310     row1 = fvector_alloc(lx, ux);
311     row2 = fvector_alloc(lx, ux);
312     for (i = ly; i < uy; ++i)
313     {
314         im_get_rowf(row1, im, i, lx, ux);
315         for (j = lx; j < ux; ++j)
316         {
317             double val;
318 
319             val = base + range * pow(scale * (row1[j] - oldlow), gamma);
320             val = MAX(val, threslow);
321             row2[j] = (float)MIN(val, threshigh);
322         }
323         im_put_rowf(row2, im, i, lx, ux);
324     }
325 
326     fvector_free((void *) row1, lx);
327     fvector_free((void *) row2, lx);
328 }
329 
330 /* scales the range oldlow, oldhigh in image im newlow, newhigh,
331  * thresholding at threslow, threshigh */
332 void im_scale_range_inplace(Imrect * im,
333                             double oldlow, double oldhigh,
334                             double newlow, double newhigh,
335                             double threslow, double threshigh)
336 {
337     Imregion *roi;
338     float *row1, *row2;
339     int lx, ux, ly, uy;
340     float scale, base;
341     int i, j;
342 
343     if (im == NULL)
344         return;
345 
346     roi = im->region;
347     if (roi == NULL)
348         return;
349     lx = roi->lx;
350     ux = roi->ux;
351     ly = roi->ly;
352     uy = roi->uy;
353 
354     if (oldlow == oldhigh)
355         scale = (float)1.0;
356     else
357         scale = (float)((newhigh - newlow) / (oldhigh - oldlow));
358     base = (float)(newlow - scale * oldlow);
359 
360     row1 = fvector_alloc(lx, ux);
361     row2 = fvector_alloc(lx, ux);
362     for (i = ly; i < uy; ++i)
363     {
364         im_get_rowf(row1, im, i, lx, ux);
365         for (j = lx; j < ux; ++j)
366         {
367             double val;
368 
369             val = base + scale * row1[j];
370             val = MAX(val, threslow);
371             row2[j] = (float)MIN(val, threshigh);
372         }
373         im_put_rowf(row2, im, i, lx, ux);
374     }
375 
376     fvector_free((void *) row1, lx);
377     fvector_free((void *) row2, lx);
378 }
379 
380 void imf_times_inplace(double k, Imrect * im)
381 {
382     Imregion *roi;
383     float *row;
384     int lx, ux, ly, uy;
385     int i, j;
386 
387     if (im == NULL)
388         return;
389     roi = im->region;
390     if (roi == NULL)
391         return;
392     lx = roi->lx;
393     ux = roi->ux;
394     ly = roi->ly;
395     uy = roi->uy;
396     row = fvector_alloc(lx, ux);
397     for (i = ly; i < uy; ++i)
398     {
399         im_get_rowf(row, im, i, lx, ux);
400         for (j = lx; j < ux; ++j)
401             row[j] *= (float)k;
402         im_put_rowf(row, im, i, lx, ux);
403     }
404     fvector_free((void *) row, lx);
405 }
406 
407 void imf_add_inplace(double k, Imrect * im)
408 {
409     Imregion *roi;
410     float *row;
411     int lx, ux, ly, uy;
412     int i, j;
413 
414     if (im == NULL)
415         return;
416     roi = im->region;
417     if (roi == NULL)
418         return;
419     lx = roi->lx;
420     ux = roi->ux;
421     ly = roi->ly;
422     uy = roi->uy;
423     row = fvector_alloc(lx, ux);
424     for (i = ly; i < uy; ++i)
425     {
426         im_get_rowf(row, im, i, lx, ux);
427         for (j = lx; j < ux; ++j)
428             row[j] += (float)k;
429         im_put_rowf(row, im, i, lx, ux);
430     }
431     fvector_free((void *) row, lx);
432 }
433 
434 void imf_accum_inplace(Imrect * im1, double k, Imrect * im2)
435 {
436     Imregion *roi1, *roi2;
437     float *row1, *row2;
438     int lx, ux, ly, uy;
439     int i, j;
440 
441     if (im1 == NULL || im2 == NULL)
442         return;
443 
444     roi1 = im1->region;
445     roi2 = im2->region;
446     if (roi1 == NULL || roi2 == NULL)
447         return;
448     lx = MAX(roi1->lx, roi2->lx);
449     ly = MAX(roi1->ly, roi2->ly);
450     ux = MIN(roi1->ux, roi2->ux);
451     uy = MIN(roi1->uy, roi2->uy);
452     row1 = fvector_alloc(lx, ux);
453     row2 = fvector_alloc(lx, ux);
454     for (i = ly; i < uy; ++i)
455     {
456         im_get_rowf(row1, im1, i, lx, ux);
457         im_get_rowf(row2, im2, i, lx, ux);
458         for (j = lx; j < ux; ++j)
459             row1[j] += (float)(k * row2[j]);
460         im_put_rowf(row1, im1, i, lx, ux);
461     }
462     fvector_free((void *) row1, lx);
463     fvector_free((void *) row2, lx);
464 }
465 
466 double imf_mean(Imrect * im)
467 {
468     Imregion *roi;
469     float *row;
470     double sum = 0;
471     int lx, ux, ly, uy;
472     int i, j;
473 
474     if (im == NULL)
475         return(0);
476     roi = im->region;
477     if (roi == NULL)
478         return(0);
479     lx = roi->lx;
480     ux = roi->ux;
481     ly = roi->ly;
482     uy = roi->uy;
483     row = fvector_alloc(lx, ux);
484     for (i = ly; i < uy; ++i)
485     {
486         im_get_rowf(row, im, i, lx, ux);
487         for (j = lx; j < ux; ++j)
488             sum += row[j];
489     }
490     fvector_free((void *) row, lx);
491     return(sum/((uy-ly)*(ux-lx)));
492 }
493 
494 void imf_scale_inplace(Imrect * im, double low, double high)
495 {
496     Imregion *roi;
497     float *row1, *row2;
498     int lx, ux, ly, uy;
499     float min, max, scale;
500     int i, j;
501 
502     if (im == NULL)
503         return;
504 
505     roi = im->region;
506     if (roi == NULL)
507         return;
508     lx = roi->lx;
509     ux = roi->ux;
510     ly = roi->ly;
511     uy = roi->uy;
512 
513     imf_minmax(im, &min, &max);
514     if (max == min)
515         max = (float)(min + 1.0);
516 
517     scale = (float)((high - low) / (max - min));
518     low -= scale * min;
519 
520     row1 = fvector_alloc(lx, ux);
521     row2 = fvector_alloc(lx, ux);
522 
523     for (i = ly; i < uy; ++i)
524     {
525         im_get_rowf(row1, im, i, lx, ux);
526         for (j = lx; j < ux; ++j)
527             row2[j] = (float)(low + scale * row1[j]);
528         im_put_rowf(row2, im, i, lx, ux);
529     }
530 
531     fvector_free((void *) row1, lx);
532     fvector_free((void *) row2, lx);
533 }
534 
535 float im_locate_max(Imrect * im, int *y, int *x)
536 {
537     Imregion *roi;
538     float *row, max;
539     int lx, ux, ly, uy, xmax, ymax;
540     int i, j;
541 
542     if (im == NULL)
543     {
544         *y = *x = 0;
545         return(0);
546     }
547     roi = im->region;
548     if (roi == NULL)
549     {
550         *y = *x = 0;
551         return(0);
552     }
553     lx = roi->lx;
554     ux = roi->ux;
555     ly = roi->ly;
556     uy = roi->uy;
557 
558     row = fvector_alloc(lx, ux);
559     max = (float)im_get_pixf(im, ly, lx);
560     xmax = lx;
561     ymax = ly;
562     for (i = ly; i < uy; ++i)
563     {
564         im_get_rowf(row, im, i, lx, ux);
565         for (j = lx; j < ux; ++j)
566             if (max < row[j])
567             {
568                 max = row[j];
569                 xmax = j;
570                 ymax = i;
571             }
572     }
573 
574     *x = xmax;
575     *y = ymax;
576     fvector_free((void *) row, lx);
577     return(max);
578 }
579 
580 /* WARNING: `miny' may be used uninitialized in this function */
581 void im_locate_interest(Imrect * im, int *y, int *x)
582 {
583     Imregion *roi;
584     float *row1, *row2;
585     double max;
586     int lx, ux, ly, uy, minx, miny = 0;
587     int i, j;
588 
589     if (im == NULL)
590     {
591         *y = *x = 0;
592         return;
593     }
594     roi = im->region;
595     if (roi == NULL)
596     {
597         *y = *x = 0;
598         return;
599     }
600     lx = roi->lx;
601     ux = roi->ux;
602     ly = roi->ly;
603     uy = roi->uy;
604 
605     max = 0.0;
606     row1 = fvector_alloc(lx, ux);
607     row2 = fvector_alloc(lx, ux);
608     im_get_rowf(row2, im, ly, lx, ux);
609     for (i = ly + 1; i < uy - 1; ++i)
610     {
611         SWAP(float *, row1, row2);
612 
613         im_get_rowf(row2, im, i + 1, lx, ux);
614         for (j = lx + 1; j < ux - 1; ++j)
615         {
616             double dxy;
617 
618             dxy = fabs(row1[j - 1] - row2[j + 1]) + fabs(row1[j + 1] - row2[j - 1]);
619             if (max < dxy)
620             {
621                 max = dxy;
622                 minx = j;
623                 miny = i;
624             }
625         }
626     }
627 
628     *x = minx;
629     *y = miny;
630     fvector_free((void *) row1, lx);
631     fvector_free((void *) row2, lx);
632 }
633 

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