I have not seen this problem anywhere else so I am coming here for help. I am making a simple program to alter the contrast of a black and white image and have nearly everything done. However I have an issu with the getRGB
method. For the first half of the image (roughly) it returns normal values around 150, 150, 150 or whatever the image has. Then for a reason that I cannot explain it returns 0, 0, 0 for a while and then 255, 255, 255 with a couple of the 0's mixed in. Does anyone know why this is happening ?
private void getRGB(BufferedImage image, int x, int y){
//get pixel rgb from image
int temp=image.getRGB(x,y);
//set the value to color and then get each component
Color color=new Color(temp);
rgb[0]=color.getRed();
rgb[1]=color.getGreen();
rgb[2]=color.getBlue();
//debug file
data.println(color.getRed()+"\t"+color.getGreen()+"\t"+color.getBlue());
}
Here is a snippet of the file data.txt. There are more zeros than I have shown here.
189 189 189
184 184 184
178 178 178
181 181 181
171 172 171
196 196 195
182 182 182
175 175 175
186 186 186
181 181 181
192 192 191
183 183 183
184 184 184
186 186 186
188 188 188
183 183 183
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
255 255 255
255 255 255
255 255 255
255 255 255
255 255 255
255 255 255
255 255 255
Here is my function to calculate the new grey level of each pixel. As far as I am aware this is functioning properly.
private void equalize(int x, int y, int max, int min){
//This calls getRGB then calculates the old and new grey levels for a pixel
//It then will assign the new grey level to the pixel
getRGB(image1, x, y);
int oldGrey = ((rgb[0]+rgb[1]+rgb[2])/3);
int newGrey = (int)Math.round(
(((cdf[oldGrey]*1.0)-minCdfValue)/
((x*y)-minCdfValue))*
(newMax-newMin)+newMin);
if(oldGrey != 0){
for(int i=0; i<3; i++){
rgb[i] *= ((newGrey*1.0)/(oldGrey*1.0));
}
}else{
rgb[0]=rgb[1]=rgb[2]=newGrey;
}
setRGB(image1, x, y);
}
If there is any information that I have left out please let me know.
Edit: CDF is Cumulative Distribution Function which is a method for histogram equalization. I believe there is an article on Wikipedia about it. As for using RGB, it is just a requirement. The values of each are all the same. As for 0 and 255, yes they are legal arguments, however, I have edited the image so that it's histogram is very narrow with values from about 80 to 210. There are no true black(0) or true white (255) pixels at all in the image.