-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHistogramEQ.java
More file actions
170 lines (125 loc) · 5.17 KB
/
HistogramEQ.java
File metadata and controls
170 lines (125 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
/**
* Image histogram equalization
*
* Author: Bostjan Cigan (http://zerocool.is-a-geek.net)
*
*/
public class HistogramEQ {
private static BufferedImage original, equalized;
public static BufferedImage computeHistogramEQ(BufferedImage im){
return histogramEqualization(im);
}
private static void writeImage(String output) throws IOException {
File file = new File(output+".jpg");
ImageIO.write(equalized, "jpg", file);
}
private static BufferedImage histogramEqualization(BufferedImage original) {
int red;
int green;
int blue;
int alpha;
int newPixel = 0;
// Get the Lookup table for histogram equalization
ArrayList<int[]> histLUT = histogramEqualizationLUT(original);
BufferedImage histogramEQ = new BufferedImage(original.getWidth(), original.getHeight(), original.getType());
for(int i=0; i<original.getWidth(); i++) {
for(int j=0; j<original.getHeight(); j++) {
// Get pixels by R, G, B
alpha = new Color(original.getRGB (i, j)).getAlpha();
red = new Color(original.getRGB (i, j)).getRed();
green = new Color(original.getRGB (i, j)).getGreen();
blue = new Color(original.getRGB (i, j)).getBlue();
// Set new pixel values using the histogram lookup table
red = histLUT.get(0)[red];
green = histLUT.get(1)[green];
blue = histLUT.get(2)[blue];
// Return back to original format
newPixel = colorToRGB(alpha, red, green, blue);
// Write pixels into image
histogramEQ.setRGB(i, j, newPixel);
}
}
return histogramEQ;
}
// Get the histogram equalization lookup table for separate R, G, B channels
private static ArrayList<int[]> histogramEqualizationLUT(BufferedImage input) {
// Get an image histogram - calculated values by R, G, B channels
ArrayList<int[]> imageHist = imageHistogram(input);
// Create the lookup table
ArrayList<int[]> imageLUT = new ArrayList<int[]>();
// Fill the lookup table
int[] rhistogram = new int[256];
int[] ghistogram = new int[256];
int[] bhistogram = new int[256];
for(int i=0; i<rhistogram.length; i++) rhistogram[i] = 0;
for(int i=0; i<ghistogram.length; i++) ghistogram[i] = 0;
for(int i=0; i<bhistogram.length; i++) bhistogram[i] = 0;
long sumr = 0;
long sumg = 0;
long sumb = 0;
// Calculate the scale factor
float scale_factor = (float) (255.0 / (input.getWidth() * input.getHeight()));
for(int i=0; i<rhistogram.length; i++) {
sumr += imageHist.get(0)[i];
int valr = (int) (sumr * scale_factor);
if(valr > 255) {
rhistogram[i] = 255;
}
else rhistogram[i] = valr;
sumg += imageHist.get(1)[i];
int valg = (int) (sumg * scale_factor);
if(valg > 255) {
ghistogram[i] = 255;
}
else ghistogram[i] = valg;
sumb += imageHist.get(2)[i];
int valb = (int) (sumb * scale_factor);
if(valb > 255) {
bhistogram[i] = 255;
}
else bhistogram[i] = valb;
}
imageLUT.add(rhistogram);
imageLUT.add(ghistogram);
imageLUT.add(bhistogram);
return imageLUT;
}
// Return an ArrayList containing histogram values for separate R, G, B channels
public static ArrayList<int[]> imageHistogram(BufferedImage input) {
int[] rhistogram = new int[256];
int[] ghistogram = new int[256];
int[] bhistogram = new int[256];
for(int i=0; i<rhistogram.length; i++) rhistogram[i] = 0;
for(int i=0; i<ghistogram.length; i++) ghistogram[i] = 0;
for(int i=0; i<bhistogram.length; i++) bhistogram[i] = 0;
for(int i=0; i<input.getWidth(); i++) {
for(int j=0; j<input.getHeight(); j++) {
int red = new Color(input.getRGB (i, j)).getRed();
int green = new Color(input.getRGB (i, j)).getGreen();
int blue = new Color(input.getRGB (i, j)).getBlue();
// Increase the values of colors
rhistogram[red]++; ghistogram[green]++; bhistogram[blue]++;
}
}
ArrayList<int[]> hist = new ArrayList<int[]>();
hist.add(rhistogram);
hist.add(ghistogram);
hist.add(bhistogram);
return hist;
}
// Convert R, G, B, Alpha to standard 8 bit
private static int colorToRGB(int alpha, int red, int green, int blue) {
int newPixel = 0;
newPixel += alpha; newPixel = newPixel << 8;
newPixel += red; newPixel = newPixel << 8;
newPixel += green; newPixel = newPixel << 8;
newPixel += blue;
return newPixel;
}
}