00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef OLENA_LRDE_EFIGI_REQ_HH
00029 # define OLENA_LRDE_EFIGI_REQ_HH
00030
00031 #include <iostream>
00032 #include <cmath>
00033 #include <map>
00034 #include <oln/basics2d.hh>
00035
00036 #include <oln/lrde/efigi/misc.hh>
00037
00038
00039 namespace oln
00040 {
00041
00042 namespace efigi
00043 {
00044
00045
00046 image2d<unsigned char>
00047 convert_linear_8(const image2d<float>& input,
00048 float min, float max)
00049 {
00050 float coef = 256.f / (max - min);
00051 const int nrows = input.nrows(), ncols = input.ncols();
00052 image2d<unsigned char> output(nrows, ncols);
00053 for (int row = 0; row < nrows; ++row)
00054 for (int col = 0; col < ncols; ++col)
00055 {
00056 float dest = min + coef * input(row, col);
00057 if (dest < 0.)
00058 output(row, col) = 0;
00059 else if (dest > 255.)
00060 output(row, col) = 255;
00061 else
00062 output(row, col) = unsigned(dest);
00063 }
00064 return output;
00065 }
00066
00067
00068 image2d<unsigned short>
00069 convert_linear_16(const image2d<float>& input,
00070 float min, float max)
00071 {
00072 float coef = 65536.f / (max - min);
00073 const int nrows = input.nrows(), ncols = input.ncols();
00074 image2d<unsigned short> output(nrows, ncols);
00075 for (int row = 0; row < nrows; ++row)
00076 for (int col = 0; col < ncols; ++col)
00077 {
00078 float dest = min + coef * input(row, col);
00079 if (dest < 0.)
00080 output(row, col) = 0;
00081 else if (dest > 65535.)
00082 output(row, col) = 65535;
00083 else
00084 output(row, col) = unsigned(dest);
00085 }
00086 return output;
00087 }
00088
00089
00090 float slog(float f, float sigma)
00091 {
00092 return f < 0 ? - std::log(1. - f / sigma) : std::log(1. + f / sigma);
00093 }
00094
00095
00096 image2d<unsigned char>
00097 convert_log(const image2d<float>& input,
00098 float min, float max,
00099 float sigma)
00100 {
00101 assert(sigma > 0);
00102 assert(max > min);
00103
00104 float orig = slog(min, sigma);
00105 float L = (slog(max, sigma) - orig) / 255.9999f;
00106
00107 unsigned too_low = 0, too_high = 0;
00108 const int nrows = input.nrows(), ncols = input.ncols();
00109 image2d<unsigned char> output(nrows, ncols);
00110 for (int row = 0; row < nrows; ++row)
00111 for (int col = 0; col < ncols; ++col)
00112 {
00113 float f = input(row, col), f_dyn = 0;
00114 if (f >= max)
00115 output(row, col) = 255;
00116 else
00117 if (f <= min)
00118 output(row, col) = 0;
00119 else
00120
00121 {
00122 f_dyn = (slog(f, sigma) - orig) / L;
00123 if (f_dyn < 0)
00124 ++too_low;
00125 if (f_dyn > 255)
00126 ++too_high;
00127 }
00128 output(row, col) = unsigned(f_dyn);
00129 }
00130
00131 if (too_low || too_high)
00132 std::cerr << "pb in efigi::convert_log: "
00133 << too_low << ' ' << too_high << std::endl;
00134
00135 return output;
00136 }
00137
00138
00139 image2d<unsigned char>
00140 equalize(const image2d<float>& input)
00141 {
00142 std::vector<pix> v = efigi::sort(input);
00143 const int nrows = input.nrows(), ncols = input.ncols();
00144 image2d<unsigned char> output(nrows, ncols);
00145
00146 unsigned n = unsigned((nrows * ncols) / 256.f + 1.f);
00147 unsigned l = 0;
00148 for (;;)
00149 {
00150 for (unsigned i = 0; i < n; ++i)
00151 {
00152 unsigned j = l * n + i;
00153 if (j >= v.size())
00154 return output;
00155 pix& p = v[l * n + i];
00156 output(p.row, p.col) = l;
00157 }
00158 ++l;
00159 }
00160 return output;
00161 }
00162
00163 image2d<unsigned short>
00164 equalize_16(const image2d<float>& input)
00165 {
00166 std::vector<pix> v = efigi::sort(input);
00167 const int nrows = input.nrows(), ncols = input.ncols();
00168 image2d<unsigned short> output(nrows, ncols);
00169
00170 unsigned n = unsigned((nrows * ncols) / 65536.f + 1.f);
00171 unsigned l = 0;
00172 for (;;)
00173 {
00174 for (unsigned i = 0; i < n; ++i)
00175 {
00176 unsigned j = l * n + i;
00177 if (j >= v.size())
00178 return output;
00179 pix& p = v[l * n + i];
00180 output(p.row, p.col) = l;
00181 }
00182 ++l;
00183 }
00184 return output;
00185 }
00186
00187
00188 image2d<bool>
00189 lab2bin(const image2d<unsigned char>& input)
00190 {
00191 const int nrows = input.nrows(), ncols = input.ncols();
00192 image2d<bool> output(nrows, ncols);
00193 for (int row = 0; row < nrows; ++row)
00194 for (int col = 0; col < ncols; ++col)
00195 output(row, col) = (input(row, col) == 255);
00196 return output;
00197 }
00198
00199
00200 }
00201
00202 }
00203
00204
00205 #endif // ! OLENA_LRDE_EFIGI_REQ_HH