Go to the documentation of this file.00001 #ifndef OPENTISSUE_UTILITY_IMAGE_ANALYSIS_IMAGE_ANALYSIS_COMPUTE_INTENSITY_GRADIENT_HISTOGRAM_H
00002 #define OPENTISSUE_UTILITY_IMAGE_ANALYSIS_IMAGE_ANALYSIS_COMPUTE_INTENSITY_GRADIENT_HISTOGRAM_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/containers/grid/grid.h>
00013 #include <OpenTissue/core/containers/grid/util/grid_transform.h>
00014 #include <OpenTissue/core/math/math_basic_types.h>
00015
00016 #include <boost/bind.hpp>
00017
00018 #include <iostream>
00019 #include <fstream>
00020 #include <cmath>
00021 #include <algorithm>
00022
00023 namespace OpenTissue
00024 {
00025
00026 namespace image_analysis
00027 {
00028
00029 namespace detail
00030 {
00035 template<typename grid_iterator>
00036 class GridGradientMagnitudeFunctor
00037 {
00038 public:
00039
00040 typename grid_iterator::math_types::real_type
00041 operator()( grid_iterator const & iter )
00042 {
00043 return OpenTissue::grid::gradient_magnitude(iter);
00044 }
00045
00046 };
00047 }
00048
00049
00050
00060 template< typename image_type, typename histogram_type >
00061 void compute_intensity_gradient_histogram(
00062 image_type const & U
00063 , unsigned short const & cnt_intensity_buckets
00064 , unsigned short const & cnt_grad_mag_buckets
00065 , histogram_type & histogram
00066 , OpenTissue::grid::Grid<size_t, OpenTissue::math::default_math_types> & index_grid
00067 )
00068 {
00069 typedef typename image_type::const_index_iterator intensity_iterator;
00070 typedef typename image_type::value_type intensity_type;
00071
00072 typedef double real_type;
00073
00074 typedef OpenTissue::grid::Grid<real_type,math::default_math_types> grid_type;
00075
00076 typedef typename grid_type::index_iterator grid_iterator;
00077 typedef OpenTissue::math::Vector3<real_type> vector3_type;
00078
00079
00080 {
00081 for(size_t g=0;g<cnt_grad_mag_buckets;++g)
00082 for(size_t i=0;i<cnt_intensity_buckets;++i)
00083 histogram[i][g] = 0;
00084 }
00085
00086
00087 grid_type tmp;
00088 tmp.create(U.min_coord(),U.max_coord(),U.I(),U.J(),U.K());
00089
00090
00091 index_grid.create(U.min_coord(),U.max_coord(),U.I(),U.J(),U.K());
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 OpenTissue::grid::transform(
00105 U.begin()
00106 , U.end()
00107 , tmp.begin()
00108
00109
00110 , detail::GridGradientMagnitudeFunctor<intensity_iterator>()
00111 );
00112
00113
00114 double min_val = 1.0*min(tmp);
00115 double max_val = 1.0*max(tmp);
00116 double min_intensity = 1.0*min(U);
00117 double max_intensity = 1.0*max(U);
00118 double i_width = (max_intensity-min_intensity)/cnt_intensity_buckets;
00119 double g_width = (max_val-min_val)/cnt_grad_mag_buckets;
00120
00121
00122
00123
00124 intensity_iterator Ibegin = U.begin();
00125 intensity_iterator Iend = U.end();
00126 intensity_iterator i;
00127 grid_iterator Gbegin = tmp.begin();
00128 grid_iterator Gend = tmp.end();
00129 grid_iterator g;
00130 for(i=Ibegin,g=Gbegin;i!=Iend;++i,++g)
00131 {
00132 size_t i_bucket = static_cast<size_t>( std::floor((*i - min_intensity)/i_width) );
00133 size_t g_bucket = static_cast<size_t>( std::floor((*g - min_val)/g_width) );
00134 if(i_bucket==cnt_intensity_buckets) --i_bucket;
00135 if(g_bucket==cnt_grad_mag_buckets) --g_bucket;
00136 assert(i_bucket<cnt_intensity_buckets);
00137 assert(g_bucket<cnt_grad_mag_buckets);
00138
00139 index_grid(i.get_index()) = static_cast<size_t>((i_bucket & 0x00FF) << 16) | (g_bucket & 0x00FF);
00140 ++(histogram[i_bucket][g_bucket]);
00141 }
00142 }
00143
00144 }
00145
00146 }
00147
00148
00149 #endif