From CDS 130

Jump to: navigation, search


  1. Objective
  2. Motivation
  3. Priming questions
  4. Notes
    1. Motivation I
    2. Question
    3. Answer
    4. The colormap
    5. Colormap definition
    6. Example
    7. The image function
    8. The colormap function
    9. A more simple palatte
    10. How to change the colormap
    11. The colorbar function
    12. Question
  5. Questions
    1. Representation of Colors in Software
    2. Image manipulation
    3. Unique colors

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. Priming questions

  • What is RGB? What is HSL? What is CMYK? What is alpha?

4. Notes

4.1. Motivation I

  • Where is Waldo?
  • Can you write a computer program to help find him?
  • Recall that computer programs only understand numbers.


4.2. Question

Suppose that you have a table of decimal numbers. How would you represent these numbers as colors?

4.3. Answer

  • Spreadsheet approach: [1]
  • For each numeric value, assign a color (for future reference: each color is associated with three numbers, R, G, and B).
  • One can also assign colors for ranges (e.g., blue = 0 to 0.999).


4.4. 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 Octave commands
M = rand(100); % Create a 100x100 matrix with random values
imagesc(M);    % Plot random values as colors

4.5. Colormap definition

A colormap maps (translates) a decimal number (or a range of numbers) to a color

Bob's colormap:

  • 0.0 = Red
  • 0.1 = Green
  • 0.2 = Blue
  • 0.3 = Gray
  • 0.4 = Black

4.6. Example

Use Bob's colormap to represent the matrix M as an image.

Bob's colormap:

  • 0.0 = Red
  • 0.1 = Green
  • 0.2 = Blue
  • 0.3 = Gray
  • 0.4 = Black
octave:1> M = [0.1, 0.3, 0.4, 0.1 ; 0.3, 0.2, 0.3, 0.0];
octave:2> M = [ M, M ; M, M ; M , M];
octave:3> M
M =

  0.10000   0.30000   0.40000   0.10000   0.10000   0.30000   0.40000   0.10000
  0.30000   0.20000   0.30000   0.00000   0.30000   0.20000   0.30000   0.00000
  0.10000   0.30000   0.40000   0.10000   0.10000   0.30000   0.40000   0.10000
  0.30000   0.20000   0.30000   0.00000   0.30000   0.20000   0.30000   0.00000
  0.10000   0.30000   0.40000   0.10000   0.10000   0.30000   0.40000   0.10000
  0.30000   0.20000   0.30000   0.00000   0.30000   0.20000   0.30000   0.00000

4.7. The image function

To display a matrix M as an image, use image(M)


These are not Bob's colors. How do I tell Octave to use Bob's colormap?


4.8. The colormap function

By default, Octave uses its own color palatte. Imagine each color on this palatte is associated with a number.

From on August 17 2017 05:06:29.

4.9. A more simple palatte

From on January 30 2012 17:45:11.

4.10. How to change the colormap

To change the color palatte, we need to tell Octave what colors are allowed. Here is a table that specifies the desired colors:

Red Green Blue
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

To make this as a matrix in Octave, 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]


4.11. The colorbar function

The colorbar function tells Octave to show a color scale that a viewer can use to figure out what number is associated with each color.



4.12. Question

How did Octave 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


Answer: It assumed that you wanted five colors to be associated with the range of numbers in M.


5. Questions

This homework will be turned in electronically. (The motivation is for you to gain some experience with alternative forms of electronic communication.) On the due date at the beginning of class, I will circulate a piece of paper where you will write down your wiki username (so that we can find your assignment).

5.1. Representation of Colors in Software

(2 points)

Find a software program that is used to manipulate images (other than Inkscape or MATLAB/Octave).

  • How does the program's documentation describe colors? How do any of the things mentioned in the documentation relate to anything covered in the Images lecture?
  • Create a screenshot like any of those shown in the slides for the Inkscape program. See Screenshots for instructions on how to do this.
  • Upload your screenshots to your wiki page in a section called Homework 7. In this section also type in your sentences. See User:Weigel and click the "view source" link Image:viewsource.png (near the top of the page) for an example. I will ask students at random to present their page in class and answer questions about what they found.

5.2. Image manipulation

(6 points)

  • Create a 10x10 matrix that contains any number from 1 through 10 at least once. The numbers may be in any order. Create the matrix using a double for loop. Suggestion: write your program as a script using edit hw7.m. To execute your program, type hw7 (without the .m!) on the command line.
  • Create a colormap matrix such that each number is mapped to a unique color.
  • Use the image command to plot your image.
  • Use the colorbar command to show a colorbar.
  • Use a nested (double) for loop and an if statement to operate on your 10x10 matrix so that in the image the squares having the color associated with the number 1 are given the color associated with the number 10.
  • Describe (in words) how you could change the colormap matrix to achieve the same result.
  • Save your image by typing
print -dpng yourwikiname_hw7.png
  • To find out where your image was saved, type pwd on the command line.
  • Post your images and Octave code that was required to create your image onto your wiki page. To see the syntax, see and choose "view source". (Hint: Put spaces before your Octave commands to make them show up with a box around it).

5.3. Unique colors

(2 points)

In the Inkscape screenshot for RGB, there is a slider that allows the user to pick an integer between 0 and 255 for each of these three colors. The composite color is represented by these three integers (which could be used for an instruction such as "mix 25 drops of red with 45 drops of green and 200 drops of blue"). Said another way, each color is represented by a combination of three integers.

Suppose the slider only allowed the user to pick an integer between 0 and 1 for each of these three colors. How many unique colors could be represented?

Write your answer along with an explanation of your answer on your wiki page.

Retrieved from ""
Personal tools