Using Data Types

Download Report

Transcript Using Data Types

Image Processing
Color Data Type
Color. A sensation in the eye from electromagnetic radiation.
Set of values. [RGB representation] 2563 possible values, which quantify
the amount of red, green, and blue, each on a scale of 0 to 255.
R
G
B
255
0
0
0
255
0
0
0
255
255
255
255
0
0
0
255
0
255
105
105
105
Color
2
Color Data Type
Color. A sensation in the eye from electromagnetic radiation.
Set of values. [RGB representation] 2563 possible values, which quantify
the amount of red, green, and blue, each on a scale of 0 to 255.
API. Application Programming Interface.
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Color.html
3
Monochrome Luminance
Monochrome luminance. Effective brightness of a color.
NTSC formula. Y = 0.299r + 0.587g + 0.114b.
import java.awt.Color;
public class Luminance {
public static double lum(Color c) {
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
return .299*r + .587*g + .114*b;
}
}
4
Grayscale
Grayscale. When all three R, G, and B values are the same,
resulting color is on grayscale from 0 (black) to 255 (white).
Convert to grayscale. Convert R, G, B values to their corresponding
monochrome luminance.
public static Color toGray(Color c) {
int y = (int) Math.round(lum(c));
Color gray = new Color(y, y, y);
return gray;
}
round double to nearest int
5
Color Compatibility
To determine the colors that will be readable with other background
colors on computer monitors and cell phone screens.
A. Rule of thumb: difference in luminance should be  128.
256
208
105
47
28
14
public static boolean compatible(Color a, Color b) {
return Math.abs(lum(a) - lum(b)) >= 128.0;
}
6
Picture Data Type
(0, 0)
j
Raster graphics. Basis for image processing.
Set of values. 2D array of Color objects (pixels).
i
API. (You will use a slightly modified version called “Picture2”)
7
Image Processing: Grayscale Filter
Code Demo
Goal. Convert color image to grayscale according to luminance formula.
Algorithm:

1.
2.
3.
For each pixel in the image:
Extract its R, G, B components
Convert to corresponding grayscale luminance
Put the grayscale luminance in the image at the position of the old
pixel
8
Image Processing: Scaling Filter
Goal. Shrink or enlarge an image to desired size.
Downscaling. To shrink, delete half the rows and columns.
Upscaling. To enlarge, replace each pixel by 4 copies.
9
Image Processing: Scaling Filter
Code Demo
Goal. Shrink or enlarge an image to desired size.
Strategy. To convert from ws-by-hs to wt -by-ht :
Scale row index by ws / wt .
Scale column index by hs / ht .
Set color of pixel (i, j) in target image to color of pixel
(i  ws / wt , j  hs / ht ) in source image.



j  hs / ht
Halving:
ht = hs/2; wt = ws /2
j
Doubling:
ht = 2xhs; wt = 2x ws
?
i  ws / wt
source image
(ws-by-hs)
i
target image
(wt-by-ht)
10
Tips for HW5





Reading and comparing strings:
Link to Java String API
Making the slide show
Each Picture2 object uses its own display
box
Use a separate Picture2 object and change
its contents to display the image you want
Checking Equality of Reference Variables
Reference Variables and Equality






Arrays and objects are both “reference”
types
They are allocated a chunk of memory in the
address space
The memory needs to be initialized
Assigning one object/array to another object/array
results in an alias
How do we check the equality of objects?
Cannot use == operator
Use instance method equals(). Can define this
method for any class. Its your responsibility
to include this method if you are designing a
new class.
13
Reference Variable Equality Quiz
String s1 = new String("hello”);
String s2 = new String("hello”);
System.out.println( s1.equals(s2) );
true
System.out.println( s1==s2 );
false
System.out.println( s1==s1 );
true
String s3 = s1;
System.out.println( s1==s3 );
true
14
Some things can’t be changed: Color objects, for example
Immutability. Can't change a Color object's value once created.
no relevant methods in Color API
Consequence. Don't need to worry about aliases.
Color a = new Color(160, 82,
Color b = a;
a = new Color(0, 0, 0);
45);
makes a point to a different Color, but does not change b
15
Java objects: Some are Immutable, Some are Not
Immutability. Can't change a Color object's value once created.
no relevant methods in Color API
We can create a new color from an old one.
We can change the Color object the reference points to.
The String class is immutable too.
Mutability. Can change a Picture object's value.
Color red = new Color(255, 0,
pic.set(0, 3, red);
0);
D0
D4
D8
DC
16
OOP Summary
Object. Holds a data type value; variable name
refers to object.
In Java, programs manipulate references to objects.
Exception: primitive types, e.g., boolean, int,
double.
Reference types: String, Picture, Color, arrays,
everything else.


Next. Object Oriented Design.
17
Code Example
public static void main (String[] args) {
String s1 = "Defined in main.";
changeString(s1);
System.out.println("String obj is now: " + s1);
// reference still points to original object
StringBuffer sb1 = new StringBuffer("Defined in main.");
changeStringBuffer(sb1);
System.out.println("StringBuffer obj is now: " + sb1);
// object pointed to has be changed!
}
public static void changeString(String s) {
s = "Changed in method"; // change is only local
}
public static void changeStringBuffer(StringBuffer sb) {
sb.append(" -- Changed in method");
}
18