You are on page 1of 6

CS1112 Lecture 15

Previous Lecture:

Who Can Fill the Order?


38 5 19 29 99 83 21 34 12 56 42 42 87

2-d array examples Working with images

Todays Lecture:

Yes No Yes

Inv

82 51

Announcements:

Discussion this week in the UP B7 computer lab Prelim 1 to be returned at end of lecture. Unclaimed papers (and those on which student didnt indicate the lecture time) can be picked up during consulting hours (Su-R 5-10p) at ACCEL Green Rm (Carpenter Hall) starting at 5pm today
Lecture 15 4

PO

12 29 5

Lecture 14

Wanted: A True/False Function i Inv PO DO is true if factory i can fill the order. DO is false if factory i cannot fill the order.
Lecture 14 7

Example: Check inventory of factory 2

38

5 19 29

99 83 21

34 12 56

42 42 87

iCanDo

DO

Inv

82 51

PO

12 29 5

Lecture 14

Initialization
38 5 19 29 99 83 21 34 12 56 42 42 87

Still True
38 5 19 29 99 83 21 34 12 56 42 42 87

Inv

82 51

DO

Inv

82 51

DO

PO

12 29 5

PO

12 29 5

DO = DO && ( Inv(2,1) >= PO(1) )


Lecture 14 9

without any if statements; checking inv(2,1)>=PO(1) Lecture 14 - 'history term':: is it true first bit of boolean expression before?

10

Lecture slides

CS1112 Lecture 15

No Longer True
38 5 19 29 99 83 21 34 12 56 42 42 87

Encapsulate
function DO = iCanDo(i,Inv,PO) % DO is true if factory i can fill % the purchase order. Otherwise, false nProd = length(PO); DO = 1; for j = 1:nProd DO = DO && ( Inv(i,j) >= PO(j) ); end
Lecture 14 15

Inv

82 51

DO

PO

12 29 5

DO = DO && ( Inv(2,4) >= PO(4) )


DO for product 4: TRUE && FALSE = FALSE DO for product 5: FALSE && TRUE = FALSELecture 14
13

Encapsulate
function DO = iCanDo(i,Inv,PO) % DO is true if factory i can fill % the purchase order. Otherwise, false nProd = length(PO); j = 1; while j<=nProd && Inv(i,j)>=PO(j) j = j+1; DO should be true when end A j < nProd DO = _________; B j == nProd C j > nProd
Lecture 14 17

Back To Finding the Cheapest


iBest = 0; minBill = inf; for i=1:nFact iBill = iCost(i,C,PO); if iBill < minBill % Found an Improvement iBest = i; minBill = iBill; end end

Lecture 14

19

Back To Finding the Cheapest


iBest = 0; minBill = inf; for i=1:nFact if iCanDo(i,Inv,PO) iBill = iCost(i,C,PO); if iBill < minBill % Found an Improvement iBest = i; minBill = iBill; end end end See Cheapest.m
for alternative implementation
Lecture 14 20

A picture as a matrix
1458-by-2084
0 = black 255 = white these are integer values type: uint8 (there are 2^8 power)

150 151 153 154 156 157

149 150 151 153 154 156

152 153 155 156 158 159

153 154 156 157 159 160

152 153 155 156 158 159

155 156 158 159 161 162


Lecture 15 24

Lecture slides

CS1112 Lecture 15

Images can be encoded in different ways

Lets put a picture in a frame

Common formats include


JPEG: Joint Photographic Experts Group GIF: Graphics Interchange Format

Data are compressed We will work with jpeg files:

Things to do:
1.

imread: read a .jpg file and convert it to a normal numeric array that we can work with imwrite: write an array into a .jpg file (compressed data)

2. 3.

4.

Read bwduck.jpg from memory and convert it into an array Show the original picture Assign a gray value (frame color) to the edge pixels Show the manipulated picture
Lecture 15 27

Lecture 15

25

