0

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?

it2901
  • 69
  • 8
  • 1
    I don't get it. You computer `yuv` but then you don't use it. Your `r`, `g` and `b` values also don't make sense. Based on what you have, you can calculate Y = r + g + b, but you can't calculate U/V without further coefficients. Y is supposed to be only one plane, but you do `img.setRGB(...)`. In order to achieve a grey image with 3 planes you have to set the same value for all channels, i.e., RGB = YYY. Is `Image` a custom class you've written? Because it doesn't seem to be [this](https://docs.oracle.com/javase/7/docs/api/java/awt/Image.html). Can you share the code of its relevant methods? – Reti43 Dec 16 '20 at 00:46
  • This question is not at all clear. What are you asking and what are you actually trying to do? If all you want to do is find luminance from sRGB, your linearization looks correct, but nothing else. The sRGB/Rec709 coefficients are 0.2126, 0.7152, 0.0722, and after you apply them to the linearized r,g,b you need to sum them to Y... if you want to display on an sRGB monitor, then Y needs to have the sRGB trc re-applied, and that sent to all three channels. If you are trying to get LUV??? Try http://brucelindbloom.com – Myndex Jan 31 '21 at 23:46

0 Answers0