Go to the documentation of this file.00001 #include "normalize_image.hpp"
00002
00003 #include <math.h>
00004 #include "tictoc.hpp"
00005
00006 namespace fovis
00007 {
00008
00009 static void
00010 mean_stddev(const uint8_t * buf,
00011 int stride, int width, int height,
00012 float *mean, float *stddev) {
00013 float sum = 0;
00014 float sqr_sum = 0;
00015 for (int i = 0; i < height; ++i) {
00016 const uint8_t * row_ptr = buf + i*stride;
00017 for (int j = 0; j < width; ++j) {
00018 float x = row_ptr[j];
00019 sum += x;
00020 sqr_sum += (x * x);
00021 }
00022 }
00023 float n = width * height;
00024 *mean = sum / n;
00025 *stddev = sqrt( (sqr_sum - sum * *mean)/n );
00026 }
00027
00028 void
00029 normalize_image(uint8_t * buf, int stride, int width, int height) {
00030 const float NEW_MEAN = 128.;
00031 const float NEW_SD = 74.;
00032 float mean, stddev;
00033 mean_stddev(buf, stride, width, height, &mean, &stddev);
00034
00035 static uint8_t lookup_table[256];
00036 for (int i=0; i < 256; ++i) {
00037 int new_val = (int)((i-mean)*(NEW_SD/stddev) + NEW_MEAN);
00038
00039 lookup_table[i] = std::min(std::max(new_val, 0), 255);
00040 }
00041 for (int i = 0; i < height; ++i) {
00042 uint8_t* row_ptr = buf + i*stride;
00043 for (int j = 0; j < width; ++j) {
00044 row_ptr[j] = lookup_table[row_ptr[j]];
00045 }
00046 }
00047 }
00048
00049 }