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_MISC_HH
00029 # define OLENA_LRDE_EFIGI_MISC_HH
00030
00031 #include <cmath>
00032 #include <cstdlib>
00033
00034 #include <vector>
00035 #include <algorithm>
00036 #include <fstream>
00037
00038 #include <oln/basics2d.hh>
00039
00040
00041 namespace oln
00042 {
00043
00044 namespace efigi
00045 {
00046
00047
00048 void min_max(const image2d<float>& ima, float& min, float& max)
00049 {
00050 min = 1.0E30;
00051 max = -1.0E30;
00052 const int nrows = ima.nrows(), ncols = ima.ncols();
00053 for (int row = 0; row < nrows; ++row)
00054 for (int col = 0; col < ncols; ++col)
00055 {
00056 if (ima(row, col) < min)
00057 min = ima(row, col);
00058 if (ima(row, col) > max)
00059 max = ima(row, col);
00060 }
00061 }
00062
00063
00064
00065 image2d<float> add_float_noise(const image2d<unsigned char>& input)
00066 {
00067 image2d<float> output(input.nrows(), input.ncols());
00068 for (int row = 0; row < input.nrows(); ++row)
00069 for (int col = 0; col < input.ncols(); ++col)
00070 output(row, col) = input(row, col) + float(rand() / RAND_MAX);
00071 return output;
00072 }
00073
00074
00075
00076 bool diff(const image2d<float>& ima1, const image2d<float>& ima2)
00077 {
00078 const float err = 1E-5;
00079 assert(ima1.nrows() == ima2.nrows() && ima1.ncols() == ima2.ncols());
00080 for (int row = 0; row < ima1.nrows(); ++row)
00081 for (int col = 0; col < ima1.ncols(); ++col)
00082 {
00083 float sim =
00084 std::min(ima1(row, col), ima2(row, col))
00085 / std::max(ima1(row, col), ima2(row, col));
00086 if (sim < 1 - err)
00087 return true;
00088 }
00089 return false;
00090 }
00091
00092
00093
00094 std::vector<unsigned> compute_histo(const image2d<unsigned char>& input)
00095 {
00096 std::vector<unsigned> h(256, 0);
00097 const int nrows = input.nrows(), ncols = input.ncols();
00098 for (int row = 0; row < nrows; ++row)
00099 for (int col = 0; col < ncols; ++col)
00100 ++h[input(row, col)];
00101 return h;
00102 }
00103
00104 std::vector<unsigned> compute_histo(const image2d<unsigned short>& input)
00105 {
00106 std::vector<unsigned> h(65536, 0);
00107 const int nrows = input.nrows(), ncols = input.ncols();
00108 for (int row = 0; row < nrows; ++row)
00109 for (int col = 0; col < ncols; ++col)
00110 ++h[input(row, col)];
00111 return h;
00112 }
00113
00114 void print_histo(std::vector<unsigned>& h,
00115 std::ofstream& file)
00116 {
00117 for (unsigned i = 0; i < h.size(); ++i)
00118 file << i << ' ' << h[i] << std::endl;
00119 }
00120
00121
00122 struct pix
00123 {
00124 pix() {}
00125 pix(float val, int row, int col) : val(val), row(row), col(col) {}
00126 bool operator<(const pix& rhs) const {
00127 if (val < rhs.val)
00128 return true;
00129 if (val > rhs.val)
00130 return false;
00131 return row < rhs.row or (row == rhs.row and col < rhs.col);
00132 }
00133 float val;
00134 int row, col;
00135 };
00136
00137
00138 std::vector<pix>
00139 sort(const image2d<float>& input)
00140 {
00141 std::vector<pix> v;
00142
00143 const int nrows = input.nrows(), ncols = input.ncols();
00144 v.reserve(nrows * ncols);
00145 for (int row = 0; row < nrows; ++row)
00146 for (int col = 0; col < ncols; ++col)
00147 v.push_back(pix(input(row, col), row, col));
00148 std::sort(v.begin(), v.end());
00149 return v;
00150 }
00151
00152
00153
00154 template <typename T>
00155 image2d<T>
00156 crop(const image2d<T>& input, coord x, coord y, coord dx, coord dy)
00157 {
00158 assertion (dx > 0);
00159 assertion (dy > 0);
00160 assertion(input.hold(point2d(x, y)));
00161 assertion(input.hold(point2d(x + dx, y + dy)));
00162
00163 image2d<float> output(dx, dy);
00164 for (coord row = 0; row < dx; ++row)
00165 for (coord col = 0; col < dy; ++col)
00166 output(row, col) = input(x + row, y + col);
00167 return output;
00168 }
00169
00170
00171 }
00172
00173 }
00174
00175
00176 #endif // ! OLENA_LRDE_EFIGI_MISC_HH