Reading a jpeg file and displaying the image % Read jpg image and convert to % an array P P = imread(bwduck.jpg'); % Show the data in array P as % an image imshow(P)

% Frame a grayscale picture P= imread(bwduck.jpg); imshow(P) % Change the frame color width= 50; frameColor= 200; % light gray [nr,nc]= size(P); for r= 1:nr for c= 1:nc % At pixel (r,c)

end end imshow(P)

Lecture 15

28

Lecture 15

31

% Frame a grayscale picture P= imread(bwduck.jpg); imshow(P) % Change the frame color width= 50; frameColor= 200; % light gray [nr,nc]= size(P); for r= 1:nr for c= 1:nc % At pixel (r,c) if r<=width || r>nr-width || ... c<=width || c>nc-width P(r,c)= frameColor; Things to consider end 1. What is the type of the end values in P? end 2. Can we be more efficient? imshow(P)

Accessing a submatrix

2 3 5

-1 .5 8 6

0 7 7

-3 7 2

-3 8.5 9 10

52 81 .5

M refers to the whole matrix M(3,5) refers to one component of M M(2:3,3:5) refers to a submatrix of M

row indices column indices

Lecture 15

32

Lecture 15

34

Lecture slides

CS1112 Lecture 15

Grayness: a value in [0..255]


0 = black 255 = white These are integer values Type: uint8

A color picture is made up of RGB matrices 3-d array


114 114 115 116 113 115 112 115 116 153 153 154 155 151 153 150 153 154 212 212 213 214 213 215 212 215 216 114 113 114 117 112 115 113 116 116 153 152 153 156 150 153 151 154 154 212 211 212 215 212 215 213 216 216 112 111 112 116 112 115 116 118 117 150 149 151 155 150 153 152 154 153 212 211 210 214 212 216 214 216 215 112 109 111 114 112 115 117 118 117 150 147 150 152 150 153 153 154 153 212 209 209 214 212 216 215 216 215 114 113 111 112 112 113 113 113 114 154 153 151 152 150 151 151 151 149 216 215 212 213 212 213 213 213 213 111 111 112 115 110 111 112 112 114 151 151 152 155 148 149 150 150 149 213 213 214 216 210 211 212 212 213 114 113 112 113 111 111 112 112 112 152 151 150 151 149 149 150 150 150 215 214 213 214 211 211 212 212 213 115 115 111 112 113 113 113 113 112 153 153 149 150 151 151 151 151 150 216 216 212 213 213 213 213 213 213 112 112 112 115 116 116 114 114 114 150 150 150 153 152 152 152 152 152 213 213 213 215 214 212 214 214 214 113 113 112 114 115 114 113 114 115 151 151 150 153 151 150 151 152 153 213 213 212 212 211 210 213 214 215

150 151 153 154 156 157

149 150 151 153 154 156

152 153 155 156 158 159

153 154 156 157 159 160

152 153 155 156 158 159

155 156 158 159 161 162


Lecture 15 35

E.g., color image data is stored in a 3-d array A:


Lecture 15

0 A(i,j,1) 255 0 A(i,j,2) 255 0 A(i,j,3) 255


36

Example: Mirror Image

Reading and writing jpg files % Read jpg image and convert to % a 3D array A A = imread('LawSchool.jpg');

LawSchool.jpg
1.

LawSchoolMirror.jpg

2. 3.

Read LawSchool.jpg from memory and convert it into an array. Manipulate the Array. Convert the array to a jpg file and write it to memory.
Lecture 15 38

% Write 3D array B to memory as % a jpg image imwrite(B,'LawSchoolMirror.jpg')

Lecture 15

39

A 3-d array as 3 matrices


[nr, nc, np] = size(A) % dimensions of 3-d array A
#rows #columns #layers (pages)
2-d array

%Store mirror image of A in array B [nr,nc,np]= size(A); for r= 1:nr for c= 1:nc for p= 1:np B(r,c,p)= A(r,nc-c+1,p); end end end
Lecture 15 42

A(1:nr,1:nc,1) M1= A(:,:,1) M2= A(:,:,2) M3= A(:,:,3)


40

4-by-6 4-by-6 4-by-6


Lecture 15

Lecture slides

CS1112 Lecture 15

[nr,nc,np]= size(A); for r= 1:nr for c= 1:nc for p= 1:np B(r,c,p)= A(r,nc-c+1,p); end end [nr,nc,np]= size(A); end for p= 1:np for r= 1:nr Both fragments for c= 1:nc create a mirror B(r,c,p)= A(r,nc-c+1,p); image of A . end A true end false B end
Lecture 15 44

% Make mirror image of A - the whole thing A= imread(LawSchool.jpg); [nr,nc,np]= size(A); B= zeros(nr,nc,np); B= uint8(B); % Type for image color values for r= 1:nr for c= 1:nc for p= 1:np B(r,c,p)= A(r,nc-c+1,p); end end end imshow(B) % Show 3-d array data as an image imwrite(B,LawSchoolMirror.jpg)
Lecture 15 46

Vectorized code simplifies things Work with a whole column at a time


A B

Consider a single matrix (just one layer) [nr,nc,np] = size(A); for c= 1:nc all rows,c all rows ,nc+1-c B(: ) = A(:

);

1 2 3 4 5 6

1 2 3 4 5 6

Column c in B is column nc-c+1 in A

end

Lecture 15

57

Lecture 15

58

Vectorized code to create a mirror image A = imread(LawSchool.jpg) [nr,nc,np] = size(A); for c= 1:nc B(:,c,1) = A(:,nc+1-c,1) B(:,c,2) = A(:,nc+1-c,2) B(:,c,3) = A(:,nc+1-c,3) end imwrite(B,'LawSchoolMirror.jpg')
Lecture 15 62

Even more compact vectorized code to create a mirror image

for c= 1:nc B(:,c,1) = A(:,nc+1-c,1) B(:,c,2) = A(:,nc+1-c,2) B(:,c,3) = A(:,nc+1-c,3) end

B = A(:,nc:-1:1,:)
Lecture 15 63

Lecture slides

CS1112 Lecture 15

Example: color black and white

Averaging the RGB values to get a gray value


R G B
.3R+.59G+.11B

Can average the three color values to get one gray value.

Lecture 15

65

Lecture 15

66

Averaging the RGB values to get a gray value


R G B for i= 1:m for j= 1:n M(i,j)= .3*R(i,j) + .59*G(i,j) + .11*B(i,j) end end scalar operation
Lecture 15 68

Averaging the RGB values to get a gray value


R

.3R+.59G+.11B

G B

.3R+.59G+.11B

M= .3*R + .59*G + .11*B

vectorized operation

Lecture 15

69

Here are 2 ways to calculate the average. Are gray value matrices g and h the same given image data A?
for r= 1:nr for c= 1:nc g(r,c)= A(r,c,1)/3 + A(r,c,2)/3 + ... A(r,c,3)/3; h(r,c)= ... ( A(r,c,1)+A(r,c,2)+A(r,c,3) )/3; end end

showToGrayscale.m

Matlab has a built-in function to convert from color to grayscale, resulting in a 2-d array: B = rgb2gray(A)

A: yes

B: no

Lecture 15

70

Lecture 15

71

Lecture slides

You might also like