How do I display an HSB image I converted from RGB using Color.RGBtoHSB?

70 views Asked by At

I have a 450x390 image in RGB colour space. using Color.RGBtoHSB() I have extracted the HSB (Hue, Saturation, and Brightness) values of each pixel. How do I display this HSB image?

-I tried storing the values into a 3d array of (3x450x390). I do not know how to convert it into a displayable image.

-I have also tried converting the HSB values using (int)(hsbVals[i]*255) and then using img.setRGB to replace the original pixels with the new HSB pixels and then displaying it. the image came out with a heavy blue tint instead of a grayscale-like image one would typically expect.

-I expected to use raster to do the job like raster.setsamples(). But due to my unfamiliarity with java raster and the lack of online documentations I am having trouble with it.


import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;

import javax.swing.JLabel;
public class example {  
    public static BufferedImage readImage(String url) {
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File(url));
        } catch (IOException e) {
        }
       return img;
    }
    public static void showImage(Image img, String label) throws IOException {
        ImageIcon icon=new ImageIcon(img);
        JFrame frame=new JFrame(label);
        frame.setLayout(new FlowLayout());
        int width = img.getWidth(null); 
        int height = img.getHeight(null);
        frame.setSize(width,height);
        JLabel lbl=new JLabel();
        lbl.setIcon(icon);
        frame.add(lbl);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) throws IOException {

        BufferedImage img = readImage("src/test_001.jpg");
        showImage(img, "RGB image");
        for(int i=0;i<450;i++) {
            for(int j=0;j<390;j++) {
                Color rgbColor = new Color(img.getRGB(i, j));
                int r = rgbColor.getRed();
                int g = rgbColor.getGreen();
                int b = rgbColor.getBlue();
                float [] hsbVals = new float[3];
                Color.RGBtoHSB(r, g, b, hsbVals);
            }
        }
        showImage(img, "HSB image");        
    }
}

0

There are 0 answers