11#include < iostream>
22#include " x3f_denoise_aniso.h"
3+ #include " x3f_printf.h"
34#include < algorithm> // for std::sort for the median filter
45
56static inline float determine_pixel_difference (const float & v1_1, const float & v1_2, const float & v1_3,
@@ -30,7 +31,7 @@ void median_filter(x3f_area16_t *image)
3031 const uint32_t edgeless_rows = rows - 1 ;
3132 const uint32_t jump = image->channels ;
3233 const uint32_t stride = image->row_stride ;
33-
34+
3435 uint16_t * current_set = new uint16_t [9 ];
3536 memset (current_set, 0 , sizeof (uint16_t )*9 );
3637 uint16_t * current_ptr;
@@ -40,9 +41,9 @@ void median_filter(x3f_area16_t *image)
4041 // gonna do median filtering by channel for now, to start with.
4142 uint16_t * scratch = new uint16_t [rows * cols * jump];
4243 float avg;
43-
44+
4445 memcpy (scratch, image->data , rows*cols*jump*sizeof (uint16_t ));
45-
46+
4647 for (y = 1 ; y < edgeless_rows; y++){
4748 for (x = 1 ; x < edgeless_cols; x++) {
4849 for (i = 0 ; i < jump; i++){
@@ -78,8 +79,8 @@ void median_filter(x3f_area16_t *image)
7879 }
7980 // copy from scratch back into the image
8081 memcpy (image->data , scratch, rows*cols*jump*sizeof (uint16_t ));
81-
82-
82+
83+
8384 delete [] scratch;
8485 delete [] current_set;
8586}
@@ -101,20 +102,20 @@ void denoise_aniso_float(const uint32_t& in_rows, const uint32_t& in_columns, fl
101102 // not ideal, because there will be noise there, but it should avoid the green boundary problem.
102103 // there are a number of ways to solve boundary problems, but none of them are efficient.
103104 memcpy (out_image, image, in_rows * in_columns * jump * sizeof (float ));
104-
105+
105106 // diffusion for the first channel is different, to prevent color bleed
106107 float * h = new float [jump];
107108 h[0 ] = 5 .0f ;
108109 h[1 ] = 1 .0f ;
109110 h[2 ] = 1 .0f ;
110-
111+
111112 for (y = 1 ; y < edgeless_rows; y++){
112113 for (x = 1 ,
113114 out_ptr = (out_image + y * row_stride + jump),
114115 in_ptr = (image + y * row_stride + jump);
115116 x < edgeless_cols;
116117 x++, out_ptr += jump, in_ptr += jump){
117-
118+
118119 coeff_fwd = determine_pixel_difference (*in_ptr, *(in_ptr+1 ), *(in_ptr+2 ),
119120 *(in_ptr + jump), *(in_ptr + jump + 1 ), *(in_ptr + jump + 2 ));
120121 coeff_bkwd = determine_pixel_difference (*in_ptr, *(in_ptr+1 ), *(in_ptr+2 ),
@@ -123,12 +124,12 @@ void denoise_aniso_float(const uint32_t& in_rows, const uint32_t& in_columns, fl
123124 *(in_ptr + row_stride), *(in_ptr + row_stride + 1 ), *(in_ptr + row_stride + 2 ));
124125 coeff_up = determine_pixel_difference (*in_ptr, *(in_ptr+1 ), *(in_ptr+2 ),
125126 *(in_ptr - row_stride), *(in_ptr - row_stride + 1 ), *(in_ptr - row_stride + 2 ));
126-
127+
127128 // uncomment out this line to do isotropic noise reduction
128129 // coeff_fwd = coeff_up = coeff_down = coeff_bkwd = -1.0f;
129130 lambda1 = coeff_fwd + coeff_bkwd + coeff_down + coeff_up;
130131 lambda2 = fabs (lambda1);
131-
132+
132133 for (i = 0 ; i < jump; i++)
133134 {
134135 *(out_ptr + i) = *(in_ptr + i) - (lambda2 * *(in_ptr + i) + coeff_fwd * *(in_ptr + jump + i)
@@ -139,7 +140,7 @@ void denoise_aniso_float(const uint32_t& in_rows, const uint32_t& in_columns, fl
139140 }
140141 }
141142 }
142-
143+
143144 memcpy (image, out_image, in_rows * in_columns * jump * sizeof (float ));
144145 delete [] out_image;
145146 delete [] h;
@@ -153,7 +154,7 @@ void denoise_aniso(x3f_area16_t *image, const int& in_iterations)
153154 float * float_image = convert_to_float_image (image);
154155 for (int i = 0 ; i < in_iterations; i++){
155156 denoise_aniso_float (image->rows , image->columns , float_image);
156- std::cout << " iteration: " << i << std::endl ;
157+ x3f_printf (DEBUG, " iteration: %d \n " , i) ;
157158 }
158159 convert_from_float_image (image, float_image);
159160 delete [] float_image;
@@ -178,36 +179,36 @@ void denoise_iso_float(const uint32_t& in_rows, const uint32_t& in_columns, floa
178179 // not ideal, because there will be noise there, but it should avoid the green boundary problem.
179180 // there are a number of ways to solve boundary problems, but none of them are efficient.
180181 memcpy (out_image, image, in_rows * in_columns * jump * sizeof (float ));
181-
182+
182183 // diffusion for the first channel is different, to prevent color bleed
183184 float * h = new float [jump];
184185 h[0 ] = 5 .0f ;
185186 h[1 ] = 1 .0f ;
186187 h[2 ] = 1 .0f ;
187-
188+
188189 coeff_fwd = coeff_up = coeff_down = coeff_bkwd = -1 .0f ;
189190 lambda1 = coeff_fwd + coeff_bkwd + coeff_down + coeff_up;
190191 lambda2 = fabs (lambda1);
191-
192+
192193 for (y = 1 ; y < edgeless_rows; y++){
193194 for (x = 1 ,
194195 out_ptr = (out_image + y * row_stride + jump),
195196 in_ptr = (image + y * row_stride + jump);
196197 x < edgeless_cols;
197198 x++, out_ptr += jump, in_ptr += jump){
198-
199-
199+
200+
200201 for (i = 0 ; i < jump; i++)
201202 {
202203 *(out_ptr + i) = *(in_ptr + i) - (lambda2 * *(in_ptr + i) + coeff_fwd * *(in_ptr + jump + i)
203204 + coeff_bkwd * *(in_ptr - jump + i)
204205 + coeff_down * *(in_ptr + row_stride + i)
205206 + coeff_up * *(in_ptr - row_stride + i))/(h[i] *lambda2);
206-
207+
207208 }
208209 }
209210 }
210-
211+
211212 memcpy (image, out_image, in_rows * in_columns * jump * sizeof (float ));
212213 delete [] out_image;
213214 delete [] h;
@@ -221,7 +222,7 @@ void denoise_iso(x3f_area16_t *image, const int& in_iterations)
221222 float * float_image = convert_to_float_image (image);
222223 for (int i = 0 ; i < in_iterations; i++){
223224 denoise_iso_float (image->rows , image->columns , float_image);
224- std::cout << " iteration: " << i << std::endl ;
225+ x3f_printf (DEBUG, " iteration: %d \n " , i) ;
225226 }
226227 convert_from_float_image (image, float_image);
227228 delete [] float_image;
@@ -244,19 +245,19 @@ void morphological_op(x3f_area16_t *image, const int& in_radius, const bool& ero
244245{
245246 const int ysize = image->rows ; // forcing a cast to signed for boundary conditions
246247 const int xsize = image->columns ;
247-
248+
248249 uint16_t (*determinant)(const uint16_t &, const uint16_t &);
249250 determinant = &give_max;
250251 uint16_t seed = 0 ;
251252 if (erode){
252253 determinant = &give_min;
253254 seed = 65535 ;
254255 }
255-
256+
256257 const int the_edge = in_radius*2 + 1 ;
257258 bool ** theMask = new bool *[the_edge];
258259 const int sqrRad = in_radius*in_radius;
259-
260+
260261 int x, y;
261262 for (y = -in_radius; y <= in_radius; y++){
262263 theMask[y + in_radius] = new bool [the_edge];
@@ -268,7 +269,7 @@ void morphological_op(x3f_area16_t *image, const int& in_radius, const bool& ero
268269 }
269270 }
270271 }
271-
272+
272273 int dx, dy, i;
273274 const int jump = image->channels ;
274275 uint16_t * outPixelData = new uint16_t [ysize*xsize*jump];
@@ -296,11 +297,11 @@ void morphological_op(x3f_area16_t *image, const int& in_radius, const bool& ero
296297 }
297298 }
298299 }
299-
300+
300301 // memcpy(image->data, outPixelData, ysize*xsize*jump*sizeof(uint16_t));
301302 delete [] outPixelData;
302303 delete [] maxes;
303-
304+
304305 for (y = -in_radius; y <= in_radius; y++){
305306 delete [] theMask[y + in_radius];
306307 }
@@ -319,10 +320,10 @@ void denoise_splotchify(x3f_area16_t *image, const int& in_radius)
319320 uint16_t * channel = new uint16_t [channel_size];
320321 uint16_t * chan_ptr = channel;
321322 uint16_t * image_ptr;
322-
323+
323324 int morph_elem = 2 ; // ellipse
324325 int morph_size = in_radius;
325-
326+
326327 cv::Mat element = cv::getStructuringElement ( morph_elem, cv::Size ( 2 *morph_size + 1 , 2 *morph_size+1 ), cv::Point ( morph_size, morph_size ) );
327328 for (i = 0 ; i < image->channels ; i++){
328329 // first, get the data, one channel at a time.
@@ -332,16 +333,16 @@ void denoise_splotchify(x3f_area16_t *image, const int& in_radius)
332333 *chan_ptr = *image_ptr;
333334 }
334335 }
335-
336+
336337 if (i>0 ){// skipping the first channel
337338 cv::Mat img (image->rows , image->columns , CV_16U, // apparently doesn't work on color images
338339 channel, sizeof (uint16_t )*xsize);
339-
340+
340341 cv::UMat out;
341342 cv::morphologyEx ( img, out, 3 , element );
342343 cv::morphologyEx ( out, img, 2 , element );
343-
344-
344+
345+
345346 for (y = 0 ; y < ysize; y++){
346347 for (x = 0 , chan_ptr = &(channel[y*xsize]), image_ptr = &(image->data [y*image->row_stride + i]);
347348 x < xsize; x++, chan_ptr++, image_ptr+=3 ){
@@ -351,5 +352,5 @@ void denoise_splotchify(x3f_area16_t *image, const int& in_radius)
351352 }
352353 }
353354 delete [] channel;
354-
355+
355356}
0 commit comments