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 #ifndef MLN_WORLD_BINARY_2D_SUBSAMPLE_HH
00028 # define MLN_WORLD_BINARY_2D_SUBSAMPLE_HH
00029
00036
00037 # include <mln/core/image/image2d.hh>
00038 # include <mln/core/alias/dpoint2d.hh>
00039 # include <mln/data/convert.hh>
00040 # include <mln/value/int_u8.hh>
00041
00042 namespace mln
00043 {
00044
00045 namespace world
00046 {
00047
00048 namespace binary_2d
00049 {
00050
00057
00058 image2d<value::int_u8>
00059 subsample(image2d<bool>& input, unsigned n);
00060
00061
00062 # ifndef MLN_INCLUDE_ONLY
00063
00064 inline
00065 image2d<value::int_u8>
00066 subsample(image2d<bool>& input, unsigned n)
00067 {
00068 trace::entering("world::binary_2d::subsample");
00069
00070 mln_precondition(input.is_valid());
00071
00072 using value::int_u8;
00073
00074 if (n == 0)
00075 {
00076 image2d<value::int_u8>
00077 output = data::convert(int_u8(), input);
00078
00079 trace::exiting("world::binary_2d::subsample");
00080 return output;
00081 }
00082
00083 const bool** ptr = new const bool*[n];
00084 const unsigned nrows = input.nrows() / n;
00085 const unsigned ncols = input.ncols() / n;
00086 algebra::vec<2, unsigned int> vmin;
00087 algebra::vec<2, unsigned int> vmax;
00088 vmin[0] = 0;
00089 vmin[1] = 0;
00090 vmax[0] = nrows - 1;
00091 vmax[1] = ncols - 1;
00092 point2d pmin(vmin);
00093 point2d pmax(vmax);
00094 image2d<int_u8> output(box<point2d>(pmin, pmax));
00095
00096 dpoint2d dp_row(1, 0);
00097 const unsigned delta_row = input.delta_index(dp_row);
00098 unsigned count = 0;
00099
00100 for (unsigned row = 0; row < nrows; ++row)
00101 {
00102 ptr[0] = & input(point2d(n * row, 0));
00103 for (unsigned i = 1; i < n; ++i)
00104 ptr[i] = ptr[i - 1] + delta_row;
00105 for (unsigned col = 0; col < ncols; ++col)
00106 {
00107 count = 0;
00108 for (unsigned i = 0; i < n; ++i)
00109 {
00110 for (unsigned j = 0; j < n; ++j, ++(ptr[i]))
00111 {
00112 if (*(ptr[i]))
00113 ++count;
00114 }
00115 }
00116 output(point2d(row, col)) = count * 255 / n / n;
00117 }
00118 }
00119
00120 trace::exiting("world::binary_2d::subsample");
00121 return output;
00122 }
00123
00124 # endif // ! MLN_INCLUDE_ONLY
00125
00126 }
00127
00128 }
00129
00130 }
00131
00132 #endif // ! MLN_WORLD_BINARY_2D_SUBSAMPLE_HH