previous lecture - cs.cornell.edu · previous lecture: ... inv icando po i do dois “true” if...
TRANSCRIPT
Lecture 15 4
Previous Lecture: 2-d array examples
Today’s Lecture: Working with images
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 didn’t 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 14 6
Who Can Fill the Order?
38 5 99 34
82 19 83 12Inv
51 29 21 56
42
42
87
1 0 12 529PO
Yes
No
Yes
Lecture 14 7
Wanted: A True/False Function
iCanDoInv
PO
i
DO
DO is “true” if factory i can fill the order.DO is “false” if factory i cannot fill the order.
Lecture 14 8
Example: Check inventory of factory 2
38 5 99 34
82 19 83 12Inv
51 29 21 56
42
42
87
1 0 12 529PO
Lecture 14 9
Initialization
38 5 99 34
82 19 83 12Inv
51 29 21 56
42
42
87
1 0 12 529PO
DO 1
Lecture 14 10
Still True…
38 5 99 34
82 19 83 12Inv
51 29 21 56
42
42
87
1 0 12 529PO
DO 1
DO = DO && ( Inv(2,1) >= PO(1) )
Lecture 14 11
Still True…
38 5 99 34
82 19 83 12Inv
51 29 21 56
42
42
87
1 0 12 529PO
DO 1
DO = DO && ( Inv(2,2) >= PO(2) )
Lecture 14 12
Still True…
38 5 99 34
82 19 83 12Inv
51 29 21 56
42
42
87
1 0 12 529PO
DO 1
DO = DO && ( Inv(2,3) >= PO(3) )
Lecture 14 13
No Longer True…
38 5 99 34
82 19 83 12Inv
51 29 21 56
42
42
87
1 0 12 529PO
DO 0
DO = DO && ( Inv(2,4) >= PO(4) )
Lecture 14 14
Stay False…
38 5 99 34
82 19 83 12Inv
51 29 21 56
42
42
87
1 0 12 529PO
DO 0
DO = DO && ( Inv(2,5) >= PO(5) )
Lecture 14 15
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
Encapsulate…
Lecture 14 16
function DO = iCanDo(i,Inv,PO)% DO is true if factory i can fill% the purchase order. Otherwise, falsenProd = length(PO);j = 1;while j<=nProd && Inv(i,j)>=PO(j)
j = j+1;endDO = _______;
Encapsulate…
Lecture 14 17
function DO = iCanDo(i,Inv,PO)% DO is true if factory i can fill% the purchase order. Otherwise, falsenProd = length(PO);j = 1;while j<=nProd && Inv(i,j)>=PO(j)
j = j+1;endDO = _________;
Encapsulate…
DO should be true when…• j < nProd• j == nProd• j > nProd
A
CB
Lecture 14 18
function DO = iCanDo(i,Inv,PO)% DO is true if factory i can fill% the purchase order. Otherwise, falsenProd = length(PO);j = 1;while j<=nProd && Inv(i,j)>=PO(j)
j = j+1;endDO = (j>nProd);
Encapsulate…
Lecture 14 19
iBest = 0; minBill = inf;
for i=1:nFactiBill = iCost(i,C,PO);if iBill < minBill
% Found an Improvement iBest = i; minBill = iBill;
endend
Back To Finding the Cheapest
Lecture 14 20
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
endend
Back To Finding the Cheapest
See Cheapest.mfor alternative implementation
Lecture 15 24
A picture as a matrix
1458-by-2084
150 149 152 153 152 155151 150 153 154 153 156153 151 155 156 155 158154 153 156 157 156 159156 154 158 159 158 161157 156 159 160 159 162
Lecture 15 25
Images can be encoded in different ways
Common formats include JPEG: Joint Photographic Experts Group GIF: Graphics Interchange Format
Data are compressed We will work with jpeg files:
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)
Lecture 15 26
Grayness: a value in [0..255]
150 149 152 153 152 155151 150 153 154 153 156153 151 155 156 155 158154 153 156 157 156 159156 154 158 159 158 161157 156 159 160 159 162
0 = black255 = white
These are integer valuesType: uint8
Lecture 15 27
Let’s put a picture in a frame
Things to do:1. Read bwduck.jpg from memory and convert it
into an array2. Show the original picture3. Assign a gray value (frame color) to the “edge
pixels”4. Show the manipulated picture
Lecture 15 28
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)
Lecture 15 29
% Frame a grayscale picture
P= imread(’bwduck.jpg’);imshow(P)
% Change the ”frame” color
imshow(P)
Lecture 15 30
% Frame a grayscale picture
P= imread(’bwduck.jpg’);imshow(P)
% Change the ”frame” colorwidth= 50;frameColor= 200; % light gray
imshow(P)
Lecture 15 31
% Frame a grayscale picture
P= imread(’bwduck.jpg’);imshow(P)
% Change the ”frame” colorwidth= 50;frameColor= 200; % light gray[nr,nc]= size(P);for r= 1:nr
for c= 1:nc% At pixel (r,c)
endendimshow(P)
Lecture 15 32
% Frame a grayscale picture
P= imread(’bwduck.jpg’);imshow(P)
% Change the ”frame” colorwidth= 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-widthP(r,c)= frameColor;
endend
endimshow(P)
Things to consider…1. What is the type of the values in P?2. Can we be more efficient?
Lecture 15 33
Accessing a submatrix
M refers to the whole matrix M(3,5) refers to one component of M
2 0.5-1 -3
52 7.581 2
5 98.5-3 10
3 768 7M
Lecture 15 34
Accessing a submatrix
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
2 0.5-1 -3
52 7.581 2
5 98.5-3 10
3 768 7M
row indices
column indices
Lecture 15 35
Grayness: a value in [0..255]
150 149 152 153 152 155151 150 153 154 153 156153 151 155 156 155 158154 153 156 157 156 159156 154 158 159 158 161157 156 159 160 159 162
0 = black255 = white
These are integer valuesType: uint8
A color picture is made up of RGB matrices 3-d array114 114 112 112 114 111 114 115 112 113114 113 111 109 113 111 113 115 112 113115 114 112 111 111 112 112 111 112 112116 117 116 114 112 115 113 112 115 114113 112 112 112 112 110 111 113 116 115115 115 115 115 113 111 111 113 116 114112 113 116 117 113 112 112 113 114 113115 116 118 118 113 112 112 113 114 114116 116 117 117 114 114 112 112 114 115
153 153 150 150 154 151 152 153 150 151153 152 149 147 153 151 151 153 150 151154 153 151 150 151 152 150 149 150 150155 156 155 152 152 155 151 150 153 153151 150 150 150 150 148 149 151 152 151153 153 153 153 151 149 149 151 152 150150 151 152 153 151 150 150 151 152 151153 154 154 154 151 150 150 151 152 152154 154 153 153 149 149 150 150 152 153
212 212 212 212 216 213 215 216 213 213212 211 211 209 215 213 214 216 213 213213 212 210 209 212 214 213 212 213 212214 215 214 214 213 216 214 213 215 212213 212 212 212 212 210 211 213 214 211215 215 216 216 213 211 211 213 212 210212 213 214 215 213 212 212 213 214 213215 216 216 216 213 212 212 213 214 214216 216 215 215 213 213 213 213 214 215
0 ≤ A(i,j,1) ≤ 255
0 ≤ A(i,j,3) ≤ 255
0 ≤ A(i,j,2) ≤ 255E.g., color image data is stored in a 3-d array A:
Lecture 15 36
Lecture 15 37
Operations on images amount to operations on matrices!
Color image 3-d Array
0 ≤ A(i,j,1) ≤ 255
0 ≤ A(i,j,3) ≤ 255
0 ≤ A(i,j,2) ≤ 255
A color picture is made up of RGB matrices 3-d array
Lecture 15 38
Example: Mirror Image
LawSchool.jpg LawSchoolMirror.jpg
1. Read LawSchool.jpg from memory and convert it into an array.
2. Manipulate the Array.3. Convert the array to a jpg file and write it to memory.
Lecture 15 39
Reading and writing jpg files
% Read jpg image and convert to % a 3D array A
A = imread('LawSchool.jpg');
% Write 3D array B to memory as % a jpg image
imwrite(B,'LawSchoolMirror.jpg')
Lecture 15 40
A 3-d array as 3 matrices
[nr, nc, np] = size(A) % dimensions of 3-d array A
#rows#columns
#layers (pages)
4-by-6 M1= A(:,:,1)
4-by-6 M3= A(:,:,3)
4-by-6 M2= A(:,:,2)
A(1:nr,1:nc,1)
Lecture 15 41
%Store mirror image of A in array B
[nr,nc,np]= size(A);for r= 1:nr
for c= 1:nc
B(r,c )= A(r,nc-c+1 );
endend
A B
1
5
2
4
3
3
4
2
5
1
Lecture 15 42
%Store mirror image of A in array B
[nr,nc,np]= size(A);for r= 1:nr
for c= 1:ncfor p= 1:np
B(r,c,p)= A(r,nc-c+1,p);end
endend
Lecture 15 45
% Make mirror image of A -- the whole thing
A= imread(’LawSchool.jpg’);[nr,nc,np]= size(A);
for r= 1:nrfor c= 1:ncfor p= 1:npB(r,c,p)= A(r,nc-c+1,p);
endend
endimshow(B) % Show 3-d array data as an imageimwrite(B,’LawSchoolMirror.jpg’)
Lecture 15 46
% 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:nrfor c= 1:ncfor p= 1:npB(r,c,p)= A(r,nc-c+1,p);
endend
endimshow(B) % Show 3-d array data as an imageimwrite(B,’LawSchoolMirror.jpg’)
Lecture 15 47
Vectorized code simplifies things…Work with a whole column at a time
A
Lecture 15 48
Vectorized code simplifies things…Work with a whole column at a time
A B
Lecture 15 54
Vectorized code simplifies things…Work with a whole column at a time
A B
16
Lecture 15 55
Vectorized code simplifies things…Work with a whole column at a time
A B
16 25
Lecture 15 56
Vectorized code simplifies things…Work with a whole column at a time
A B
16 25 34
Lecture 15 57
Vectorized code simplifies things…Work with a whole column at a time
A B
16 25 34 6541 2 3
Column c in Bis column nc-c+1 in A
Lecture 15 58
Consider a single matrix (just one layer)
[nr,nc,np] = size(A);for c= 1:nc
B(: ,c ) = A(: ,nc+1-c );
end
all rows all rows
Lecture 15 59
Consider a single matrix (just one layer)
[nr,nc,np] = size(A);for c= 1:nc
B(1:nr,c ) = A(1:nr,nc+1-c );
end
Lecture 15 60
Consider a single matrix (just one layer)
[nr,nc,np] = size(A);for c= 1:nc
B( : ,c ) = A( : ,nc+1-c );
end
Lecture 15 61
Now repeat for all layers
[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
Lecture 15 62
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)
endimwrite(B,'LawSchoolMirror.jpg')
Prelim 1 Q1: Program trace (vectors) & function scope Q2: random numbers, for-loop pattern (for vector) Q3: accumulation pattern (similar to P2 pi sequence) Q4: simulation involving while-loop, rand int, if-construct; function
and function call; building a vector of non-determined length (similar to random walk and P3 simulation)
Q5: nested loops, drawing a 2-d pattern (similar to P2 café wall)
Median 84Mean 78.2; Std. Dev. 18.9Max 100
If your paper isn’t here, pick it up from Matlab consultants in ACCEL Green Rm during consulting hrs (starting today at 5pm)
if score>80celebrate, look up solutions and learn from mistakes
elseif score>60re-do the open questions that you got wrong first; then read solutions
elsesee course staff one-on-one to re-do the questions; avoid the solutions!
end