Docsity

Understanding Digital Images: Pixels, Colors, and Picture Structure, Assignments of Computer Science

Toggle sidebar
bg1
C
CM
MS
SC
C
1
13
31
1:
:
C
Ch
ha
ap
pt
te
er
r
1
12
2
(
(S
Su
up
pp
pl
le
em
me
en
nt
t)
)
I
In
nt
te
er
rf
fa
ac
ce
es
s
a
an
nd
d
t
th
he
e
P
Pi
ic
ct
tu
ur
re
e
L
Li
ib
br
ra
ar
ry
y
F
Fu
un
nd
da
am
me
en
nt
ta
al
ls
s
o
of
f
D
Di
ig
gi
it
ta
al
l
I
Im
ma
ag
ge
es
s
Digital Image: A digital picture (also called a digital image) is stored as a two-dimensional
array of small regions called pixels
Pixel: (“picture element”) contains a representation of a color.
Color: There are many different ways to represent a color. The most common way for
computer monitors is based on the three (additive) primary colors: red, green, and blue,
also called RGB.
Color components: Each RGB color component is typically stored a single integer ranging
from 0 to 255.
F
Fu
un
nd
da
am
me
en
nt
ta
al
ls
s
o
of
f
D
Di
ig
gi
it
ta
al
l
I
Im
ma
ag
ge
es
s
Color Rescaling: Because the discrete range [0..255] is messy to work with computationally,
it is common to scale each integer color component to a double ranging from 0.0 to 1.0:
int intRed = … // get the pixels R component as an integer
double doubleRed = ( double ) intRed / 255.0;
Color values: 0.0 = darkest; 1.0 = brightest.
Some Common colors:
R G B Color R G B Color
1 1 1 White 0 0 0 Black
1 0 0 Red 0 1 1 Cyan (light blue)
0 1 0 Green 1 0 1 Magenta
0 0 1 Blue 1 1 0 Yellow
Mixtures: (0.5, 0.5, 0.5) = Gray; (0.23, 1.00, 0.51 ) = Lime green.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Understanding Digital Images: Pixels, Colors, and Picture Structure and more Assignments Computer Science in PDF only on Docsity!

CMCMSSCC 1 1331 1:: CChhaapptteerr 1 12 2 ((SSuupppplleemmeenntt)) InIntteerrffaacceess aanndd tthhee PPiiccttuurree LLiibbrraarryy

FFuunnddaammeennttaallss ooff DDiiggiittaall IImmaaggeess

Digital Image : A digital picture (also called a digital image ) is stored as a two-dimensional array of small regions called pixels

Pixel : (“ picture element ”) contains a representation of a color.

Color : There are many different ways to represent a color. The most common way for computer monitors is based on the three (additive) primary colors : red , green , and blue , also called RGB.

Color components : Each RGB color component is typically stored a single integer ranging from 0 to 255.

FFuunnddaammeennttaallss ooff DDiiggiittaall IImmaaggeess

Color Rescaling : Because the discrete range [0..255] is messy to work with computationally, it is common to scale each integer color component to a double ranging from 0.0 to 1.0: int intRed = … // get the pixels R component as an integer double doubleRed = ( double ) intRed / 255.0;

Color values : 0.0 = darkest; 1.0 = brightest.

Some Common colors : R G B Color R G B Color 1 1 1 White 0 0 0 Black 1 0 0 Red 0 1 1 Cyan (light blue) 0 1 0 Green 1 0 1 Magenta 0 0 1 Blue 1 1 0 Yellow

Mixtures : (0.5, 0.5, 0.5) = Gray; (0.23, 1.00, 0.51 ) = Lime green.

FFuunnddaammeennttaallss ooff DDiiggiittaall IImmaaggeess

Picture Structure : Pixels are arranged in a 2-dimensional grid.

Width and height : are measured in terms of the number of pixels in this grid, horizontally and vertically.

Pixel index : Each pixel is specified by giving its index, which indicates its row and vertical column numbers in this grid. Pixel indices start with (0, 0) in the upper left corner. Row indices : increase as we move down. Column indices : increase as we move to the right.

cm cmsscc1 1331 1PPiiccttuurreeLLiibb

cmsc131PictureLib : This package contains a number of classes for creating, accessing, and displaying images. Major elements: Picture : An interface for specifying the content of a picture. PictureUtil : Contains a number of utilities, most important the method show( p ) , for displaying a Picture p. PictureColor : Represents the color of a picture pixel. It stores the RGB color components as doubles in the range 0.0 to 1.0. Constructor : is given the RGB values (as doubles) as its arguments: PictureColor myPixel = new PictureColor( 0.23, 1.0, 0.512 ); Accessors : getRed( ), getGreen( ), and getBlue( ), return the respective RGB color components for the pixel (as doubles): double r = myPixel.getRed( ); // set r = 0. System.out.println( myPixel.getBlue( ) ); // outputs: 0.

