# Uniform light and color -- implementation and application of histogram matching algorithm

1, Histogram matching (histogram specification) algorithm: it takes an image histogram as a template to approximate the histogram of the image to be processed with the template histogram.

Algorithm flow:

1. Calculate the cumulative histogram of the template image and the image to be processed respectively (count the distribution proportion of the pixel value on 0 ~ 255);
2. Calculate the difference between each value on the image histogram to be used and each value of the template image histogram at one time, and generate 256 difference tables;
3. Calculate the minimum value in each difference table, take the minimum value index as the transformed value of the corresponding image value to be processed in the difference table, and generate a look-up table;
4. The image to be processed is processed through the look-up table.

Implementation steps:

1. Calculate cumulative histogram:

Calculate histogram

```Map<Integer, Double> map = new HashMap<>();
int width = image.getWidth();
int height = image.getHeight();
double totalPixel = width * height;
double rate = 1 / (3 * totalPixel);
for (int i = 0; i < 256; i++) {
map.put(i, 0.0);// Through the loop, fill the set with 0 ~ 255 positions, and the initial value is 0
}
//Count the total number of 0 ~ 255 distribution on the image respectively
for (int i = 0; i < image.getWidth(); i++) {
for (int j = 0; j < image.getHeight(); j++) {
int rgb = image.getRGB(i, j);
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = rgb & 0xff;
map.put(r, map.get(r) + rate);
map.put(g, map.get(g) + rate);
map.put(b, map.get(b) + rate);
}
}```

Calculate cumulative histogram

```for(int i = 0; i < 256;i++) {
tempSum += tempMap.get(i);
imageSum += imageMap.get(i);
tempList[i] = tempSum;
imageList[i] = imageSum;
}```

2. Calculation difference table

```List<Double[]> differenceList = new ArrayList<>();
for(int i = 0; i < 256;i++) {
Double[] differenceTable = new Double;
for(int j = 0; j< 256;j++) {
differenceTable[j] = Math.abs(imageList[i] - tempList[j]);
}
}```

3. Calculate the minimum value of the difference table and generate a lookup table

```for(int i = 0; i < 256;i++) {
Double[] differenceTable = differenceList.get(i);
Double[] sortTable = new Double;
for(int j = 0; j < 256;j++) {
sortTable[j] = differenceTable[j];
}
Arrays.sort(sortTable);
double mini = sortTable;
Double[] table = differenceList.get(i);
for(int j = 0; j < 256; j++) {
if (mini == table[j]) {
resultTable[i] = j;
}
}
}```

4. Process the image through the look-up table

```for(int i = 0; i < width; i++) {
for(int j = 0 ;j < height;j++) {
int rgb = image.getRGB(i, j);
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = rgb & 0xff;
r = rTable[r];
g = gTable[g];
b = bTable[b];
rgb = (r & 0xff) << 16 | (g & 0xff) << 8 | b & 0xff;
resultImage.setRGB(i, j, rgb);
}
}
return resultImage;```

2, The histogram matching algorithm is used to homogenize the light and color of the image. Two images with different hues are selected and processed by histogram matching algorithm.

Template image: Image to be processed: After treatment: Conclusion: the hue of the template image before processing is quite different from that of the image to be processed, and the overall hue of the processed image is very close to that of the template image.

Tags: image processing

Posted by zimick on Fri, 13 May 2022 21:30:34 +0300