Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views7 pages

Prog 2 RGBTo HSVColor Model

Uploaded by

Boza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Prog 2 RGBTo HSVColor Model

Uploaded by

Boza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CSC 112 Fundamentals of Computer Science

Fall 2003
Pauca/Burg

Lab #2
Conversion for RGB to HSV Color Model

1. Goals:
• To learn some more basic Linux commands.
• To understand the basic difference between RGB and HSV color representation in
digital images.
• To be able to use basic loops and conditional statements in a C++ program.
• To be able to do simple file I/O in a C++ program.
• To be able to use a few functions for formatted output.
• To be able to write a C++ function and pass parameters correctly.
• To be able to use command-line arguments.

2. Background: Color Representation in Digital Images


2.1. RGB Color Model
In kindergarten, you learned that the three primary colors are red, yellow, and
blue. You were able to take your red, yellow, and blue crayons and mix them to create
nearly any other color. These are the primary colors in a subtractive color model. The
model is subtractive in that, roughly speaking, the more color you put in, the more light
you subtract out, so that the combination of the three primaries gives you black.
For computer monitors, colors are represented by points of light. Computers are
therefore based on an additive color system, where the more colored light you add in, the
more light you have. In the RGB color system, the three primary colors are red, green,
and blue, and the combination of the three yields white. This is not the only possible
combination of primary colors, but these work well both for the engineering of computer
monitors and for the human visual system. Computer monitors can be designed so that
each pixel’s color results from combined beams of red, green, and blue light. This model
is appropriate to the physiology of the human eye, which has receptors for the three
components and combines them into one color.
In the RGB color mode, each of the three components is represented by a word (a
sequence of bits) of a fixed size. The standard for RGB is 8 bits (i.e., 1 byte) per
component, requiring 8 * 3 = 24 bits (i.e., 3 bytes) for each pixel in the bitmapped image
file. This bit depth of 24 bits per sample yields 224 colors. That’s 16,777,216 colors. The
down side is that 3 bytes per pixel can create large image files if the resolution is high.
RGB color is one of the most frequently used color modes in photographic
processing programs. When you make color specifications or adjustments in such a
program, you do so either by choosing from a visual palette where you see the actual
color, or by specifying a number for each of the RGB components. Because the bit depth
of each of these components is 8 bits, there are 28 = 256 shades of red, green, and blue,
respectively. Thus, you can choose a number between 0 and 255 for each of the three
components.
The color space for the RGB color model is easy to depict graphically. Let r, g,
and b represent the 3 axes in 3-dimensional space, where r is what we usually call the x-
axis, g the y-axis, and b the z-axis. We will normalize the relative amounts of red, green,
and blue in a color so that each value varies between 0 and 1. This color space is shown
in Figure 2.5. The origin (0,0,0) of the RGB color cube corresponds to black. White is
the value (1,1,1). The corners of the cube correspond to red, green, blue, and their
complementary colors – cyan, magenta, and yellow, respectively. Other colors are
created at values between 0 and 1 for each of the components. For example, (1, .65, .15)
is light orange, and (.26, .37, .96) is a shade of blue. Shades of gray have equal
proportions of red, green, and blue and lie along the line between (0,0,0) and (1,1,1).
Notice that if you decrease each of the values for light orange in (1, .65, .15) but keep
them in the same proportion to each other, you are in effect decreasing the brightness of
the color, which is like adding in more black. The color moves from a light orange to a
muddy brown.
You may want to note that in mathematical depictions of the RGB color model, it
is convenient to allow the three color components to range between 0 and 1. However,
the corresponding RGB color mode in photographic processing programs is more likely
to have values ranging between 0 and 255, since each of the three components is captured
in 8 bits. What is important is the relative amounts of each component, and how large
these amounts are with respect to the maximum possible values. For example, the light
orange described as (1, .65, .15) above would become (255, 166, 38) in an RGB mode
with maximum values of 255.

Blue (0,0,1) Cyan (0,1,1)

Magenta (1,0,1)

White (1,1,1)

Black (0,0,0) Green (0,1,0)

Red (1,0,0) Yellow (1,1,0)

Figure 2.5 RGB Color Space

2.2. HSV Color Model


