47 SourceImage.convertTo(tempIm1, CV_32FC1, 1.0, 0.0);
48 cv::divide(tempIm1, NormImage, tempIm1, 1.0);
51 tempIm1.convertTo(DestImage, CV_8UC1);
57 if (image_list.empty())
62 const cv::Mat& rep_im = image_list.front();
64 cv::Mat image_sum(cv::Size(rep_im.cols, rep_im.rows), CV_64F);
66 for (uint32_t i = 0; i < image_list.size(); i++)
69 image_list[i].convertTo(temp_im, CV_64F, 1.0, 0.0);
71 image_sum = image_sum + temp_im;
74 image_sum = image_sum /
static_cast<double>(image_list.size());
77 image_sum.convertTo(mean_image, CV_8U);
78 cv::Mat temp_norm_image;
80 cv::medianBlur(mean_image, temp_norm_image, 45);
82 cv::Mat temp_norm_image2;
83 temp_norm_image.convertTo(temp_norm_image2, CV_32F);
85 for (int32_t i = 0; i < temp_norm_image2.rows; i++)
87 for (int32_t j = 0; j < temp_norm_image2.cols; j++)
89 if (temp_norm_image2.at<
float>(i, j) > max1)
91 max1 = temp_norm_image2.at<
float>(i, j);
96 temp_norm_image2 = temp_norm_image2 * (255.0 / max1);
98 cv::Mat temp_norm_image3;
99 temp_norm_image2.convertTo(temp_norm_image3, CV_8U, 1.0, 0.0);
102 cv::GaussianBlur(temp_norm_image3,
119 cv::Size(kernel_size, kernel_size),
122 cv::Mat local_mask_sums;
127 cv::Size(kernel_size, kernel_size),
131 cv::divide(local_sums, local_mask_sums, blurred, 1, mat.type());
132 blurred.copyTo(mat, local_mask_sums != 0);
137 const cv::Mat& source_image,
143 double cell_width =
static_cast<double>(source_image.cols) / grid_size;
144 double cell_height =
static_cast<double>(source_image.rows) / grid_size;
146 cv::Mat max_vals(grid_size, grid_size, CV_32F);
147 cv::Mat min_vals(grid_size, grid_size, CV_32F);
148 cv::Mat grid_mask = cv::Mat::ones(grid_size, grid_size, CV_8U) * 255;
150 bool has_mask = !mask.empty();
151 for(
int i = 0; i < grid_size; i++)
153 for(
int j = 0; j < grid_size; j++)
158 int x = j * cell_width;
159 int y = i * cell_height;
160 int x2 = (j + 1) * cell_width;
161 int y2 = (i + 1) * cell_height;
165 cv::Rect roi = cv::Rect(x, y, w, h);
166 roi.width = std::min(source_image.cols - roi.x, roi.width);
167 roi.height = std::min(source_image.rows - roi.y, roi.height);
171 if (cv::countNonZero(mask(roi)) > 0)
173 cv::minMaxLoc(source_image(roi), &minVal, &maxVal, 0, 0, mask(roi));
177 grid_mask.at<uint8_t>(i, j) = 0;
182 cv::minMaxLoc(source_image(roi), &minVal, &maxVal, 0, 0);
185 max_vals.at<
float>(i, j) = maxVal;
186 min_vals.at<
float>(i, j) = minVal;
194 for(
int i = 0; i < source_image.rows; i++)
196 int cell_y = std::min(grid_size - 1, static_cast<int>(i / cell_height));
197 int cell_y_start = cell_y * cell_height;
198 int cell_y_end = std::min(source_image.rows - 1, static_cast<int>((cell_y + 1) * cell_height));
199 double py = (i - cell_y_start) / static_cast<double>(cell_y_end - cell_y_start);
201 for(
int j = 0; j < source_image.cols; j++)
204 int cell_x = std::min(grid_size - 1, static_cast<int>(j / cell_width));
205 int cell_x_start = cell_x * cell_width;
206 int cell_x_end = std::min(source_image.cols - 1, static_cast<int>((cell_x + 1) * cell_width));
207 double px = (j - cell_x_start) / static_cast<double>(cell_x_end - cell_x_start);
209 int cell_x1 = cell_x;
210 int cell_x2 = cell_x;
213 if (px < 0.5 && cell_x > 0)
215 cell_x1 = cell_x - 1;
220 else if (px > 0.5 && cell_x + 1 < grid_size)
223 cell_x2 = cell_x + 1;
228 int cell_y1 = cell_y;
229 int cell_y2 = cell_y;
232 if (py < 0.5 && cell_y > 0)
234 cell_y1 = cell_y - 1;
239 else if (py > 0.5 && cell_y + 1 < grid_size)
242 cell_y2 = cell_y + 1;
248 double min_x1_y1 = min_vals.at<
float>(cell_y1, cell_x1);
249 double max_x1_y1 = max_vals.at<
float>(cell_y1, cell_x1);
251 double min_x2_y1 = min_vals.at<
float>(cell_y1, cell_x2);
252 double max_x2_y1 = max_vals.at<
float>(cell_y1, cell_x2);
254 double min_x1_y2 = min_vals.at<
float>(cell_y2, cell_x1);
255 double max_x1_y2 = max_vals.at<
float>(cell_y2, cell_x1);
257 double min_x2_y2 = min_vals.at<
float>(cell_y2, cell_x2);
258 double max_x2_y2 = max_vals.at<
float>(cell_y2, cell_x2);
261 double xM1 = max_x1_y1 * px1 + max_x2_y1 * px2;
262 double xM2 = max_x1_y2 * px1 + max_x2_y2 * px2;
263 double M = xM1 * py1 + xM2 * py2;
265 double xm1 = min_x1_y1 * px1 + min_x2_y1 * px2;
266 double xm2 = min_x1_y2 * px1 + min_x2_y2 * px2;
267 double m = xm1 * py1 + xm2 * py2;
271 if(maxVal > 255) maxVal = 255;
272 if(minVal < 0) minVal = 0;
275 maxVal = std::max(maxVal, min_max);
276 minVal = std::min(minVal, max_min);
278 double val = source_image.at<uint8_t>(i,j);
279 val = 255.0 * (val - minVal) / (maxVal - minVal);
280 if(val > 255) val = 255;
282 dest_image.at<uint8_t>(i,j) = val;
286 dest_image.setTo(0, mask == 0);
298 dst.create(src.size(), CV_8U);
302 int x,
y, wsz2 = winsize / 2;
303 int* vsum =
reinterpret_cast<int*
>(cv::alignPtr(buf + (wsz2 + 1) *
sizeof(vsum[0]), 32));
304 int scale_g = winsize * winsize / 8, scale_s = (1024 + scale_g) / (scale_g * 2);
305 const int OFS = 256 * 5, TABSZ = OFS * 2 + 256;
307 const uchar* sptr = src.ptr();
308 int srcstep = src.step;
309 cv::Size size = src.size();
313 for (x = 0; x < TABSZ; x++)
315 tab[x] = (uchar)(x - OFS < -ftzero ? 0 : x - OFS > ftzero ? ftzero * 2 : x - OFS + ftzero);
318 for (x = 0; x < size.width; x++)
320 vsum[x] = (ushort)(sptr[x] * (wsz2 + 2));
323 for (y = 1; y < wsz2; y++)
325 for (x = 0; x < size.width; x++)
327 vsum[x] = (ushort)(vsum[x] + sptr[srcstep*y + x]);
331 for (y = 0; y < size.height; y++)
333 const uchar* top = sptr + srcstep * MAX(y - wsz2 - 1, 0);
334 const uchar* bottom = sptr + srcstep * MIN(y + wsz2, size.height - 1);
335 const uchar* prev = sptr + srcstep * MAX(y - 1, 0);
336 const uchar* curr = sptr + srcstep * y;
337 const uchar* next = sptr + srcstep * MIN(y + 1, size.height - 1);
338 uchar* dptr = dst.ptr<uchar>(y);
340 for (x = 0; x < size.width; x++)
342 vsum[x] = (ushort)(vsum[x] + bottom[x] - top[x]);
345 for (x = 0; x <= wsz2; x++)
347 vsum[-x-1] = vsum[0];
348 vsum[size.width + x] = vsum[size.width - 1];
351 int sum = vsum[0] * (wsz2 + 1);
352 for( x = 1; x <= wsz2; x++ )
357 int val = ((curr[0] * 5 + curr[1] + prev[0] + next[0]) * scale_g - sum * scale_s) >> 10;
358 dptr[0] = tab[val + OFS];
360 for( x = 1; x < size.width-1; x++ )
362 sum += vsum[x + wsz2] - vsum[x - wsz2 - 1];
363 val = ((curr[x] * 4 + curr[x - 1] + curr[x + 1] + prev[x] + next[x]) * scale_g - sum * scale_s) >> 10;
364 dptr[x] = tab[val + OFS];
367 sum += vsum[x+wsz2] - vsum[x - wsz2 - 1];
368 val = ((curr[x] * 5 + curr[x - 1] + prev[x] + next[x]) * scale_g - sum * scale_s) >> 10;
369 dptr[x] = tab[val + OFS];
375 if (image.type() == CV_8UC1)
377 cv::Mat Image8Bit(image.rows, image.cols, CV_8U), Image8BitColor;
380 image.convertTo(ImageFloat, CV_32F, 1, 0);
382 cv::minMaxLoc(ImageFloat.reshape(1,1),
NULL, &maxVal);
384 ImageFloat = ImageFloat*((1 << 8)/pow(2, ceil(log(maxVal)/log(2))));
385 ImageFloat.convertTo(Image8Bit,CV_8U, 1, 0);
391 if (image.type() == CV_8UC3)
393 cv::Mat Image8Bit =
scale_2_8bit(image), Image8BitColor;
394 cvtColor(Image8Bit, Image8BitColor, cv::COLOR_GRAY2BGR);
395 return Image8BitColor;
TFSIMD_FORCE_INLINE const tfScalar & y() const
void NormalizeResponse(const cv::Mat &src, cv::Mat &dst, int winsize, int ftzero, uchar *buf)
cv::Mat generate_normalization_image(const std::vector< cv::Mat > &image_list)
Computes a best estimate of a normalization image from a vector of images.
CvImagePtr cvtColor(const CvImageConstPtr &source, const std::string &encoding)
void MaskedBoxFilter(cv::Mat &mat, cv::Mat &mask, int32_t kernel_size)
TFSIMD_FORCE_INLINE const tfScalar & x() const
cv::Mat scale_2_8bit_color(const cv::Mat &image)
Convert the input Mat to 8 bit color.
TFSIMD_FORCE_INLINE const tfScalar & w() const
void normalize_illumination(cv::Mat NormImage, cv::Mat SourceImage, cv::Mat &DestImage)
cv::Mat scale_2_8bit(const cv::Mat &image)
Convert the input Mat to 8 bit.
void ContrastStretch(int32_t grid_size, const cv::Mat &source_image, cv::Mat &dest_image, const cv::Mat &mask=cv::Mat(), double max_min=0.0, double min_max=0.0)