Image Processing Color Data Type Color A sensation

  • Slides: 18
Download presentation
Image Processing

Image Processing

Color Data Type Color. A sensation in the eye from electromagnetic radiation. Set of

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 105 105 Color 2

Color Data Type Color. A sensation in the eye from electromagnetic radiation. Set of

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/j 2 se/1. 5. 0/docs/api/java/awt/Color. html 3

Monochrome Luminance Monochrome luminance. Effective brightness of a color. NTSC formula. Y = 0.

Monochrome Luminance Monochrome luminance. Effective brightness of a color. NTSC formula. Y = 0. 299 r + 0. 587 g + 0. 114 b. import java. awt. Color; public class Luminance { public static double lum(Color c) { int r = c. get. Red(); int g = c. get. Green(); int b = c. get. Blue(); return. 299*r +. 587*g +. 114*b; } } 4

Grayscale. When all three R, G, and B values are the same, resulting color

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 to. Gray(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

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

Picture Data Type (0, 0) j Raster graphics. Basis for image processing. Set of values. 2 D array of Color objects (pixels). i API. (You will use a slightly modified version called “Picture 2”) 7

Image Processing: Grayscale Filter Code Demo Goal. Convert color image to grayscale according to

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.

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

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. n n n j hs / ht Halving: ht = hs/2; wt = ws /2 j Doubling: ht = 2 xhs; wt = 2 x ws ? i ws / wt source image (ws-by-hs) i target image (wt-by-ht) 10

Tips for HW 5 Reading and comparing strings: • n Link to Java String

Tips for HW 5 Reading and comparing strings: • n Link to Java String API Making the slide show • n n Each Picture 2 object uses its own display box Use a separate Picture 2 object and change its contents to display the image you want

Checking Equality of Reference Variables

Checking Equality of Reference Variables

Reference Variables and Equality Arrays and objects are both “reference” types • n n

Reference Variables and Equality Arrays and objects are both “reference” types • n n n 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? • n 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 s 1 = new String("hello”); String s 2 =

Reference Variable Equality Quiz String s 1 = new String("hello”); String s 2 = new String("hello”); System. out. println( s 1. equals(s 2) ); true System. out. println( s 1==s 2 ); false System. out. println( s 1==s 1 ); true String s 3 = s 1; System. out. println( s 1==s 3 ); true 14

Some things can’t be changed: Color objects, for example Immutability. Can't change a Color

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

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); D 0 D 4 D 8 DC 16

OOP Summary Object. Holds a data type value; variable name refers to object. In

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. n n Next. Object Oriented Design. 17

Code Example public static void main (String[] args) { String s 1 = "Defined

Code Example public static void main (String[] args) { String s 1 = "Defined in main. "; change. String(s 1); System. out. println("String obj is now: " + s 1); // reference still points to original object String. Buffer sb 1 = new String. Buffer("Defined in main. "); change. String. Buffer(sb 1); System. out. println("String. Buffer obj is now: " + sb 1); // object pointed to has be changed! } public static void change. String(String s) { s = "Changed in method"; // change is only local } public static void change. String. Buffer(String. Buffer sb) { sb. append(" -- Changed in method"); } 18