Although RGB corresponds well to the technology of computer monitors and the
physiology of the human eye, it is not necessarily the most natural way to think about
color. Imagine that you have a certain color in mind and you don’t have a visual palette
from which to select it. How would you determine the relative amounts of red, green,
and blue that you need in order to create your color? It’s not intuitively clear how much
of each you need.
An alternative to RGB is to specify a color by its hue, saturation, and brightness.
Hue is the basic color. Saturation is the intensity of the color – which can be thought of
as how much white is added in. A completely saturated red is a “fire engine” red, but as
it becomes less saturated it becomes more and more pink until eventually is becomes
white. Brightness is essentially how much light is in the color. The less bright a color is,
the more it moves toward black. The initials HSV refer to this kind of color model. HSV
stands for “hue, saturation, and value.” (Value is essentially brightness.)
Geometrically, the HSB color space is a distortion of the RGB space into a kind
of 3-dimensional diamond called a hexacone. Picture turning the RGB cube around and
tilting it so that you are looking straight into the origin (black) with the remaining corners
visible – two on the top, two on the bottom, one on the left, and one on the right, as
shown in Figure 2.6a. Imagine this as a flat, 2-dimensional hexagon where the center
black point is connected by a line to each vertex, as shown in Figure 2.6b. All the colors
are at the outside vertices of this shape. Now imagine expanding this into 3 dimensions
again by pulling down on the center point. You have created the HSV color space, as
shown in Figure 2.6c.
To see how this shape captures the HSV color model, draw an imaginary circle
that touches all the vertices of the hexacone’s base. The hue is represented by a position
on this circle given in degrees, from 0 to 360, with red conventionally set at 0. As the
hue values increase, you move counterclockwise through yellow, green, cyan, etc. The
brightness axis lies from the black point of the hexacone through the center of the circle,
with lightness values ranging from 0 to 1. Saturation is a function of the color’s radial
distance from the lightness axis. For example, (358°, .88, .93) is a bright yellow.
RGB is the most frequently-offered color mode in photographic processing
programs. However, even in programs that store color data in the RGB mode, it is
sometimes possible to view the color information in the HSV or other modes. In such
programs, you can click a button in a display window and switch from one color mode
view to another. This is offered as a convenience to the user because one color model
may be easier to work with than another for some people or in some situations.

Green Yellow

Cyan Red
White

Blue Magenta

Figure 2.6a Color Cube From Top


Green Yellow

Cyan Red
White

Blue Magenta

Figure 2.6b Color Cube Collapsed to 2D

Green Yellow

White
Cyan Red

Blue Magenta

Black

Figure 2.6c Color Cube Stretched to Hexacone

3. The Programming Assignment: RGB to HSV Conversion


Your assignment is to write a program that converts RGB color to HSV format.
Your program should be written to the following specifications:
• The name of the file containing the RGB pixel data is given as a command-line
argument. The main function opens this file for reading.
• The main function reads in a list of RGB colors. Each RGB color consists of
three integers in the range of [0…255]. The three integers are on one line,
separated by spaces. The main function keeps reading until it reaches the end of
the file. (Don’t “hard code” in a loop of a fixed length.)
• Before running your program, you need to create a text file that contains some
sample RGB values. Some values that you can use are given below, in the
Program Input section.
• For each of the RGB values read in, the main function calls an RGB_to_HSV
function to do the conversion. This function takes input and gives output as
described in the algorithm below. The RGB inputs should be value parameters,
and the HSV outputs should be reference parameters.
• RGB_to_HSV calls minimum and maximum functions, as shown in the
pseudocode below. Functions minimum and maximum both return floating point
values.
• Upon return from the function call to RGB_to_HSV, main prints out the original
RGB values and the corresponding HSV values. The s and v values should be
printed out with no decimal point (rounded to integer values). Use print
formatting functions for this (e.g. setprecision).
• Have comments in your program explaining
o What the program does overall
o How the program is run (what input it requires and output it gives)
o What each function does
o What any particularly abstruse blocks of code do
o Anything else that is needed help someone understand your code
• The header comment of your program should look like this
/*******************************************************************/
/*M. Name */
/*CSC112 – Fall 2002 */
/*Date */
/*Program 2 – RGB to HSV Conversion */
/*Input: */
/*Output: */
/*Program Description:… */
/*******************************************************************/

4. Algorithm to Convert RGB to HSV


The algorithm for converting RGB to HSV values is given below. Turn this into a
function that is called by main.
algorithm RGB_to_HSV
/* Input: r_init, g_init, and b_init, each integers in the range [0…255]
Output: h, a real number in the range of [0…360], except if s = 0, in which
case h is undefined
s and v, real numbers in the range of [0…1] (a percentage)
{
r = r_init/255
g = g_init/255
b = b_init/255
max = maximum(r,g,b)
min = minimum(r,g,b)
v = max
if max ≠ 0 then s = (max – min)/max
else s = 0
if s == 0 then h = undefined
else {
diff = max − min
if r == max then h = (g − b) / diff
else if g == max then h = 2 + (b − r) / diff
else if b == max then h = 4 + (r − g) / diff
h = h * 60
if h < 0 then h = h + 360
}
}

5. Program Input
To test your program, create an input file that has, say, 15 RGB values in it. It
should like something like this:

255 125 56
23 144 200
151 92 35
33 126 131

and so forth. On each line, choose any three numbers you want, as long as they are all
between 0 and 255.
You can verify that your HSV answers are correct by looking at these numbers in
the color picker window of Photoshop (on your Windows side of your computer). For
any color, both the RGB and HSV values are given.
6. Learning Some More Unix/Linux
Listed below are the Unix/Linux features we’ll cover in the lab. Jot your own
notes in the space provided.

alias

cat

more

Piping one instruction to another with |

Redirecting program input and output with < and >

script

lp

Environment variables

env

Transferring files from the Linux partition to your Windows partition

You might also like