computational and biological vision - bguben-shahar/teaching/computational-vision/... · lighting a...

15
Computational and Biological Vision Student Project – Fall 2012 “Colors Out Of Space” Digital color representation, color spaces and more! By: Amir Eluk Software Engineering

Upload: others

Post on 30-Sep-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computational and Biological Vision - BGUben-shahar/Teaching/Computational-Vision/... · Lighting a small red LED on black TV screen adds red to the previously empty spectrum of wavelengths

Computational and Biological Vision

Student Project – Fall 2012

“Colors Out Of Space”

Digital color representation, color spaces and more!

By:

Amir Eluk

Software Engineering

Page 2: Computational and Biological Vision - BGUben-shahar/Teaching/Computational-Vision/... · Lighting a small red LED on black TV screen adds red to the previously empty spectrum of wavelengths

Introduction

Color can be represented by numerous ways. What are those different ways and what

makes them different from each other? Why do we need so many color spaces and which

one should be used? How color is represented and interpreted with our eyes? TV screens?

Printed media? Photographs?

Motivation

The main goal of this paper is to introduce the basic concepts of color representation, and to

find interesting correlation between them, As well as pointing out the advantages and

disadvantages of using different color models.

I will start by explaining some basic concepts

Color Model

A mathematical model describing the way colors can be represented. Color models usually

use a base of 3 or 4.

Example: The RBG color model uses 3 parameters to describe color, each representing Red,

Blue and Green parts of the color.

Color Space

A color model with a mapping function (to an already defined color space) results in a new

color space based on the color model. sRGB is a color space based on RGB color model.

For the rest of the paper I’ll use the terms color space and color mode interchangeably.

Gamut

Most color spaces cannot represent the full range of colors visible to the human eye. The

subset of colors which can be accurately represented in a given color space is called the

color gamut.

The entire range of possible chromaticity is given by the CIE 1931 chromaticity diagram

sRGB. The corners of the triangle are the primary colors for this gamut;

Page 3: Computational and Biological Vision - BGUben-shahar/Teaching/Computational-Vision/... · Lighting a small red LED on black TV screen adds red to the previously empty spectrum of wavelengths

Color Blending Modes

Color can be mixed using additive or subtractive blending.

Additive color blending is where the visible spectrum of light is added to the current color.

Lighting a small red LED on black TV screen adds red to the previously empty spectrum of

wavelengths the TV emits. Lighting a green LED right next to it will emits a mixed spectrum

of light that will be perceived by our brain as yellow.

Subtractive color blending is where various wavelengths are removed from the color-

filtered out. A white page reflects (virtually) all wavelengths. Cyan ink absorbs red light and

reflect cyan, so applying cyan ink to a white page subtract red from the page and leaves it

cyanic. Further subtracting green by adding magenta ink will appear purple or blue.

Perceptually Uniform Color Space

A color space is called perceptually uniform if the Euclidian distance between colors is

proportional to the perceived difference between the colors (see HSV).

Additive color mixing Subtractive color mixing

Page 4: Computational and Biological Vision - BGUben-shahar/Teaching/Computational-Vision/... · Lighting a small red LED on black TV screen adds red to the previously empty spectrum of wavelengths

Color Spaces Examples

CMYK

CMYK is a subtractive color model, used mainly in printing and press; the acronym

stands for cyan, magenta, yellow, and key (black).

Black ink is used where pure black is needed since combination of the tree primary

colors yields a 'muddy brown' color instead of black. Black ink is also considerably

cheaper than the chromatic ones.

Some techniques are used to allow less than full saturation of colors and wider color

blends, but this is beyond the scope of this paper.

RGB

RGB is an additive color model based on red blue and green as its primary colors, trying to

approach the three types of cones in the retina. The RGB color model is based on the

Young–Helmholtz theory of trichromatic color vision, developed by Thomas Young and

Hermann Helmholtz.

Most consumer electronic devices work in RGB color mode including CRT,LCD and Plasma

displays, video projectors and electronic cameras. This is partly because creating these lights

is easier and mostly due to modern conventions and back compatibility.

sRGB

The most commonly used color space based on RGB model today is sRGB. It was created by

HP and Microsoft in 1996 for use on monitors, printers, and the Internet.

The gamut of chromaticity that can be represented in sRGB is the color triangle defined by

these primaries:

Color gamut of sRGB

Page 5: Computational and Biological Vision - BGUben-shahar/Teaching/Computational-Vision/... · Lighting a small red LED on black TV screen adds red to the previously empty spectrum of wavelengths

As with any RGB color space, for non-negative values of R, G, and B it is not possible to

represent colors outside this triangle, which is well inside the range of colors visible to a

human as well as the color range of CMYK. However sRGB space is considered adequate for

most consumer applications.

Adobe RGB Wide-gamut RGB and ProPhoto RGB Color Spaces

In 1998 adobe created Adobe RGB to bridge the gap between the workable palette of

computer to the printable color range of CMYK printers.

Later the Wide-gamut RGB was developed to support even wider range of colors.

ProPhoto RGB is Kodak's approach to the same concept

Color gamut of Adobe RBG

Adobe wide-gamut RGB

Kodak ProPhoto RGB

The problem was only partially solved since most (consumer) computer screens are unable

to display the full gamut of color that can be represented using these color spaces.

HSV and HSL

Stands for Hue Saturation and Value/Lightness. HSV and HSL introduce a more intuitive way

(in opposed to RGB) for picking colors. If for example you want a dark blue color you may

easily set the ‘hue’ amount to a bluish value and the ‘value’ as low as you want the color to

be dark. Try to pick the same color using red green and blue sliders and you’ll find it much

harder and unintuitive.

The difference between value and lightness is that value at its max is a pure color with no

black in it while lightness at its max is pure white. IF uou have two colors both were chosen

with S=V=100% (HSV color space). You now need to select a second color for each that is

brighter. You cannot directly use HSV because both are already at full value. How do you

intuitively increase brightness?

Page 6: Computational and Biological Vision - BGUben-shahar/Teaching/Computational-Vision/... · Lighting a small red LED on black TV screen adds red to the previously empty spectrum of wavelengths

Both HSV and HSL are NOT Perceptually Uniform, as can be seen in the example:

A shift from 80 to 120 will result in lesser perceived change than shifting from 240 to 280 even though the shift is over the save distance.

Color space cone illustration:

HSV Cone HSL Cone

L.a.b

Lab is a color-opponent space with L for lightness (or luminosity), and a\b for the color-

opponent dimensions. a is for position between magenta and green ( )

and b is for position between yellow and blue ( ).

Unlike the RGB and CMYK color models, Lab color is designed to approximate human vision.

Unlike HSV and HSL- it is perceptually uniform (along the a and b dimensions), and its L

component closely matches human perception of lightness.

Lab is used to make accurate color balance in images by modifying the a and b channels, or

to adjust the lightness contrast using the L channel.

Page 7: Computational and Biological Vision - BGUben-shahar/Teaching/Computational-Vision/... · Lighting a small red LED on black TV screen adds red to the previously empty spectrum of wavelengths

Comparison of RGB and L.a.b

For example, let’s take a look at the following image represented using RBG and L.a.b color

space:

Original Image

Represented in RBG

R G B

Represented in L.a.b

a channel b channel L.a.b L channel

You can see how intuitive the Lab color space is. You can imagine superimposing the three

pictures on top of each other to get the original image. This is almost impossible with RGB.

Page 8: Computational and Biological Vision - BGUben-shahar/Teaching/Computational-Vision/... · Lighting a small red LED on black TV screen adds red to the previously empty spectrum of wavelengths

If so, how RGB histogram differs from VHL and Lab histogram? Can we find correlation

between the different dimensions of color spaces?

Inspecting a single image

Original Image followed by histograms of RGB, Lab and HSV

R G B

a b L

H S V

One thing to notice is the difference between the L and V channels. While the L channel

describes most pixels to have medium lightness with a peak at around 220 the V (value)

channel reveals a peak at 255. This is due to the fact that in HSV mode V at its maximum is

Page 9: Computational and Biological Vision - BGUben-shahar/Teaching/Computational-Vision/... · Lighting a small red LED on black TV screen adds red to the previously empty spectrum of wavelengths

not white color but rather ‘full’ color. White is achieved where V is at max and S =0 (as

opposed to HSL where l=255 is white color).

Also, it is seems like L in Lab is proportional to the combination of R, G and B. This makes

sense since increasing all three dimensions in RGB yields brighter color.

With that said we could expect that if the only way to create brightness in RGB (w\o altering

the tone) is to increase all three that the values will be exactly equal to the lightness of Lab.

Experimenting reveals slightly different results:

Average of R, G and Blue Lightness (of Lab)

The histograms are indeed very similar but not identical. The difference might come out of

the way brightness is perceived. Take a look at the RGB colors, while all are pure colors they

do not appear with the same brightness- pure green appears brighter than pure blue. On the

other hand, the lightness curve for the Lab profile is not linear with the RGB average level- I

assume here that the green channel affects lightness more than the blue or red channels.

I took the numerical values of both channels and calculated the absolute difference matrix

(Diff = |L - B|). I then calculated the mean of the given matrix Diff.

Mean(Diff) = 19.1057

Let’s call this number the mean absolute difference and use it as a measure of similarity

between two matrices. The lower this number is the more the two channels are similar (in

the context of the global value of pixels- with no correlation to their position within the

image)

Some more calculations of the mean absolute difference:

Between L and G Between L and B Between avg(RGB) and L

18.7357 60.3849 19.1057

As can be seen the Lightness channel of Lab is much more similar to the green channel than

the blue channel.

Page 10: Computational and Biological Vision - BGUben-shahar/Teaching/Computational-Vision/... · Lighting a small red LED on black TV screen adds red to the previously empty spectrum of wavelengths

Is it just because there are more green pixels in the image than blue ones? I will try to

confirm the previous hypothesis by running the same experiment on a bank of real images.

Another thing I would like to test is the correlation between Lab lightness and HSV value.

Remember that the histograms for the example previously showed most of the V pixel to be

maxed at 255 while the L pixels were normally distributed along the middle value (128). This

led to the assumption that V should closely relate to two times L- So I’ll add a column for

checking this assumption as well (mean value of the V / 2).

All 136 images are approx. 360x240 pixels and are real photographs, so they should closely

resemble scenes our own eyes can capture naturally.

I tried to collect all different kind of pictures, from light to dark, with different dominating

colors and different lighting conditions.

I’m showing here a sample of the resulting table for the sake of simplicity but the

experiment was run for all 136 images (full table can be found at the end of the paper). All

values are the absolute mean value of difference between the two channels in the header

(RGB is the average of R,G and B):

Image# L and G L and B L and RGB V/2 and L V/2 and RGB

1 16.7 44.7 10.3 129.0 122.4

2 11.4 43.3 14.7 157.8 143.2

3 7.8 32.9 12.9 121.0 108.4

4 12.5 16.7 8.8 142.9 134.2

5 6.4 16.7 6.2 179.9 174.6

6 8.4 22.4 6.7 144.6 138.6

7 4.6 38.5 13.7 114.8 102.0

8 9.3 38.6 13.3 135.8 122.8

The experiment shows a tight correlation between the intensity of the green channel in RGB

and the lightness of the same image. This is shown as a low value for the difference between

Lightness and Green in opposed to a higher value for Blue. This value is also quite similar to

the lightness\RGB similarity.

This means: Green channel ≈ Lightness channel ≈ average of R,G and B. This is quite

surprising when you think about it.

Some images showed greater correlation than others:

L and G: This value is the mean difference between lightness and green. The image that

showed the highest similarity (2.1- where lower is better) is #13. This image also holds the

smallest ratio of L and G / L and B , meaning not just that green is very similar to L but blue is

very not-similar altogether.

Page 11: Computational and Biological Vision - BGUben-shahar/Teaching/Computational-Vision/... · Lighting a small red LED on black TV screen adds red to the previously empty spectrum of wavelengths

Original Green Blue Lightness RGB

L and RGB: This value represents the similarity between the RGB brightness and L. The

image with most similarity (2.5) is #103, with surprising similarity in the B and G channels as

well.

Original Green Blue Lightness RGB

The image with the least similarity (in the context of RGB brightness) is #87 with a score of

27.5- still not that far.

Original Green Blue Lightness RGB

Image #126 showed exactly the same similarity between (L and G) and (L and RGB) (8.4),

both three times lower than (L and B) (24.6).

Original Green Blue Lightness RGB

V and L:

My assumption on the V value came out to be so far from the truth I initially thought to omit

it entirely. The experiments have shown little to no correlation at all between the value

channel of HSV and Lightness channel of Lab. It is important to remember though, that I

defined similarity by the mean value of pixels in the image- I believe that a different method

for comparing images would show completely different results. This will wait for the next

time though.

Page 12: Computational and Biological Vision - BGUben-shahar/Teaching/Computational-Vision/... · Lighting a small red LED on black TV screen adds red to the previously empty spectrum of wavelengths

Conclusions:

This paper is following the beginning of a journey into the world of color. I have reviewed

most of the basic concepts of computer color representation, color spaces, and more. I have

also reviewed some example of commonly used color spaces asserting the advantages and

disadvantages of using each one of them.

I also performed an experiment to assert my assumption about the correlation between

different channels of Lab, HSV and RGB and came out with some surprising results:

The brightness of an RGB image is composed of its three channels with most weight

given to the Green channel, Red comes second followed by blue.

This uneven contribution to the brightness is the reason why simple average of RGB

values is not the exact brightness of the image.

There is definitely a correlation between the ‘value’ and ‘brightness’ though it is not

what I expected it to be.

References:

http://mkweb.bcgsc.ca/color_summarizer/?faq#purpose

http://mkweb.bcgsc.ca/brewer/

http://www.broadhurst-family.co.uk/lefteye/MainPages/Lab.htm

Wikipedia (Color, Color Opponent, Color Space, RGB color model, additive color

model, Lab color space, and more…)

http://www.mathworks.com/help (for MATLAB support)

Appendix 1: MATLAB code:

calc_mad.m

clc;cla;clear; total = 136; tbl = zeros(total,6); C = makecform('srgb2lab'); for i=1:total i if i<10; zeroz = '0000'; elseif i<100; zeroz = '000'; else zeroz = '00'; end filename = strcat('imageBank\Image',zeroz, num2str(i),'.jpg'); I = imread(filename);

Irgb=double(I); Ired = Irgb(:,:,1); Igreen = Irgb(:,:,2); Iblue = Irgb(:,:,3); IrgbAvg = (Ired+Igreen+Iblue)./3;

I_lab = applycform(I,C); I_lab = double(I_lab ); IL = I_lab(:,:,1);

hsv_image = rgb2hsv(I); Iv = hsv_image(:,:,3);

Page 13: Computational and Biological Vision - BGUben-shahar/Teaching/Computational-Vision/... · Lighting a small red LED on black TV screen adds red to the previously empty spectrum of wavelengths

tbl(i,1) = i; [tbl(i,2)] = mad(Igreen,IL); [tbl(i,3)] = mad(Iblue,IL); [tbl(i,4)] = mad(IrgbAvg,IL); [tbl(i,5)] = mad(Iv./2,IL); [tbl(i,6)] = mad(Iv./2,IrgbAvg);

end

showHistograms.m

(Assuming I holds a color image)

%% Show RGB Irgb=double(I); Ired = Irgb(:,:,1); Igreen = Irgb(:,:,2); Iblue = Irgb(:,:,3);

IrgbAvg = (Ired+Igreen+Iblue)./3;

figure (10); figure('Renderer','OpenGL'); opengl software; title('sRGB Flowers'); set(gca,'xdir','reverse');

subplot(2,2,1); imshow(uint8(Irgb)); title('Original Image'); axis off;

subplot(2,2,2); hist(Ired(:),256); h = findobj(gca,'Type','patch'); set(h,'FaceColor','r','EdgeColor','r');

title('Red');

subplot(2,2,3); hist(Igreen(:),256); h = findobj(gca,'Type','patch'); set(h,'FaceColor','g','EdgeColor','g'); title('Green');

subplot(2,2,4); hist(Iblue(:),256); h = findobj(gca,'Type','patch'); set(h,'FaceColor','b','EdgeColor','b'); title('Blue');

%% Show Lab C = makecform('srgb2lab'); I_lab = applycform(I,C); I_lab = double(I_lab ); IL = I_lab(:,:,1); Ia = I_lab(:,:,2); Ib = I_lab(:,:,3);

figure (11); title('L.a.b Flowers'); set(gca,'xdir','reverse');

Page 14: Computational and Biological Vision - BGUben-shahar/Teaching/Computational-Vision/... · Lighting a small red LED on black TV screen adds red to the previously empty spectrum of wavelengths

subplot(2,2,1); imshow((I)); title('Original Image'); axis off;

subplot(2,2,2); hist(double(IL(:)),256); h = findobj(gca,'Type','patch'); title('L');

subplot(2,2,3); hist(double(Ia(:)),256); h = findobj(gca,'Type','patch'); title('a');

subplot(2,2,4); hist(double(Ib(:)),256); h = findobj(gca,'Type','patch'); title('b');

%% Show HSV hsv_image = rgb2hsv(I); Ih = hsv_image(:,:,1); Is = hsv_image(:,:,2); Iv = hsv_image(:,:,3);

figure (12); set(gca,'xdir','reverse');

subplot(2,2,1); hist((IrgbAvg(:)),256); title('Original Image'); axis off;

subplot(2,2,2); hist(double(Ih(:).*256),256); h = findobj(gca,'Type','patch'); title('H');

subplot(2,2,3); hist(double(Is(:).*256),256); h = findobj(gca,'Type','patch'); title('S');

subplot(2,2,4); hist(double(Iv(:).*256),256); h = findobj(gca,'Type','patch'); title('V');

mad.m

function [mad] = mad(a,b) absDiff = abs(a-b); mad = mean(absDiff(:));

Page 15: Computational and Biological Vision - BGUben-shahar/Teaching/Computational-Vision/... · Lighting a small red LED on black TV screen adds red to the previously empty spectrum of wavelengths

Appendix 2: Experiment Result Table:

Image L and G L and B L and RGB Image L and G L and B L and RGB 2 16.6 44.4 10.2 72 7.5 12.3 6.8 3 11.4 43.3 14.7 73 0.4 0.9 0.5 4 7.8 32.9 12.9 74 11.4 13.1 6.7 5 12.4 16.8 8.9 75 6.1 45.9 17.4 6 6.7 15.3 6.1 76 12.7 39.7 12.4 7 8.5 21.8 6.7 77 15.8 51.1 14.6 8 4.6 37.9 13.6 78 13.6 77.6 27.3 9 9.2 38.3 13.2 79 10.5 36.3 12.9

10 13.0 32.3 9.7 80 4.5 38.8 18.2 11 5.5 8.8 4.0 81 9.2 22.3 9.1 12 7.0 16.5 8.0 82 11.1 29.0 11.1 13 2.1 47.3 20.4 83 7.0 31.5 13.3 14 7.9 16.6 9.8 84 8.7 43.6 16.7 15 6.6 34.0 8.3 85 7.3 9.7 5.7 16 9.2 26.6 5.6 86 4.7 4.7 4.7 17 13.9 18.3 5.3 87 63.3 96.9 27.5 18 24.2 42.2 11.4 88 43.7 85.9 24.2 19 15.4 38.9 10.1 89 7.3 25.4 8.6 20 5.4 28.8 11.5 90 6.2 18.3 7.3 21 13.1 58.6 16.5 91 18.9 46.4 14.4 22 9.8 36.8 12.6 92 8.1 17.4 6.2 23 5.2 19.5 10.8 93 4.0 32.1 3.8 24 11.1 29.3 11.7 94 9.9 33.7 10.2 25 9.7 47.3 15.4 95 5.7 7.0 5.2 26 5.7 11.2 4.6 96 14.9 27.1 7.8 27 8.7 34.9 10.8 97 11.1 32.1 10.6 28 44.0 27.9 10.2 98 4.9 23.4 3.4 29 7.0 32.6 10.6 99 2.7 16.1 7.3 30 4.2 10.4 4.8 100 15.0 27.3 8.2 31 63.7 66.7 21.4 101 5.9 15.2 8.2 32 17.4 56.2 15.6 102 16.4 19.8 4.6 33 18.3 20.3 7.0 103 4.8 6.9 2.5 34 9.7 39.9 13.7 104 4.1 30.6 13.7 35 32.6 61.5 17.6 105 11.0 69.2 22.5 36 23.2 22.8 6.9 106 31.8 46.4 15.7 37 41.9 91.0 25.3 107 17.5 36.5 12.4 38 12.0 46.8 15.9 108 17.0 70.9 20.2 39 12.0 30.6 11.4 109 10.1 26.9 10.6 40 19.4 25.1 6.5 110 10.9 49.5 17.6 41 6.0 42.3 18.5 111 16.3 49.8 14.9 42 2.6 44.9 18.8 112 15.7 40.3 13.0 43 13.3 43.3 12.6 113 9.6 23.9 7.4 44 1.4 1.5 1.4 114 11.0 23.4 8.1 45 8.5 22.1 5.8 115 13.4 10.1 5.6 46 12.5 48.5 15.9 116 18.1 43.5 14.7 47 11.5 55.9 17.8 117 5.2 13.7 7.9 48 5.8 11.5 5.6 118 3.6 26.2 11.2 49 4.2 4.6 3.5 119 5.4 11.9 9.1 50 3.8 53.7 22.8 120 6.4 52.2 19.4 51 12.9 39.6 23.0 121 12.7 39.0 10.9 52 11.5 15.2 6.9 122 4.2 15.6 5.6 53 25.6 67.5 18.5 123 9.6 18.8 7.0 54 6.8 20.5 8.4 124 10.5 49.8 16.4 55 9.5 34.5 14.1 125 11.0 18.2 6.1 56 13.1 38.6 11.3 126 8.4 34.6 8.4 57 6.9 55.7 3.1 127 16.6 22.9 9.0 58 10.6 51.8 16.3 128 3.5 29.2 4.4 59 10.2 27.3 10.5 129 10.9 44.3 14.0 60 9.1 20.0 14.6 130 8.1 33.7 14.2 61 10.3 39.2 6.7 131 5.9 4.1 8.2 62 15.4 37.1 10.6 132 12.4 11.7 4.1 63 6.7 21.3 7.8 133 7.4 23.1 10.1 64 26.6 34.8 8.3 134 2.7 7.3 3.0 65 8.7 24.5 9.7 135 4.8 29.8 13.7 66 4.0 7.0 3.6 136 9.0 56.1 16.8 67 3.6 8.6 2.8 68 10.3 14.9 7.4 69 10.6 43.0 14.5 70 5.0 16.4 11.1 71 11.9 26.9 8.7