SVD and digital image processing - TFE-Moodle 2 · A digital image differs from a photo in that the...
Transcript of SVD and digital image processing - TFE-Moodle 2 · A digital image differs from a photo in that the...
MATLAB Basics
2
MATLAB’s Workspace• who,whos - current variables in workspace
• save - save workspace variables to *.mat file
• load - load variables from *.mat file
• clear all - clear workspace variables
Matrices in MATLAB• Matrix is a main MATLAB’s data type
• How to build a matrix?
A = [ 1 2 3; 4 5 6; 7 8 9 ];
Creates matrix A with size 3x3.
• Special matrices :
zeros(n,m), ones(n,m),eye (n,m)
MATLAB Basics
3
Basic Operations on Matrices
• All the operators in MATLAB defined on matrices :
+, -, *, /, ^, sqrt, sin, cos, etc.
• Element wise operators defined with preceding dot :
.*, ./, .^ .
• size(A) - size vector
• sum(A) - columns sums vector
• sum(sum(A)) - all the elements sum
Logical Conditions• == , < , > , (not equal) ~= , (not) ~
• find(‘condition’) - Returns indexes of A’s elements that satisfies the
condition.
4
Flow Control• MATLAB has five flow control constructs:– if statements
– switch statements
– for loops
–while loops
– break statementsIF statement condition.
The general form of the IF statement is
IF expression
statements
ELSEIF expression
statements
ELSE
statements
END
Scripts and Functions
5
There are two kinds of M-files:– Scripts, which do not accept input arguments or return output arguments. They
operate on data in the workspace.
–Functions, which can accept input arguments and return output arguments.
Internal variables are local to the function.
FUNCTION Add new function.
• New functions may be added
to MATLAB's vocabulary if they
are expressed in terms of other
existing functions.
Example :
The existence of a file on disk called STAT.M with:function [mean,stdev] = stat(x)
%STAT Interesting statistics.
n = length(x);
mean = sum(x) / n;
stdev = sqrt(sum((x - mean).^2)/n);
end
defines a new function called STAT that calculates
the mean and standard deviation of a vector.
Visualization and Graphics
6
• plot(x,y), plot(x,sin(x)) - plot 1-D function
• figure , figure(k) - open a new figure
• hold on, hold off - refreshing
• mesh(x_ax,y_ax,z_mat) - view surface
• contour(z_mat) - view z as top. map
• subplot(3,1,2) - locate several plots in figure
• axis([xmin xmax ymin ymax]) - change axes
• title(‘figure title’) - add title to figure
What Is the Image Processing
Toolbox?
7
The Image Processing Toolbox is a collection of functions that
extend the capability of the MATLAB ® numeric computing
environment. The toolbox supports a wide range of image
processing operations, including:
– Geometric operations
– Neighborhood and block operations
– Linear filtering and filter design
– Transforms
– Image analysis and enhancement
– Binary image operations
– Region of interest operations
8
A digital image differs from a photo in that the values are all
discrete.
Usually they take on only integer values.
A digital image can be considered as a large array of discrete dots,
each of which has a brightness associated with it. These dots are
called picture elements, or more simply pixels
The pixels surrounding a given pixel constitute its neighborhood
A neighborhood can be characterized by its shape in the same
way as a matrix: we can speak of a 3x3 neighborhood, or of a 5x7
neighborhood.
Images and Digital Images
9
Digital Images
Aspects of Image Processing
10
Image Enhancement: Processing an image so that the result is
more suitable for a particular application. (sharpening or
deblurring an out of focus image, highlighting edges, improving
image contrast, or brightening an image, removing noise)
Image Restoration: This may be considered as reversing the
damage done to an image by a known cause. (removing of blur
caused by linear motion, removal of optical distortions)
Image Segmentation: This involves subdividing an image into
constituent parts, or isolating certain aspects of an image. (finding
lines, circles, or particular shapes in an image, in an aerial
photograph, identifying cars, trees, buildings, or roads.
Types of Digital Images
11
True Color, or RGB: Each pixel has a particular color; that color is described
by the amount of red, green and blue in it. If each of these components has a
range 0–255, this gives a total of 2563 different possible colors. Such an image
is a “stack” of three matrices; representing the red, green and blue values for
each pixel. This means that for every pixel there correspond 3 values.
Types of Digital Images
12
Indexed. Indexed (paletted) images are represented with an index matrix of
size M×N and a colormap matrix of size K×3. The colormap holds all colors used
in the image and the index matrix represents the pixels by referring to colors in
the colormap.
» [x,map] = imread('trees.tif');
» imshow(x,map);
Types of Digital Images
13
Grayscale: Each pixel is a shade of gray, normally from 0 (black) to 255
(white). This range means that each pixel can be represented by eight bits, or
exactly one byte. Other greyscale ranges are used, but generally they are a
power of 2.» image = ind2gray(x,map);
» imshow(image);
Types of Digital Images
14
Binary: Each pixel is just black or white. Since there are only two
possible values for each pixel (0,1), we only need one bit per pixel.
imshow(edge(image));
Image Display
15
image - create and display image object
imagesc - scale and display as image
imshow - display image
colorbar - display colorbar
getimage- get image data from axes
truesize - adjust display size of image
zoom - zoom in and zoom out of 2D plot
Points to Note
16
Pixel values are accessed as matrix elements.
2D Image with intensity values: I(row,col)
2D RGB images I(row,col,color)
- Color : Red = 1; Green = 2 ; Blue = 3
Displaying images : figure, imshow(I)
impixel(i,j) : the command returns the value of the pixel (i,j)
Displaying pixel position and intensity information pixval on
iminfo: Information about the image.
All arithmetic operations performed on matrices may be performed on
images
After processing, an image matrix can be written to an output image
file with the imwrite function : imwrite(I,map,’filename’,’fmt’)
Without the map argument, the image data is supposed to be grayscale
or RGB. The format ‘fmt’ needs to support the particular
type of image.
17
iminfo: Information
about the image
Data Types
18
Image Conversion
19
gray2ind - intensity image to index image
im2bw - image to binary
im2double - image to double precision
im2uint8 - image to 8-bit unsigned integers
im2uint16 - image to 16-bit unsigned integers
ind2gray - indexed image to intensity image
mat2gray - matrix to intensity image
rgb2gray - RGB image to grayscale
rgb2ind - RGB image to indexed image
Bit Planes
20
Greyscale images can be transformed into a
sequence of binary images by breaking them up
into their bit-planes.
• We consider the grey value of each pixel of an
8-bit image as an 8-bit binary word.
• The 0th bit plane consists of the last bit of
each grey value. Since this bit has the least
effect (least significant bit plane).
• The 7th bit plane consists of the first bit in each
value (most significant bit plane.
Initial Image
Bit Plane 0
Bit Plane 4Bit Plane 7
Spatial Resolution
21
Spatial resolution is the density of pixels over the image: the
greater the spatial resolution, the more pixels are used to display the
image.
Halve the size of the image: It does this by taking out every other
row and every other column, thus leaving only those matrix elements
whose row and column indices are even.
Double the size of the image: all the pixels are repeated to produce
an image with the same size as the original, but with half the
resolution in each direction.
22
Interpolation
Extrapolation
23
These operations act by applying a simple function y=f(x) to each gray
value in the image.
Simple functions include adding or subtract a constant value to each
pixel: y = x±C (imadd, imsubtract)
Addition – Subtration
24
Image: J+20Image : J
Subtration
25
Image: J-50
26
Multiplying each pixel by a constant: y = C·x (immultiply, imdivide).
Multiplication
27
Image: Jx3
Division
28
Image: J/2
29
Complement: For a grayscale image is its photographic negative
Complement
30
Image: 255- J
Example Code
31
% Working with Images (example)
[I,map]=imread('trees.tif'); % read a TIFF image
figure, imshow(I,map) % display it as indexed image
I2=ind2gray(I,map); % convert it to grayscale
figure
colormap('gray') % use gray colormap
imagesc(I2,[0 1]) % scale data to use full colormap
% for values between 0 and 1
axis('image') % make displayed aspect ratio
%proportional
% to image dimensions
I=imread(‘moon.jpg'); % read a JPEG image into 3D %array
figure
imshow(I)
rect=getrect; % select rectangle
I2=imcrop(I,rect); % crop
I2=rgb2gray(I2); % convert cropped image to grayscale
imagesc(I2) % scale data to use full colormap
32
% between min and max values in I2
colormap('gray')
colorbar % turn on color bar
pixval % display pixel values interactively
truesize % display at resolution of one %screen pixel
% per image pixel
truesize(2*size(I2)) % display at resolution of two
%screen pixels
% per image pixel
I3=imresize(I2,0.5,'bil'); % resize by 50% using bilinear
% interpolation
I3=imrotate(I2,45,'bil'); % rotate 45 degrees and crop to
% original size
I3=double(I2); % convert from uint8 to double, to %allow
% math operations
imagesc(I3.^2) % display squared image (pixel-wise)
imagesc(log(I3)) % display log of image
MATLAB Resources on the Internet
33
http://www.mathworks.com/products/demos/#
http://www.math.siu.edu/MATLAB/tutorials.html
http://math.ucsd.edu/~driver/21d -s99/MATLAB-primer.html
http://www-cse.ucsd.edu/~sjb/classes/MATLAB/MATLAB.intro.html
http://www.mit.edu/~pwb/cssm/
http://www.mathworks.com
Interesting and very complete tutorials in:
http://www.mathworks.com/academia/student_center/tutorials/launchpad.html
http://www.mathworks.com/matlabcentral/fileexchange
Getting started with MATLAB
http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/learn_matlab.
shtml
MATLAB tutorial
http://www.math.mtu.edu/~msgocken/intro/intro.html
http://amath.colorado.edu/scico/tutorials/matlab/
MATLAB helpdesk
http://www.mathworks.com/access/helpdesk/help/helpdesk.shtml
MATLAB Primer
ftp://ftp.eng.auburn.edu/pub/sjreeves/matlab_primer_40.pdf
34
Link to useful MatLAB tutorials
•Interactive MatLab course
http://www.imc.tue.nl/
•Gatech MatLab tutorial
http://users.ece.gatech.edu/~bonnie/book/TUTORIAL/tutorial.html
•MIT: Introduction to MatLab
http://ocw.mit.edu/courses/electrical-engineering-and-computer-
science/6-094-introduction-to-matlab-january-iap-2010/