| 98 | typename out_image_type |
| 99 | > |
| 100 | void equalize_histogram ( |
| 101 | const in_image_type& in_img_, |
| 102 | out_image_type& out_img_ |
| 103 | ) |
| 104 | { |
| 105 | const_image_view<in_image_type> in_img(in_img_); |
| 106 | image_view<out_image_type> out_img(out_img_); |
| 107 | |
| 108 | typedef typename image_traits<in_image_type>::pixel_type in_pixel_type; |
| 109 | typedef typename image_traits<out_image_type>::pixel_type out_pixel_type; |
| 110 | |
| 111 | COMPILE_TIME_ASSERT( pixel_traits<in_pixel_type>::has_alpha == false ); |
| 112 | COMPILE_TIME_ASSERT( pixel_traits<out_pixel_type>::has_alpha == false ); |
| 113 | |
| 114 | COMPILE_TIME_ASSERT( pixel_traits<in_pixel_type>::is_unsigned == true ); |
| 115 | COMPILE_TIME_ASSERT( pixel_traits<out_pixel_type>::is_unsigned == true ); |
| 116 | |
| 117 | typedef typename pixel_traits<in_pixel_type>::basic_pixel_type in_image_basic_pixel_type; |
| 118 | COMPILE_TIME_ASSERT( sizeof(in_image_basic_pixel_type) <= 2); |
| 119 | |
| 120 | |
| 121 | // if there isn't any input image then don't do anything |
| 122 | if (in_img.size() == 0) |
| 123 | { |
| 124 | out_img.clear(); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | out_img.set_size(in_img.nr(),in_img.nc()); |
| 129 | |
| 130 | unsigned long p; |
| 131 | |
| 132 | matrix<unsigned long,1,0> histogram; |
| 133 | get_histogram(in_img_, histogram); |
| 134 | in_img = in_img_; |
| 135 | |
| 136 | double scale = pixel_traits<out_pixel_type>::max(); |
| 137 | if (in_img.size() > histogram(0)) |
| 138 | scale /= in_img.size()-histogram(0); |
| 139 | else |
| 140 | scale = 0; |
| 141 | |
| 142 | // make the black pixels remain black in the output image |
| 143 | histogram(0) = 0; |
| 144 | |
| 145 | // compute the transform function |
| 146 | for (long i = 1; i < histogram.size(); ++i) |
| 147 | histogram(i) += histogram(i-1); |
| 148 | // scale so that it is in the range [0,pixel_traits<out_pixel_type>::max()] |
| 149 | for (long i = 0; i < histogram.size(); ++i) |
| 150 | histogram(i) = static_cast<unsigned long>(histogram(i)*scale); |
| 151 | |
| 152 | // now do the transform |
| 153 | for (long row = 0; row < in_img.nr(); ++row) |
| 154 | { |
| 155 | for (long col = 0; col < in_img.nc(); ++col) |
| 156 | { |
| 157 | p = histogram(get_pixel_intensity(in_img[row][col])); |