HoHoww iitt WWoorrkkss

Huh? How can we define an entire image by just implementing three little methods (getWidth, getHeight, getColor)? You might ask, “Where is the 2-dimensional grid of pixels?"

Answer : You do not need to; it is generated by PictureUtil.show( ).

PictureUtil.show( p ) :

  • invokes p.getWidth( ) and p.getHeight( ) to determine the size of the new image;
  • asks Java to create an image of this size. (This creates the 2-dimensional grid.)
  • fills in the pixel colors of this image , row-by-row and column-by-column. To get the color of each pixel, it calls p.getColor( x, y ) , which returns the desired color.
  • when all the pixels have been filled in, it asks Java to display it.

EExxaammpplleess

To illustrate this, this consider a few examples of Picture classes. Remember, each one needs to define: getWidth, getHeight, and getColor.

  • RedSquare : Draws a solid red square.
  • FrenchFlag : Draws a French flag.
  • Inverse : Complements the colors of all pixels (as seen in HW3).
  • FlipLeftRight : Produces a mirror image of a picture, by flipping it left to right.

RReeddSSqquuaarree

RedSquare : Has a fixed width (150) and height (150), and every pixel is defined to be red. It has no instance data , and no need for a constructor.

We could create our own red color using:

PictureColor red = new PictureColor( 1, 0, 0 );

But the PictureColor class defines a number of common colors:

PictureColor.RED PictureColor.GREEN PictureColor.BLUE (…and also WHITE, BLACK, CYAN, MAGENTA, YELLOW, and GRAY).

RReeddSSqquuaarree Defining it:

public class RedSquare implements Picture { public int getWidth( ) { return 150; } public int getHeight( ) { return 150; } public PictureColor getColor( int x, int y ) { return PictureColor.RED; } }

Displaying it: (from your Driver main method):

… RedSquare redSquare = new RedSquare( ); PictureUtil.show( redSquare ); …

FFrreenncchhFFllaagg How does it work?

public PictureColor getColor( int x, int y ) { if ( x < width/3 ) return PictureColor.BLUE; else if ( x < 2width/3 ) return PictureColor.WHITE; else return PictureColor.RED; }*

As PictureUtil.show( ) fills in pixel colors starting with row y=0 and generates x = 0, 1, 2, …, 149:

getColor( 0, 0 ) → (0 < 50): yes → BLUE getColor( 1, 0 ) → (1 < 50): yes → BLUE … getColor(50, 0) → (50 < 50): no! (50 < 100): yes → WHITE … getColor(100, 0) → (100 < 50): no! (100 < 100): no! else → RED

IInnvveerrssee

Inverse : Is given a base image in its constructor and creates an image of the same size, but each pixel is replaced with its color complement. This is done by replacing each color component c, which ranges from 0.0 up to 1.0, with (1.0 - c).

Example : The yellowish tones of the cat's fur is close to the RGB color (1.0, 1.0, 0.0), and is mapped to (0.0, 0.0, 1.0), which is blue.

IInnvveerrssee public class Inverse implements Picture { private Picture base;

public Inverse( Picture basePicture ) { base = basePicture; } public int getWidth( ) { return base.getWidth( ); } public int getHeight( ) { return base.getHeight( ); } public PictureColor getColor( int x, int y ) { PictureColor color = base.getColor( x, y ); return new PictureColor( 1.0 - color.getRed( ), 1.0 - color.getGreen( ), 1.0 - color.getBlue( ) );

}^ } … Image cuteKitty = new Image( imageName ); Inverse psychoKitty = new Inverse( cuteKitty ); PictureUtil.show( psychoKitty ); …

AvAvooiiddiinngg ““OOffff--BByy--11”” EErrrroorrss

Off-by-1 : Being “off by 1” is a very common programming error.

Why did we use width-1?

flipX = width - 1 - x; (rather than) flipX = width - x;

Being off by 1 is very common, but easy to avoid. Just consider the two extreme cases of the largest and smallest possible values of x. We know that the column indices range from [0..width-1]. x width – x width – 1 – x min: 0 width (NO) width – 1 (YES) max: width – 1 1 (NO) 0 (YES)

If calculations work correctly in the extreme cases , they usually work correctly for the cases in between.