Accuracy of Conversion between sRGB and Linear RGB and Displaying only the Luminance component of an image

697 views Asked by At

Using an answer here: What are the practical differences when working with colors in a linear vs. a non-linear RGB space? to convert from sRGB to linear RGB, I have assumed the word intensity referred to in the formula as the red, green and blue components of a color.

My code for conversion is:

public static float getLinear(float color) {
        color /= 255.0;
        float linear;
        if (color <= 0.04045) {
            linear = (float) (color/12.92);
        } else {
            linear = (float) Math.pow((color + 0.055)/1.055, 2.4);
        }
        
        return linear;
    }

What I am trying to achieve is displaying an image in only its Luminance component (Y). The code for doing so is:

public static void saveAsY(BufferedImage img, String fname) {
        int w = Image.getWidth(img);
        int h = Image.getHeight(img);
        
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                float r = (float) (Image.getLinear(Image.getRed(Image.getRGB(img, j, i)))*0.299);
                float g = (float) (Image.getLinear(Image.getGreen(Image.getRGB(img, j, i)))*0.587);
                float b = (float) (Image.getLinear(Image.getBlue(Image.getRGB(img, j, i)))*0.114);
                
                double[] yuv = Image.getYUV(r, g, b);
                img.setRGB(j, i, Image.computeRGB(r, g, b));
            }
        }
        
        try {
            ImageIO.write(img, "jpg", new File(fname));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(0);
        }
    }

At this present moment I am unsure if the calculations I am using are indeed correct for only trying to display the luminance component an image. I do not get the desired output as it has been achieved in the research paper: https://wakespace.lib.wfu.edu/bitstream/handle/10339/14775/buchanan_stem04.pdf?sequence=1 on page 22 when using the same image of the tiger. My output appears very dark in comparison.

Any help with the matter of checking whether my calculations are correct are much appreciated. Also, if there is a better way of displaying only the Y, U or V components of an image in sRGB color space how would I go about it?

0

There are 0 answers