Professional Documents
Culture Documents
Previous Lecture:
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
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
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
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
Lecture 14
19
A picture as a matrix
1458-by-2084
0 = black 255 = white these are integer values type: uint8 (there are 2^8 power)
Lecture slides
CS1112 Lecture 15
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)
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
Lecture 15
32
Lecture 15
34
Lecture slides
CS1112 Lecture 15
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
Lecture 15
39
%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
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
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
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
B = A(:,nc:-1:1,:)
Lecture 15 63
Lecture slides
CS1112 Lecture 15
Can average the three color values to get one gray value.
Lecture 15
65
Lecture 15
66
.3R+.59G+.11B
G B
.3R+.59G+.11B
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