1 lab of comp 319 lab tutor : shenghua zhong email: [email protected] [email protected] lab...
TRANSCRIPT
![Page 1: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/1.jpg)
1
Lab of COMP 319
Lab tutor : Shenghua ZHONGEmail: [email protected]
Lab 2: Nov 9, 2011
Image Processing with MATLAB
![Page 2: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/2.jpg)
2
Outline of Lab 3
1. Review of Lab 2
2. User defined function
3. Execution control
4. Plots and graphs using Matlab
5. Basic manipulation in image processing
6. Color image compression
![Page 3: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/3.jpg)
3
Outline of Lab 3
1. Review of Lab 2
2. User defined function
3. Execution control
4. Plots and graphs using Matlab
5. Basic manipulation in image processing
6. Color image compression
![Page 4: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/4.jpg)
4
Review of Lab 2 about Data Structure
1. Introduction the basic data structure in Matlab
• Vector and Array
2. The distinct attributes of these data structures
• Numerical Value
• Index
![Page 5: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/5.jpg)
5
Review of Lab 2 about Data Structure
3. What we have learnt
• How to create them• For example: A = [3, 4, 5; 6, 5.5, 0]
• How to manipulate them• For example: A(1,1:2) = A(1,1:2) + 2
• How to access their elements• For example: B = A(1,1:2)
• How to perform mathematical and logical operations on them• For example: C = A./2.5; D = sum(sum(A))
• For example: E = A > 0.5
![Page 6: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/6.jpg)
6
Review of Lab 2 about M file
• “File” - “New” - “Script”/“Function”
![Page 7: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/7.jpg)
7
Review of Lab 2 about M file
• Open the M-file’s and run it.
![Page 8: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/8.jpg)
8
Outline of Lab 3
1. Review of Lab 2
2. User defined function
3. Execution control
4. Plots and graphs using Matlab
5. Basic manipulation in image processing
6. Color image compression
![Page 9: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/9.jpg)
User-Defined Function
• A function is a piece of computer code that accepts an input argument from the user and provides output to the program.
You may wish to define your own functions-those
which are used commonly in your programming. User-defined functions are stored as M-files and can be accessed by Matlab if they are in the current directory.
9
![Page 10: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/10.jpg)
User-Defined Function
• Each function consists of a name, user-provided input, and calculated output. For example, the function: my_function(x) – is named my_function,– takes user input inside the parentheses (in this case, x), and– calculate a result.
• The user does not see the calculations performed, but just accepts the answer. The function could be regarded as a black box.
input outputfunction
10
![Page 11: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/11.jpg)
User-Defined Function
• User-defined functions are stored as M-files. Each must start with a function definition line that contains the word “function”, one or several variables that defines the function output, a function name, and one or several variables used for the input argument. Save the function as M-file using the same name in your function
• Some examples: • function output = my_function(x) • function [output1, output2] = my_function(x1,x2,x3)
• Hints: The function name and the names of the input and output variables are arbitrary and selected by the programmer, but the word “function” can not be changed.
11
![Page 12: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/12.jpg)
User-Defined Function
function output = my_poly(x)
% This function calculates the value of a third-order
% polynomial
output = 3*x.^3+5*x.^2-2*x+1
Save above commands into an M-file and then type the below commands in command window to observe the results:
>> a=4;
>> my_poly(a)
>> b=1:5;
>> my_poly(b)
comments
12
![Page 13: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/13.jpg)
13
Outline of Lab 3
1. Review of Lab 2
2. User defined function
3. Execution control
4. Plots and graphs using Matlab
5. Basic manipulation in image processing
6. Color image compression
![Page 14: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/14.jpg)
1414
Objectives of Execution Control
• Why need execution control
– You may want to execute some parts of the code under certain circumstances only;
– You may want to repeat a section of code a certain number of times.
![Page 15: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/15.jpg)
1515
Conditional Execution
• General concepts
• if statements
![Page 16: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/16.jpg)
1616
General Concepts
• Generally speaking, the statements written in our scripts have been executed in sequence from the top (the first line) to the bottom (the last line).
• However, it is frequently necessary to make choices about how to process a set of data based on some characteristic of that data.
![Page 17: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/17.jpg)
1717
if Statements
• A set of statements (the code block to be executed) is shown as a rectangle, a decision point is shown as a diamond, and the flow of program control is indicated by arrows.
• The execution of a code block is based on some conditional test. If the result of the test is true, the code block is executed. Otherwise, the code block is omitted and the instructions after the end of that code block are executed.
if condition
statements
true
false
A simple if statement
![Page 18: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/18.jpg)
18
if Statements
Examples: Suppose Test =[2 0 0 0 ; 3 0 0 0; 4 0 0 0; 0 0 0 1]
Type
if Test(4,4)>0 display(‘Condition is true’)end
Matlab displaysans = Condition is true
Type
if Test(1,1)>0 | Test(2,4)==0 display(‘TRUE’)else display(‘FALSE’)end
Matlab displaysans = TRUE
![Page 19: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/19.jpg)
1919
if Statements
• In the compound if statement, if the first logical test returns false, a second test is performed to determine whether the second code block should be executed. If that test returns false, as many further tests as necessary may be performed, each with the appropriate code block to be implemented when the result is true. Finally, if none of these tests returns true, the last code block (with the keyword else) is executed.
if condition
statements(1)
true
false
A compound if statement (more than one conditional test)
elseif condition
statements(2)
true
false elseif condition
statements(n)
true
false
statements(n+1)
… else
![Page 20: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/20.jpg)
2020
if Statements
1. If one of the code blocks is executed, the next instruction to execute is the one that follows the conditional code after the end statement.
2. In particular, if there is no else clause, it is possible that no code at all is executed in this conditional statement.
if condition
statements(1)
true
false
A compound if statement (more than one conditional test)
elseif condition
statements(2)
true
false elseif condition
statements(n)
true
false
statements(n+1)
… else
![Page 21: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/21.jpg)
2121
General Template of if Statements
if <logical expression 1>
<code block 1>
elseif <logical expression 2>
<code block 2>
elseif <logical expression n>
<code block n>
else
<default code block>
end
…
![Page 22: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/22.jpg)
2222
Important Notes of if Statements
1. The only essential ingredients are the first if statement, one code block, and the end statement. All other features may be added as the logical requires.
2. The code blocks may contain any sequence of legal Matlab statements, including other if statements (nested ifs).
3. Nested if statements with a code block are an alternative implementation of a logical and statement.
clear all;a = 5;if a>3 & a-4==1 b=6end
clear all;a = 5;if a>3 if a-4 == 1 b=6 endend
![Page 23: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/23.jpg)
2323
Iteration
• General concepts
• for loops
![Page 24: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/24.jpg)
2424
General Concepts
• Iteration allow controlled repetition of a code block. Control statements at the beginning of the code block specify the manner and extent of the repetition.
• The for loop is designed to repeat its code block a fixed number of times and largely automates the process of managing the iteration.
![Page 25: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/25.jpg)
2525
for Loops
• The repeated execution of the code block is performed under the control of a loop-control variable. It is first set to an initial value that is tested against a terminating condition. If the terminating test succeeds, the program leaves the for loop. Otherwise, the computations in the code block are performed using the current value of that variable. When one pass through the code block is finished, the variable is updated to its next value, and control returns to the termination test.
for <loop>
computations
Done
Structure of a for loop
![Page 26: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/26.jpg)
2626
General Template of for Loops
for <variable specification> <code block>end
• All of the mechanics of iteration control are handled automatically in the variable specification section.
![Page 27: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/27.jpg)
2727
Important Notes of for Loops
• The core concept in the Matlab for loop implementation is in the style of the variable specification, which is accomplished as follows:
<variable specification>: <variable> = <vector>
• where <variable> is the name of the loop control variable and <vector> is any vector that can be created by the techniques discussed in this lab.
![Page 28: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/28.jpg)
28
for Statements
Example:
Type for k = 1:10 Remember - the colon operator k indicates every integer between end the start and end ORMatlab displays start : increment : endk = 1k = 2 and so on, through k = 10.
Type for counter = 2:2:10 counter notice that counter increments end by 2 with each time thru the loop
![Page 29: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/29.jpg)
29
Exercise 1 (Control Structure)
Create a M-file and write these codes down.
% use the percent sign toMymatrix = [1:5; 14:2:22; 50:-7:22]; % indicate commentsfor i = 1:3 for j = 1:5 if Mymatrix(i,j)< 5 | Mymatrix(i,j) > 20 newmatrix(i,j) = Mymatrix(i,j); elseif Mymatrix(i,j) == 20 newmatrix(i,j) = 100; else newmatrix(i,j) = 0; %semicolon means end %Matlab won’t display endend
![Page 30: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/30.jpg)
30
Outline of Lab 3
1. Review of Lab 2
2. User defined function
3. Execution control
4. Plots and graphs using Matlab
5. Basic manipulation in image processing
6. Color image compression
![Page 31: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/31.jpg)
31
Basic Plotting
>> x=[0:2:18];
>> y=[0,0.33,4.13,6.29,6.85,11.19,13.19,13.96,16.33,18.17];
>> plot(x,y)
Simple x-y plots:
Hint: The number of elements in vector x must be equal to the number of elements in vector y, otherwise it will make an error.
![Page 32: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/32.jpg)
32
Basic Plotting
>> x=[0:2:18];
>> y=[0,0.33,4.13,6.29,6.85,11.19,13.19,13.96,16.33,18.17];
>> plot(x,y), title('Lab Experiment 1'), xlabel('Time'), ylabel('Distance'), grid on
Titles, Labels, and Grids:
Hint: You must create a graph before you add the title and labels. If you specify the title and labels first, they are erased when the plot command executes.
![Page 33: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/33.jpg)
33
Basic Plotting
>> x=[0:2:18];
>> y=[0,0.33,4.13,6.29,6.85,11.19,13.19,13.96,16.33,18.17];
>> plot(x,y,':ok',x,y*2,'--xr',x,y/2,'-b')
Line, Color, and Mark style:
Hint: You plot three different x-y plots in one image, using different line type, point type and color.
![Page 34: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/34.jpg)
34
Basic Plotting
Line, Color, and Mark style:
Line Type Indicator Point Type Indicator Color Indicator
solid - point . blue b
dotted : circle o green g
dash-dot -. x-mark x red r
dashed -- plus + cyan c
star * magenta m
square s yellow y
diamond d black k
![Page 35: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/35.jpg)
35
Exercise 2 (Basic Plotting)
1. Plot x versus y for y=sin(x). Let x vary from 0 to 2pi in increments of 0.1pi.
2. Add a title and labels to your plot.
3. Plot x versus y1 and y2 for y1=sin(x) and y2=cos(x). Let x vary from 0 to 2*pi in increments of 0.1*pi. Add a title and labels to your plot.
4. Re-create the plot from step 3, but make the sin(x) line dashed and red. Make the cos(x) line green and dotted.
5. Use the M file to write and run it.
![Page 36: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/36.jpg)
36
Subplots
• The subplot command allows you to subdivide the graphing window into a grid of m rows and n columns.
• The function:
subplot(m,n,p)
split the figure into m*n matrix. The variable p identifies the portion of the window where the current plot will be drawn.
![Page 37: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/37.jpg)
37
Subplots
• For example, if the command subplot(2,2,1) is used, the window is divided into two rows and two columns, and the plot is drawn in the upper left-hand window. The windows are numbered from left to right, top to bottom.
p=1 p=2
p=3 p=4
![Page 38: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/38.jpg)
38
Subplots
>> x=0:pi/20:2*pi;
>> subplot(2,1,1)
>> plot(x,sin(x))
>> subplot(2,1,2)
>> plot(x,sin(2*x))
Simple x-y plots, in the first part of figure, y = sin(x). And in the second part of figure, y= sin(2*x).
![Page 39: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/39.jpg)
39
Exercise 3 (Subplots)
1. Subdivided a figure window into one row and two columns.
2. In the left window, plot y=tan(x) Let x vary from -1.5 to 1.5 in increment of 0.1.
3. Add a title and axis labels to your graph.
4. In the right window, plot y=sinh(x) for the same x range. (Hyperbolic sine function)
5. Add a title and axis labels to this graph.
6. Use the M file to write and run it.
![Page 40: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/40.jpg)
40
Histograms
• A histogram is a special type of graph that is particularly useful for the statistical analysis of data. A histogram is a plot showing the distribution of a set of values. In Matlab, the histogram compute the number of values falling into 10 bins (categories) that are equally spaced between the minimum and maximum values.
>> x=[100,96,74,87,75,22,56,78,34,35,93,88,86,42,55,48,9,6];
>> hist(x)
![Page 41: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/41.jpg)
41
Histograms
• The default number of bins is 10, but if we have a large data set, we may want to divide the data up into more bins. For example, to create a histogram with 25 bins, the command would be hist(x, 25) .
![Page 42: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/42.jpg)
42
Exercise 4 (Histogram)
1. Subdivided a figure window into two rows and one column.
2. In the top window, plot the histogram of x with the default number of bins.
3. Add a title to your graph.
4. In the bottom window, plot the histogram of x with 20 bins.
5. Add a title to this graph.
6. Use the M file to write and run it.
Suppose that x = [1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 14, 17, 19, 23, 29, 30, 31, 32, 35, 40, 57,66,67,68,80,90,91,100]
![Page 43: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/43.jpg)
43
Outline of Lab 3
1. Review of Lab 2
2. User defined function
3. Execution control
4. Plots and graphs using Matlab
5. Basic manipulation in image processing
6. Color image compression
![Page 44: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/44.jpg)
44
The MATLAB Image Processing Toolbox
• The Image Processing Toolbox is a collection of MATLAB functions (called M-functions or M-files) that extend the capability of the MATLAB environment for the solution of digital image processing problems.
![Page 45: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/45.jpg)
45
The MATLAB Image Processing Toolbox
• Including: Spatial transformations and image registration Linear filtering and transforms Image enhancement and restoration Image analysis and statistics
![Page 46: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/46.jpg)
46
How to Find Suitable M-function?
• Find it in Matlab Help.
-by category
-by alphabetical list
• Find it on the textbook.
• Find in the sub-folder in Matlab
![Page 47: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/47.jpg)
47
Where is Image Processing Toolbox
![Page 48: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/48.jpg)
48
Matlab Built-in Images
• Path: \Matlab\R2011a\toolbox\images\imdemos
Y:\Win32\Matlab\R2011a\toolbox\images\imdemos
• They are built-in images in Matlab which can be used directly.
• It is very convenient to use these images to observe some image processing results.
coins
onion
![Page 49: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/49.jpg)
49
Reading Image
• Function:– imread()
• Goal:– Load the image and save it
as the array format.
• Method:– I = imread(filename);– [I,map] = imread(filename);
• Examples: I = imread('pout.tif');
I = imread('rice.png');
pout
rice
![Page 50: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/50.jpg)
50
Displaying Image
• Function:– imshow()
• Goal:– Open a window to show the image
• Method:– imshow(I)
• Goal:– Open a new window to show the image
• Method:– figure,imshow(I)
![Page 51: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/51.jpg)
51
Displaying Image(cont.)
• Function:– colorbar
• Goal:– To display an image with a colorbar that indicates the range of
intensity values.
• Method:– imshow(I), colorbar
• Example:– I = imread('pout.tif');
imshow(I) , colorbar
![Page 52: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/52.jpg)
52
Writing Image
• Function:– imwrite()
• Goal:– Function: write the image out as a file
• Method:– imwrite(I,filename,format)
• Example:– imwrite(I, ‘pout.jpg’, ‘JPEG’);
![Page 53: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/53.jpg)
53
Image Information
• Function:– size()
• Goal:– Returns the number of rows and columns of an matrix/image
• Method:– [M,N] = size(I) for matrix/image I, returns the number of rows and
columns in X as separate output variables.
• Example:– I= imread('saturn.png'); % I is a gray image
[I_x,I_y] = size(I) % I_x= height of the image, I_y= width of the image
• Method:– M = SIZE(X,DIM) returns the length of the dimension specified
![Page 54: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/54.jpg)
54
Image Information
• Function:– whos
• Goal:– Display information about a variable .
• Example:– whos I
• Function:– imfinfo()
• Goal:– display information about image file .
• Example:– info = imfinfo('saturn.png')
![Page 55: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/55.jpg)
55
Digital Image Processing
• Function:– im2bw()
• Goal:– Convert intensity image I to binary image g using threshold T,
where T must be in range [0, 1].
• Method:– g = im2bw(I, T);
• Example:– I= imread('pout.tif');
g = im2bw(I, 0.4);
imshow(g) ,colorbar
![Page 56: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/56.jpg)
56
Digital Image Processing (cont.)
• Function:– rgb2gray()
• Goal:– Transform RGB color model image into gray-level image.
• Example:– I= imread ('saturn.png');
imshow(I);
g = rgb2gray(I);
figure,
imshow(g), colorbar
![Page 57: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/57.jpg)
57
Digital Image Processing (cont.)
• Function:– imresize()
• Goal:– Change the size of an image.
• Method:– imresize(A, SCALE, METHOD)
• Example:– I = imread('circuit.tif');
J = imresize(I,1.25);
imshow(I)
figure, imshow(J)
• Method:– imresize(A, [NUMROWS
NUMCOLS], METHOD)
• Example:– I = imread('circuit.tif');
J = imresize(I,[100 150], 'bilinear'); imshow(I) figure, imshow(J)
![Page 58: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/58.jpg)
58
Digital Image Processing (cont.)
• Function:– imrotate();
• Goal:– Rotate image A by ANGLE degrees in a counterclockwise
direction around its center point.
• Method:– imrotate(I, angle);
• Example:– I = imread('pout.tif');
J = imrotate(I,35);
imshow(J)
![Page 59: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/59.jpg)
59
More Example (cont.)
• How to use this function ?– imfilter()
• Find instructions about it by help– Help imfilter
• Write this code and see what will happen?– I = imread('coins.png');
h = ones(5,5) / 25;
I2 = imfilter(I,h);
imshow(I), title('Original Image');
figure, imshow(I2), title('Filtered Image')
![Page 60: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/60.jpg)
60
More Example (cont.)
• Write this code and see what will happen?– I = imread('cameraman.tif');
h = fspecial('unsharp');
I2 = imfilter(I,h);
imshow(I), title('Original Image')
figure, imshow(I2), title('Filtered Image')
![Page 61: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/61.jpg)
61
Outline of Lab 3
1. Review of Lab 2
2. User defined function
3. Execution control
4. Plots and graphs using Matlab
5. Basic manipulation in image processing
6. Color image compression
![Page 62: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/62.jpg)
62
Color Image Compression
• Color image in Matlab
• General processing of color image
• Color image compression
![Page 63: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/63.jpg)
63
Color Image in Matlab
• Since a color image requires three separate items of information for each pixel, a (true) color image of size m*n is represented in Matlab by an array of size m*n*3: a three dimensional array.
>> x=imread('onion.png');>> size(x)
ans =
135 198 3
![Page 64: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/64.jpg)
64
Color Image in Matlab
• We can isolate each color component by the colon operator:– x(:,:,1) is the first, red component.– x(:,:,2) is the second, green component.– x(:,:,3) is the third, blue component.• The color components can all be viewed with ‘imshow’
function>> figure, imshow(x(:,:,1))>> figure, imshow(x(:,:,2))>> figure, imshow(x(:,:,3))
Red component Green component Blue componentThe RGB components
![Page 65: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/65.jpg)
65
Processing of Color Images
RGB processing: process each RGB matrix separately
Image
GR B
G’R’ B’
Output
Processing
Processing
Processing
![Page 66: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/66.jpg)
66
Framework of Image Compression
Original image DCT
Reminder: Do not forget reprocessing of color images
Quantization
Compressed image
Recovered image IDCT Inverse
quantization
![Page 67: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/67.jpg)
67
An example
>> a = imread('onion.png'); >> a1=a(1:8,1:8,1)
a1 =
63 67 65 68 66 64 63 66 61 61 64 66 67 67 66 66 61 61 64 65 66 65 65 67 65 63 64 65 65 63 66 64 62 60 60 65 66 64 64 64 66 64 63 64 67 67 68 65 62 64 64 67 66 63 64 65 65 60 61 66 66 61 63 66
Red component
![Page 68: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/68.jpg)
68
DCT in Matlab
>> da1=dct2(a1)
da1 =
515.1250 -6.9009 -4.4097 -0.1354 5.6250 0.7751 -1.8265 0.4425 2.2310 -0.9758 -1.7370 -1.8775 -2.6824 -1.3261 1.0516 -1.0729 1.2950 1.6965 -0.8687 -3.0164 1.8362 -1.5076 -0.1527 -1.4253 2.7060 1.6665 -0.1457 1.5836 -2.0586 -1.2816 -1.3506 -1.4155 -2.6250 3.0794 1.0312 -0.4235 2.8750 0.4794 -1.1036 -0.5436 1.9798 3.9761 0.6547 -2.9155 -1.5988 -0.7426 -1.2419 -0.3764 1.4931 0.1617 2.0973 0.9814 0.1865 -0.4356 0.3687 0.4056 -3.0155 -1.2068 -1.9821 -2.1311 1.4400 -1.4382 0.0831 -1.3652
![Page 69: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/69.jpg)
69
Quantization in Matlab
Two different forms of quantization:
1. Uniform scalar quantization:All elements in a matrix are divided by the same number.
2. Non-uniform scalar quantization: All elements in a matrix are divided by different numbers.
There is usually a quantization table for the compression task.
![Page 70: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/70.jpg)
70
Uniform Scalar Quantization
>> q=16;>> qa1=round(da1/q)
qa1 =
32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
![Page 71: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/71.jpg)
71
Non-uniform Scalar Quantization
>> q=[16 11 10 16 24 40 51 61;... 12 12 14 19 26 58 60 55;... 14 13 16 24 40 57 69 56;... 14 17 22 29 51 87 80 62;... 18 22 37 56 68 109 103 77;... 24 35 55 64 81 104 113 92;... 49 64 78 87 103 121 120 101;... 72 92 95 98 112 100 103 99];>> qa1=round(da1./q)qa1 = 32 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
![Page 72: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/72.jpg)
72
Inverse Quantization in Matlab
>> qa2=qa1.*q
qa2 =
512 -11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
![Page 73: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/73.jpg)
73
Inverse DCT in Matlab
>> da2=idct2(qa2)
da2 =
62.0928 62.3832 62.9197 63.6206 64.3794 65.0803 65.6168 65.9072 62.0928 62.3832 62.9197 63.6206 64.3794 65.0803 65.6168 65.9072 62.0928 62.3832 62.9197 63.6206 64.3794 65.0803 65.6168 65.9072 62.0928 62.3832 62.9197 63.6206 64.3794 65.0803 65.6168 65.9072 62.0928 62.3832 62.9197 63.6206 64.3794 65.0803 65.6168 65.9072 62.0928 62.3832 62.9197 63.6206 64.3794 65.0803 65.6168 65.9072 62.0928 62.3832 62.9197 63.6206 64.3794 65.0803 65.6168 65.9072 62.0928 62.3832 62.9197 63.6206 64.3794 65.0803 65.6168 65.9072
![Page 74: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/74.jpg)
74
Difference between Two Matrices
Below is the difference between the original matrix and recovered matrix:
>> diff=double(round(da2))-double(a1)
diff =
-1 -5 -2 -4 -2 1 3 0 1 1 -1 -2 -3 -2 0 0 1 1 -1 -1 -2 0 1 -1 -3 -1 -1 -1 -1 2 0 2 0 2 3 -1 -2 1 2 2 -4 -2 0 0 -3 -2 -2 1 0 -2 -1 -3 -2 2 2 1 -3 2 2 -2 -2 4 3 0
![Page 75: 1 Lab of COMP 319 Lab tutor : Shenghua ZHONG Email: zsh696@gmail.com csshzhong@comp.polyu.edu.hk Lab 2: Nov 9, 2011 Image Processing with MATLAB](https://reader034.vdocuments.net/reader034/viewer/2022052701/56649c755503460f94929aa6/html5/thumbnails/75.jpg)
75
Next Lab Course
Introduction of the final project
Do final project by yourself