Search code examples
colorsrgblinear-gradientsrgbasrgb

Color Intensity using Average RGB values


I am working on a project where I need to calculate a color gradient on various paper strips. I have used RGB color detector app (available on Google PlayStore) to obtain RGB values. Now to plot it I simple average RGB values : (R+G+B)/3 and get a single values as intensity. Is it okay to use it or should I use other methods to obtain a single value for intensity.

The color gradient I get is ranging from dark yellow color to white. Discoloration of paper occurs with increasing concentration of chemical used. [Chemical cannot be disclosed, sorry]


Solution

  • (R+G+B)/3 BAD, SPECTRAL WEIGHTING GOOD

    (R+G+B)/3 is incorrect math for nearly every use. It happens to be used as part of the "HSI" control for setting colors, but that control is far from perceptual uniformity.

    The correct math is fairly straight forward — but as you haven't clearly defined what you need or are trying to do, it is hard to guess the best approach for you.

    I will make some assumptions. It sounds like you are scanning litmus strips of some sort, and are trying to put the data into some useable form in terms of how dark they become from a particular chemical concentration.

    It also sounds like you are looking for some absolute values, as opposed to the difference from strip A and strip B. Color difference math is a bit different from some more absolute measure.

    However, for research, color difference between a strip and a protected master reference sample is probably a better practice, especially as you are not using a real calibrated spectrophotometer in a calibrated viewing/test environment.

    CAPTURE

    The quality of your data capture will have a lot to do with the data usability, and the quality of your capture room lighting and camera or meter is a big part of this.

    ROOM COLOR Ideally that means a room painted a flat or matte neutral grey with around LRV around 20 such as this Glidden:

    GREY CHIP

    CONSISTENT LIGHTING Then you want stray light blocked from the room, or at least the measuring area. NO NATURAL LIGHT!! Ideally you'll have a calibrated light source, but at least have something consistent with a high CRI.

    CONSISTENT SAMPLE ILLUMINATION The light needs to be the same distance from the sample surface, and the ambient light conditions must be controlled so that the samples are always receiving the same about of light.

    CONSISTENT MEASUREMENT & DEVICE If you are using a phone to take the images, you are probably at the mercy of the auto-exposure. If the Google app you are using takes over and provides consistent exposure, that's important. Otherwise, you'll do better getting a Nikon dSLR, and using it in manual exposure mode (always!) and always set to teh same expoure.

    CAPTURE THE SAMPLE AND THE REFERENCE AT THE SAME TIME in other words, have a reference sample that is ideally somewhere around the LRV of your average sample. An 18% greycard is probably great for this, if for no other reason than they are inexpensive and easy to replace with decent consistency.

    If colors are involved then you want an XRite colorChecker chart.

    enter image description here5)

    WHEN YOU CAPTURE SAMPLES If you are taking photos, then you want the reference sample (i.e. grey card or colorchecker chart) in the image frame in the same location and same lighting every single time.

    CONVERSION

    If you are doing this via photography, then you want to be as consistent as possible, same exposure, and use the sRGB profile in the camera (do not use Adobe98, as it is a larger gamut and therefore LESS ACCURATE with higher delta E errors.)

    bring the images into an image app and sample the colors. set the color picker to sample an average of an area around 10px X 10px (100 pixels total), again using the sRGB profile.

    sRGB color data is gamma encoded — by you need it either in a linear luminance (Y) or possibly a perceptual space like CIELAB.

    IF...

    If you just want the absolute measurement of the lightness of each sample, then you can convert the sRGB data to luminance (the Y in CIEXYZ) and then use that figure for comparisons. Note however that luminance is linear relative to light, so it is not perceptually uniform.

    For perceptual uniformity, you'd want CIELAB, and while in CIELAB you can do accurate calculations fo the difference between two colors.

    CONVERSION STEPS AND MATH (in this order)

    1) Convert

    Convert hex or integer color values to float with 0 for black and 1.0 for white.

    2) Linearize

    Linearize the sRGB color data (that is, remove the gamma curve)

    3) Apply spectral weighting

    Apply the spectral weighting coefficients to each channel, and then sum them together for Y (luminance).

    I describe these three steps in detail in this post.

    4) Convert to L* a* b*

    If you want to use a high quality color difference equation, then you need to convert from CIEXYZ to CIELAB.

    5) Use CIEDE2000 for Difference

    Once in LAB space, then use the CIEDE2000 equations for color difference.

    The math for steps 4 and 5 is available here as code snippets.