Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // generates a smooth thresholded image by doing stuff
- image_t *img_scalarautothreshold(image_t *img, float thresh, float bias)
- {
- float min, max, range, f, *s = (float *)img->data, *d;
- int w = img->w, h = img->h, x, y, o, px, py;
- image_t *ret;
- if(!(ret = img_new(w, h, img->chans, 0)))
- return 0;
- d = (float *)ret->data;
- // for each pixul
- for(y = 0; y < h; y++)
- for(x = 0; x < w; x++)
- {
- min = 99999.0;
- max = -99999.0;
- // find min/max in 3x3 neighbz
- for(o = 0; o < 9; o++)
- {
- px = x + (o % 3) - 1;
- py = y + (o / 3) - 1;
- if(px < 0 || py < 0 || px >= w || py >= h)
- continue;
- // get value for neighborhood pixels
- f = *(float *)&img->data[(px + (py * w)) * sizeof(float)] - thresh;
- if(f < min)
- min = f;
- if(f > max)
- max = f;
- }
- // calc range
- range = max - min;
- f = *s++ - thresh;
- // avoid divzero
- if(m_abs(range) > 0.0000001)
- f /= 0.5 * range; // double up or boundary is too soft
- else
- f *= 999999;
- *d++ = f + bias;
- }
- return ret;
- }
- //
Advertisement
Add Comment
Please, Sign In to add comment