我在java中尝试从RGB
到HSI
的图片,然后我想显示三张图片:
H色调图像,S - 饱和度图像,I-强度图像
private void jButton_Convert_HSIActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
BufferedImage image = null;
BufferedImage processed_hue, processed_sat, processed_in;
try {
image = ImageIO.read(new File("C:\\Users\\ATH\\Desktop\\photo\\Rotate\\test_rotate_lena.png"));
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
int width = image.getWidth();
int height = image.getHeight();
processed_hue = new BufferedImage(width, height, image.getType());
processed_sat = new BufferedImage(width, height, image.getType());
processed_in = new BufferedImage(width, height, image.getType());
for (int y = 0; y < width; y++) //thuc hien lap cho y chay tu o den chieu cao anh
{
for (int x = 0; x < height; x++) //thuc hien lap cho x chay tu 0 den chieu rong
{
Color c = new Color(image.getRGB(x, y));
float h = 0;
float r, g, b, s, i, theta;
// RGB TO HSI CONVERSION
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();
float total = red + green + blue;
r = red / total;
g = green / total;
b = blue / total;
System.out.println("normalize red " + r);
System.out.println(" red = " + red + " green = " + green + " blue = " + blue);
s = 1 - (3 * Math.min(r, Math.min(g, b)));
i = total / (3 * 255);
theta = (float) Math.acos((0.5 * ((r - g) + (r - b))) / Math.pow((((r - g) * (r - g)) + ((r - b) * (g - b))), 1 / 2));
System.out.println(" theta " + theta);
if (b <= g) {
// h = ((1 / Math.cos((0.5 * (2 * r - g - b)) / (Math.sqrt(((r - g) * (r - g) + (r - b) * (g - b)))))));
h = theta;
} else {
h = (float) ((2 * Math.PI) - theta);
}
System.out.println("Intial hue = " + h + " saturation = " + s + " intensity = " + i);
float h1 = (float) (h * (180 / Math.PI)); //to change Radian to Degree - multiply 180/pi
float s1 = (s * 100);
float i1 = (i * 255);
System.out.println(" Hue = " + Math.round(h1) + " Saturation = " + Math.round(s1) + " Intensity = " + Math.round(i1));
// hue
float HSV[] = new float[3];
// Color.RGBtoHSB(red, green, blue, HSV);
processed_hue.setRGB(x, y, Color.getHSBColor(h1, HSV[1], HSV[2]).getRGB());
// processed_sat.setRGB(x, y, Color.getHSBColor(HSV[0], s1, HSV[2]).getRGB());
// processed_in.setRGB(x, y, Color.getHSBColor(HSV[0], HSV[1], i1).getRGB());
}
} // end for loop
try {
// save images
ImageIO.write(processed_hue, "png", new File("C:\\Users\\ATH\\Desktop\\photo\\HSI\\hue_image.png"));
ImageIO.write(processed_sat, "png", new File("C:\\Users\\ATH\\Desktop\\photo\\HSI\\sat_image.png"));
ImageIO.write(processed_in, "png", new File("C:\\Users\\ATH\\Desktop\\photo\\HSI\\in_image.png"));
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
System.out.println("Image is saved in D drive: ");
/*
try {
File sat_out1 = new File("C:\\Users\\ATH\\Desktop\\photo\\HSI\\sat_image.png");
ImageIO.write(sat_image, "png", sat_out1);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
System.out.println("Image is saved in D drive: ");
try {
File inten_out1 = new File("C:\\Users\\ATH\\Desktop\\photo\\HSI\\inten_image.png");
ImageIO.write(inten_image, "png", inten_out1);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
System.out.println("Image is saved in D drive: ");
*/
}
请建议我并帮助我。如何获得H图像,S图像和I图像?