Advertisement
deftware

Antialiased Image Threshold Function

Mar 24th, 2018
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. // generates a smooth thresholded image by doing stuff
  2. image_t *img_scalarautothreshold(image_t *img, float thresh, float bias)
  3. {
  4.     float min, max, range, f, *s = (float *)img->data, *d;
  5.     int w = img->w, h = img->h, x, y, o, px, py;
  6.     image_t *ret;
  7.  
  8.     if(!(ret = img_new(w, h, img->chans, 0)))
  9.         return 0;
  10.  
  11.     d = (float *)ret->data;
  12.  
  13.     // for each pixul
  14.     for(y = 0; y < h; y++)
  15.     for(x = 0; x < w; x++)
  16.     {
  17.         min = 99999.0;
  18.         max = -99999.0;
  19.  
  20.         // find min/max in 3x3 neighbz
  21.         for(o = 0; o < 9; o++)
  22.         {
  23.             px = x + (o % 3) - 1;
  24.             py = y + (o / 3) - 1;
  25.  
  26.             if(px < 0 || py < 0 || px >= w || py >= h)
  27.                 continue;
  28.  
  29.             // get value for neighborhood pixels
  30.             f = *(float *)&img->data[(px + (py * w)) * sizeof(float)] - thresh;
  31.  
  32.             if(f < min)
  33.                 min = f;
  34.  
  35.             if(f > max)
  36.                 max = f;
  37.         }
  38.  
  39.         // calc range
  40.         range = max - min;
  41.         f = *s++ - thresh;
  42.  
  43.         // avoid divzero
  44.         if(m_abs(range) > 0.0000001)
  45.             f /= 0.5 * range;   // double up or boundary is too soft
  46.         else
  47.             f *= 999999;
  48.  
  49.         *d++ = f + bias;
  50.     }
  51.  
  52.     return ret;
  53. }
  54. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement