sheng/Images
From CDS 130
1. Objective
 To introduce key concepts about images related to computing.
 To introduce the connection of these concepts to programming concepts.
2. Motivation
 Computing often requires the display of numbers with images
 Computing is often done on images to find interesting features
3. Prequestions
 How colors are displayed on your computer monitor?
 What is RGB? What is HSL? What is CMYK? What is alpha?
4. Slides
4.1. Introduction
 Where is Waldo?
 Use a computer program to help find him?
 But computer programs only understand numbers.
 How to count the number of red blood cells?
4.2. Ways to describe colors: RGB
There are many ways to describe colors (color basis):
 RGB (red, green, blue)
4.3. Ways to describe colors: CMYK
4.4. Ways to describe colors: HSL
4.5. Ways to describe colors: Color wheel
4.6. Ways to describe mixtures
4.7. Numeric representations
A color in the RGB color model is described by indicating how much of each of the red, green, and blue is included. The color is expressed as an RGB triplet (r,g,b), each component of which can vary from zero to a defined maximum value.
These ranges may be quantified in several different ways:
 From 0 to 1, with any fractional value in between. This representation is used in theoretical analyses, and in systems that use floatingpoint representations.
 Each color component value can also be written as a percentage, from 0% to 100%.
 In computing, the component values are often stored as integer numbers in the range 0 to 255, the range that a single 8bit byte can offer (by encoding 256 distinct values).
 Highend digital image equipment can deal with the integer range 0 to 65,535 for each primary color, by employing 16 bits instead of 8bit bytes.
For example, the full intensity red [_] is written in the different RGB notations as:
Notation  RGB triplet 

Arithmetic  (1.0, 0.0, 0.0) 
Percentage  (100%, 0%, 0%) 
Digital 8bit per channel  (255, 0, 0) 
Digital 16bit per channel  (65535, 0, 0) 
4.8. Numerical representation 1.
Associate a number between 0.0 and 1.0 for each of red, green, and blue. This is what Octave uses.
4.9. Numerical representation 2.
 This is for people who don't understand fractions, but do understand how to count drops of paint.
 How many unique colors can be created when each of red, green, and blue can be associated with an integer from 0 to 255?
4.10. Relationship Between Representations
Question 1:
 Convert
R=128,G=10,B=12
(Numerical Representation 1.) tor,g,b
values (Numerical Representation 2.)
Answer 

r = 128/255 = 0.502 g = 10/255 = 0.392 b = 12/255 = 0.0471 
Question 2:
 Convert
r=0.1,g=0.1,b=0.1
(Numerical Representation 2.) toR,G,B
values (Numerical Representation 1.)  Why will the
r,g,b
representation never be exactly the same as theR,G,B
representation?
Answer 

R = 0.1*255 = 25.5 G = 0.1*255 = 25.5 B = 0.1*255 = 25.5

4.11. Display a tile of a particular color in Matlab
The fill
command can be used to display a tile of a particular color.
x = [ 0 3 3 0 ]; y = [ 0 0 1 1 ]; v = [0.0 1.0 1.0]; fill(x,y,v)
produces a rectangle with vertices (0,0), (3,0), (3,1), and )0,1) and fills it with the color specified by the rgb vector v (cyan).
The following code displays in a single window a cyan rectangle, a magenta rectangle and a rectangle filled with the interpolated color we desire.
x = [0 3 3 0 ]; y = [0 0 1 1 ]; hold on fill (x, y, [0.0 1.0 1.0]) fill (x, y+1, [0.4 0.6 1.0]) fill (x, y+2, [1.0 0.0 1.0]) axis equal off
4.12. The image
function
To display a matrix M
as an image, use image(M)
M = [0.1, 0.3, 0.4, 0.1 ; 0.3, 0.2, 0.3, 0.0]; M = [ M, M ; M, M ; M , M]; M imagesc(M)
These are not my colors. How do I tell Matlab to use my colormap?
4.13. The colormap
 Most of the time a computer simulation returns a matrix of numbers that we want to represent as an image.
 This image is the result of the Matlab commands
M = rand(100); % Create a 100x100 matrix with random values imagesc(M); % Plot random values as colors
4.14. Colormap definition
A colormap maps (translates) a decimal number (or a range of numbers) to a color
By default, Matlab uses its own color palatte. Imagine each color on this palatte is associated with a number.
4.15. A more simple palatte
4.16. How to change the colormap
To change the color palatte, we need to tell Matlab what colors are allowed. Here is a table that specifies the desired colors:

To make this as a matrix in Matlab, type C = [1.0, 0.0, 0.0 ; 0.0, 1.0, 0.0 ; 0.0, 0.0, 1.0 ; 0.5, 0.5, 0.5 ; 0.0, 0.0, 0.0] colormap(C) 
4.17. The colorbar
function
The colorbar 
4.18. Question
What if I have a different matrix:M = [1.2, 1.3, 0.5; 0.1, 0.2, 0.3; 0.7, 0.8, 0.9]; M = [M, M; M, M; M, M]; imagesc(M)
How did Matlab know that I wanted the following decimal numbers to be associated with these colors? 0.0 = Red 0.1 = Green 0.2 = Blue 0.3 = Gray 0.4 = Black The only commands I entered were image(M) colormap(C) Answer: It assumed that you wanted five colors to be associated with the range of numbers in 
4.19. Indexed Images
The pixels in the image are represented by integers, which are pointers (indices) to color values stored in the colormap.
4.20. Turecolor Images
The three color components for each pixel are stored along the third dimension of the data array. For example, the red, green, and blue color components of the pixel (10,5) are stored in RGB(10,5,1), RGB(10,5,2), and RGB(10,5,3), respectively.
To determine the color of the pixel at (2,3), look at the RGB triplet stored in (2,3,1:3). Suppose (2,3,1) contains the value 0.5176, (2,3,2) contains 0.1608, and (2,3,3) contains 0.0627. The color for the pixel at (2,3) is
0.5176 0.1608 0.0627
Example:
plane_1 = [1 0 1]; plane_2 = [0 0 1]; plane_3 = [0 1 0]; rgb = cat(3, plane_1, plane_2, plane_3); size(rgb) image(rgb) axis image title('Truecolor image with one red, one blue, and one yellow pixel')