Upload
dinhdat
View
232
Download
6
Embed Size (px)
Citation preview
Uniq Technologies
Image Processing using MATLAB
Introduction
• MATLAB is a high-level language and interactive environment for
numerical computation, visualization, and programming. Using MATLAB,
you can analyze data, develop algorithms, and create models and
applications
• MATLAB is intended primarily for numerical computing, an optional
toolbox allowing access to symbolic computing capabilities. An additional
package, Simulink, adds graphical multi-domain simulation and Model-
Based Design for dynamic and embedded systems
Matrices in Matlab
• Defining a matrix is similar to defining a vector. Creating a matrix is as easy
as making a vector, using semicolons (;) to separate the rows of a matrix
A = [ 1 2 3; 3 4 5; 6 7 8]
• You can also treat it like a row of column vectors
B = [ [1 2 3]' [4 5 6]' [7 8 9]']
Matrix Declaration and Initialization
Transpose of the matrix A C = A’
Let’s try the list of Functions yourself…
1. X = inv(A); inverse of a matrix
2. I = inv(A) * A; inverse is the identity matrix
3. eig(A); [v,e] = eig(A); diag(e);
Eigen values and the Eigen vectors.
Probability density functions:
Y = pdf(name , X, A, B, C);
Eg: p1 = pdf('Normal',-2:2, 0,1)
p2 = pdf('Poisson', 0:4, 1:5)
What is Matlab?
• Matlab is basically a high level language which has many specialized
toolboxes for making things easier for us
Assembly
High Level Languages such as
C, Pascal etc.
Matlab
Matlab tool
• Command Window
– type commands
• Current Directory
– View folders and m-files
• Workspace
– View program variables
– Double click on a variable
to see it in the Array Editor
• Command History
– view past commands
– save a whole session using diary
Use of M-File
Click to create a new
M-File
• Extension “.m”
• A text file containing script or function or program to run
• Go to , File New Script or Function
Use of M-File
If you include “;” at the
end of each statement,
result will not be shown
immediately
Project Deployment
Windows Application
JAVA Application
Dotnet Application
Matlab can be interface with object oriented programming
IMAGE PROCESSING
TOOLBOX
Image file process in MATLAB
MATLAB can import/export several image formats
BMP (Microsoft Windows Bitmap)
GIF (Graphics Interchange Files)
HDF (Hierarchical Data Format)
JPEG (Joint Photographic Experts Group)
PCX (Paintbrush)
PNG (Portable Network Graphics)
TIFF (Tagged Image File Format)
XWD (X Window Dump)
MATLAB can also load raw-data or other
types of image data
Data conversion types in MATLAB
Double (64-bit double-precision floating
point)
Single (32-bit single-precision floating
point)
Int32 (32-bit signed integer)
Int16 (16-bit signed integer)
Int8 (8-bit signed integer)
Uint32 (32-bit unsigned integer)
Uint16 (16-bit unsigned integer)
Uint8 (8-bit unsigned integer)
Image import and export
• Read and write images in Matlab
I=imread('cells.jpg');
imshow(I);
size(I);
ans = 479 600 3 (RGB image)
Igrey=rgb2gray(I); (RGB to gray)
imshow(Igrey);
imwrite(lgrey, 'cell_gray.tif', 'tiff');
Read images in Matlab from user directory
file= 'C:\\MATLAB\\VideoImage\\';
imageList = dir(fullfile(file,'*.jpg'));
Im= imread([file,imageList(4).name]);
Images and Matrices
How to build a matrix (or image)?
A = [ 1 2 3; 4 5 6; 7 8 9 ];
A = 1 2 3
4 5 6
7 8 9
B = zeros(3,3); B = 0 0 0
0 0 0
0 0 0
C = ones(3,3) ; C = 1 1 1
1 1 1
1 1 1
imshow(A) (imshow(A,[]) to get automatic pixel range)
Image Conversion
Im = imread('fr2.jpg');
Im2 = ones(size(Im));
Im3 = zeros(size(Im));
im4=rgb2gray(Im);
Image Function Image= rgb2hsv(Im); (RGB to HSV model)
Image= im2bw(Im,0.5); (Image to Binary)
• Image Noising :
Image= imnoise (Im,'salt & pepper');
Image= imnoise (Im,‘Gaussain');
Image Type Conversion
1. gray2ind(X, map); Convert grayscale or binary image to indexed image
2. ind2gray(X, map) ; Convert indexed image to grayscale image. Map-
color intensity “n” color values
3. ind2rgb(I, map) Convert indexed image to RGB image.
4. rgb2gray(I); Convert RGB image or colormap to grayscale
5. mat2gray(I, [amin, amax]); Convert matrix to grayscale image
6. im2bw(I, map, level); Convert image to binary image, based on
threshold
7. graythresh(I); Global image threshold using Otsu's method
8. multithresh(I, n); Multilevel image thresholds using Otsu ´s method
9. grayslice(I, v); Convert grayscale image to indexed image using
multilevel thresholding. vector ‘v’ values between 0 and 1
10. im2double(I) or im2double(BW) or im2double(X, index); Convert
image to double precision
Subplot
subplot - display multiple plots in the same window
subplop(nrows,ncols,plot_number)
code
x=0:.1:2*pi;subplot(2,2,1);plot(x,sin(x));
subplot(2,2,2);plot(x,cos(x));
subplot(2,2,3)plot(x,exp(-x));subplot(2,2,4);plot(peaks);
Subplot
Filtering
Function
Y= filter2(filter,image,shape)
Description
returns the part of Y specified by the shape parameter. Shape is a
string with one of these values:
'full' Returns the full two-dimensional correlation. In this case, Y is larger
than X
'same' (default) Returns the central part of the correlation. In this case, Y
is the same size as X
'valid' Returns only those parts of the correlation that are computed
without zero-padded edges. In this case, Y is smaller than X
Filtering code
• >> clear all
>> a=imread('cute01.tif');
>> imshow(a);
>> Fil=fspecial('average');% function fspecial is one(3,3)/9
>> aFilter=filter2(Fil,a);
>> imshow(aFilter/255);
Filtering image
RGB CHANNEL CONVERSION
a=imread('image2.jpg');
subplot(3,1,1);
imshow(a);
aa=rgb2gray(a);
b=imnoise(aa,'salt & pepper',0.02);
subplot(3,1,2);
imshow(b);
bb=filter2(fspecial('average', [3 3], 255));
c=medfilt(b,[3 3]);
subplot(3,1,3);
imshow(c);
Code
OUTPUT OF CHANNEL CONVERSION
EDGE DETECTION
Edge detection is the process of localizing pixel intensity transitions. The
edge detection have been used by object recognition, target tracking,
segmentation, and etc. Therefore, the edge detection is one of the most
important parts of image processing.
There mainly exists several edge detection methods (Sobel [1,2], Prewitt
[3], Roberts [4],Canny [5]).
CODE
a=imread('image2.jpg');
subplot(3,2,1);imshow(a);title('original image');
aa=rgb2gray(a);
b=edge(aa,'sobel');
subplot(3,2,2);imshow(b);title('sobel edge');
c=edge(aa,'canny');
subplot(3,2,3); imshow(c);title('canny edge');
d=edge(aa,'prewitt');
subplot(3,2,4);imshow(d);title('prewitt edge');
e=edge(aa,'roberts');
subplot(3,2,5);imshow(e);title('roberts');
EDGE DETUCTION OUTPUT
Functions in matlab
• Predefined functions
Matlab has a variety of pre-defined functions readily available for use
by simply using their names and providing the right number and type of
arguments. For example, the natural logarithm function (log) is readily
available:
» log(-5.2) ans = 1.6487 + 3.1416i
• User defined function
• Function[output arguments] = functionname[input arguments ]
ex: Function [a]=plot[x,y]
a=7;
y=sin(o.5:.1:50) ;
end
Graphical user interface
What is GUIDE?
• GUIDE is Matlab’s Graphics User Interface (GUI)
Design Environment GUIDE stores GUIs in two files, which are generated
the first time you save or run the GUI:– .fig file - contains a complete
description of the GUI figure layout and the components of the GUI •
Changes to this file are made in the Layout Editor – .m file - contains the
code that controls the GUI • You can program the callbacks in this file
using the M-file Editor
Creating a GUI
Typical stages of creating a GUI are:
1. Designing the GUI
2. Laying out the GUI
- Using the Layout Editor
3. Programming the GUI
- Writing callbacks in the M-file Editor
4. Saving and Running the GUI
Writing Callbacks
A callback is a sequence of commands that are execute when a graphics
object is activated
• Stored in the GUI’s M-file
• Is a property of a graphics object (e.g. CreateFnc, ButtonDwnFnc,Callback, DeleteFnc)
• Also called event handler in some programming languages A callback is usually made of the following stages:
1. Getting the handle of the object initiating the action (the objectprovides event / information / values)
2. Getting the handles of the objects being affected (the object that whose properties are to be changed)
3. Getting necessary information / values
4. Doing some calculations and processing
5. Setting relevant object properties to effect action
Writing Callbacks
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename pathname]=uigetfile({'*.jpg';'*.tif'},'File Selector');
image=strcat(pathname, filename);
axes(handles.axes1);
imshow(image)
set(handles.edit2,'string',filename);
set(handles.edit1,'string',pathname);
Image browser
Push button handles
Slider value example
Call back function
function slider1_Callback(hObject, eventdata, handles)
a=get(handles.slider1,'Value');
x=0:0.1:50;
y=sin(x*a);
plot(handles.axes1,x,y)
Slider value
Increasing slider value
Property inspector
• To change the properties
of GUI
Queries & Clarifications
Happy Learning