84 #include <visp3/core/vpImageConvert.h> 85 #include <visp3/imgproc/vpImgproc.h> 89 int fastRound(
float value) {
return (
int)(value + 0.5f); }
91 void clipHistogram(
const std::vector<int> &hist, std::vector<int> &clippedHist,
int limit)
94 int clippedEntries = 0, clippedEntriesBefore = 0;
95 int histlength = (int)hist.size();
98 clippedEntriesBefore = clippedEntries;
100 for (
int i = 0; i < histlength; i++) {
101 int d = clippedHist[i] - limit;
104 clippedHist[i] = limit;
108 int d = clippedEntries / (histlength);
109 int m = clippedEntries % (histlength);
110 for (
int i = 0; i < histlength; i++) {
115 int s = (histlength - 1) / m;
116 for (
int i = s / 2; i < histlength; i += s) {
120 }
while (clippedEntries != clippedEntriesBefore);
123 void createHistogram(
int blockRadius,
int bins,
int blockXCenter,
int blockYCenter,
126 std::fill(hist.begin(), hist.end(), 0);
128 int xMin = std::max(0, blockXCenter - blockRadius);
129 int yMin = std::max(0, blockYCenter - blockRadius);
130 int xMax = std::min((
int)I.
getWidth(), blockXCenter + blockRadius + 1);
131 int yMax = std::min((
int)I.
getHeight(), blockYCenter + blockRadius + 1);
133 for (
int y = yMin; y < yMax; ++y) {
134 for (
int x = xMin; x < xMax; ++x) {
135 ++hist[fastRound(I[y][x] / 255.0f * bins)];
140 std::vector<float> createTransfer(
const std::vector<int> &hist,
int limit, std::vector<int> &cdfs)
142 clipHistogram(hist, cdfs, limit);
143 int hMin = (int)hist.size() - 1;
145 for (
int i = 0; i < hMin; ++i) {
151 for (
int i = hMin; i < (int)hist.size(); ++i) {
156 int cdfMin = cdfs[hMin];
157 int cdfMax = cdfs[hist.size() - 1];
159 std::vector<float> transfer(hist.size());
160 for (
int i = 0; i < (int)transfer.size(); ++i) {
161 transfer[i] = (cdfs[i] - cdfMin) / (
float)(cdfMax - cdfMin);
167 float transferValue(
int v, std::vector<int> &clippedHist)
169 int clippedHistLength = (int)clippedHist.size();
170 int hMin = clippedHistLength - 1;
171 for (
int i = 0; i < hMin; i++) {
172 if (clippedHist[i] != 0) {
178 for (
int i = hMin; i <= v; i++) {
179 cdf += clippedHist[i];
183 for (
int i = v + 1; i < clippedHistLength; ++i) {
184 cdfMax += clippedHist[i];
187 int cdfMin = clippedHist[hMin];
188 return (cdf - cdfMin) / (float)(cdfMax - cdfMin);
191 float transferValue(
int v,
const std::vector<int> &hist, std::vector<int> &clippedHist,
int limit)
193 clipHistogram(hist, clippedHist, limit);
195 return transferValue(v, clippedHist);
229 float slope,
bool fast)
231 if (blockRadius < 0) {
232 std::cerr <<
"Error: blockRadius < 0!" << std::endl;
236 if (bins < 0 || bins > 256) {
237 std::cerr <<
"Error: (bins < 0 || bins > 256)!" << std::endl;
241 if ((
unsigned int)(2 * blockRadius + 1) > I1.
getWidth() || (
unsigned int)(2 * blockRadius + 1) > I1.
getHeight()) {
242 std::cerr <<
"Error: (unsigned int) (2*blockRadius+1) > I1.getWidth() || " 243 "(unsigned int) (2*blockRadius+1) > I1.getHeight()!" 251 int blockSize = 2 * blockRadius + 1;
252 int limit = (int)(slope * blockSize * blockSize / bins + 0.5);
259 int cm = I1.
getWidth() - nc * blockSize;
265 for (
int i = 0; i < nc; ++i) {
266 cs[i] = i * blockSize + blockRadius + 1;
272 for (
int i = 0; i < nc; ++i) {
273 cs[i] = i * blockSize + blockRadius + 1;
275 cs[nc] = I1.
getWidth() - blockRadius - 1;
280 cs[0] = blockRadius + 1;
281 for (
int i = 0; i < nc; ++i) {
282 cs[i + 1] = i * blockSize + blockRadius + 1 + cm / 2;
284 cs[nc + 1] = I1.
getWidth() - blockRadius - 1;
287 int rm = I1.
getHeight() - nr * blockSize;
292 rs.resize((
size_t)nr);
293 for (
int i = 0; i < nr; ++i) {
294 rs[i] = i * blockSize + blockRadius + 1;
299 rs.resize((
size_t)(nr + 1));
300 for (
int i = 0; i < nr; ++i) {
301 rs[i] = i * blockSize + blockRadius + 1;
303 rs[nr] = I1.
getHeight() - blockRadius - 1;
307 rs.resize((
size_t)(nr + 2));
308 rs[0] = blockRadius + 1;
309 for (
int i = 0; i < nr; ++i) {
310 rs[i + 1] = i * blockSize + blockRadius + 1 + rm / 2;
312 rs[nr + 1] = I1.
getHeight() - blockRadius - 1;
315 std::vector<int> hist((
size_t)(bins + 1));
316 std::vector<int> cdfs((
size_t)(bins + 1));
317 std::vector<float> tl;
318 std::vector<float> tr;
319 std::vector<float> br;
320 std::vector<float> bl;
322 for (
int r = 0; r <= (int)rs.size(); ++r) {
323 int r0 = std::max(0, r - 1);
324 int r1 = std::min((
int)rs.size() - 1, r);
325 int dr = rs[r1] - rs[r0];
327 createHistogram(blockRadius, bins, cs[0], rs[r0], I1, hist);
328 tr = createTransfer(hist, limit, cdfs);
332 createHistogram(blockRadius, bins, cs[0], rs[r1], I1, hist);
333 br = createTransfer(hist, limit, cdfs);
336 int yMin = (r == 0 ? 0 : rs[r0]);
337 int yMax = (r < (int)rs.size() ? rs[r1] : I1.
getHeight());
339 for (
int c = 0; c <= (int)cs.size(); ++c) {
340 int c0 = std::max(0, c - 1);
341 int c1 = std::min((
int)cs.size() - 1, c);
342 int dc = cs[c1] - cs[c0];
348 createHistogram(blockRadius, bins, cs[c1], rs[r0], I1, hist);
349 tr = createTransfer(hist, limit, cdfs);
353 createHistogram(blockRadius, bins, cs[c1], rs[r1], I1, hist);
354 br = createTransfer(hist, limit, cdfs);
358 int xMin = (c == 0 ? 0 : cs[c0]);
359 int xMax = (c < (int)cs.size() ? cs[c1] : I1.
getWidth());
360 for (
int y = yMin; y < yMax; ++y) {
361 float wy = (float)(rs[r1] - y) / dr;
363 for (
int x = xMin; x < xMax; ++x) {
364 float wx = (float)(cs[c1] - x) / dc;
365 int v = fastRound(I1[y][x] / 255.0f * bins);
370 float t0 = 0.0f, t1 = 0.0f;
376 t0 = wx * t00 + (1.0f - wx) * t01;
377 t1 = wx * t10 + (1.0f - wx) * t11;
380 float t = (r0 == r1) ? t0 : wy * t0 + (1.0f - wy) * t1;
381 I2[y][x] = std::max(0, std::min(255, fastRound(t * 255.0f)));
387 std::vector<int> hist(bins + 1), prev_hist(bins + 1);
388 std::vector<int> clippedHist(bins + 1);
392 int xMax0 = std::min((
int)I1.
getWidth(), blockRadius);
394 for (
int y = 0; y < (int)I1.
getHeight(); y++) {
395 int yMin = std::max(0, y - (
int)blockRadius);
396 int yMax = std::min((
int)I1.
getHeight(), y + blockRadius + 1);
400 std::fill(hist.begin(), hist.end(), 0);
402 for (
int yi = yMin; yi < yMax; yi++) {
403 for (
int xi = xMin0; xi < xMax0; xi++) {
404 ++hist[fastRound(I1[yi][xi] / 255.0f * bins)];
411 for (
int yi = yMin; yi < yMax; yi++) {
412 for (
int xi = xMin0; xi < xMax0; xi++) {
413 ++hist[fastRound(I1[yi][xi] / 255.0f * bins)];
420 int yMin1 = yMin - 1;
422 for (
int xi = xMin0; xi < xMax0; xi++) {
423 --hist[fastRound(I1[yMin1][xi] / 255.0f * bins)];
427 if (y + blockRadius < (
int)I1.
getHeight()) {
428 int yMax1 = yMax - 1;
430 for (
int xi = xMin0; xi < xMax0; xi++) {
431 ++hist[fastRound(I1[yMax1][xi] / 255.0f * bins)];
438 for (
int x = 0; x < (int)I1.
getWidth(); x++) {
439 int xMin = std::max(0, x - (
int)blockRadius);
440 int xMax = x + blockRadius + 1;
443 int xMin1 = xMin - 1;
445 for (
int yi = yMin; yi < yMax; yi++) {
446 --hist[fastRound(I1[yi][xMin1] / 255.0f * bins)];
451 int xMax1 = xMax - 1;
453 for (
int yi = yMin; yi < yMax; yi++) {
454 ++hist[fastRound(I1[yi][xMax1] / 255.0f * bins)];
458 int v = fastRound(I1[y][x] / 255.0f * bins);
459 int w = std::min((
int)I1.
getWidth(), xMax) - xMin;
461 int limit = (int)(slope * n / bins + 0.5f);
462 I2[y][x] = fastRound(transferValue(v, hist, clippedHist, limit) * 255.0f);
506 clahe(pR, resR, blockRadius, bins, slope, fast);
507 clahe(pG, resG, blockRadius, bins, slope, fast);
508 clahe(pB, resB, blockRadius, bins, slope, fast);
512 unsigned char *ptrStart = (
unsigned char *)I2.
bitmap;
513 unsigned char *ptrEnd = ptrStart + size * 4;
514 unsigned char *ptrCurrent = ptrStart;
516 unsigned int cpt = 0;
517 while (ptrCurrent != ptrEnd) {
518 *ptrCurrent = resR.
bitmap[cpt];
521 *ptrCurrent = resG.
bitmap[cpt];
524 *ptrCurrent = resB.
bitmap[cpt];
527 *ptrCurrent = pa.bitmap[cpt];
unsigned int getWidth() const
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Type * bitmap
points toward the bitmap
static void split(const vpImage< vpRGBa > &src, vpImage< unsigned char > *pR, vpImage< unsigned char > *pG, vpImage< unsigned char > *pB, vpImage< unsigned char > *pa=NULL)
VISP_EXPORT void clahe(const vpImage< unsigned char > &I1, vpImage< unsigned char > &I2, int blockRadius=150, int bins=256, float slope=3.0f, bool fast=true)
unsigned int getHeight() const