Beruflich Dokumente
Kultur Dokumente
Chapter 2
Fundamentals
of
DIP & Matlab
Image Formats
http://en.wikipedia.org/wiki/Image_formats
Displaying Images
>> f = imread(‘rose.tif’);
>> imshow(f)
>> g = imread(‘x-ray.tif’);
Displaying Images
>> g = imread(‘xray.tif’);
>> imshow(g)
>> imshow(g, [10 100])
>> imshow(g, [ ])
>> size(f)
ans =
1024 1024
>> whos f
Name Size Bytes Class Attributes
Writing Images
>> b = imread(‘bubbles.tif’);
>> imwrite(b,'bubbles.png')
>> imwrite(b,'bubbles.jpg')
>> imwrite(b,'bubbles', 'jpg')
>> imwrite(b,'bubbles.jpg','quality',50)
'compression': 'none'
'packbits'
'ccitt'
'resolution': dpi
Class Exercise 1
info = imfinfo(‘imagename.ext’);
Compression_ratio = (info.Height x info.Width x info.BitDepth /8) /
info.FileSize;
Class Exercise 2
Data Classes
Converting Between
Data Classes
Syntax: B = data_class_name(A)
Example: Suppose
B = double(A)
D = uint8(C)
Image Types
1. Intensity images
2. Binary images
3. RGB images
4. Indexed images
?
Example:
Array Indexing
Example:
>> w = v.’
>> v = [1 3 5 7 9] w=
v= 1
1 3 5 7 9 3
>> v(2) 5
ans = 7
3 9
>> v(2:4)
ans =
3 5 7
Matrix Indexing
Example:
Matrix Indexing
Matrix Indexing
a.
1024 X 1024 intensity image f of
class uint8
b.
>> fp = f(end:-1:1, :);
c.
>> fc = f(257:768, 257:768);
d.
>> fs = f(1:2:end, 1:2,end);
e.
>> plot(f(512, :))
>> plot(f(size(f, 1)/2, :))
Standard Arrays
>> A = 5*ones(3, 3)
A=
5 5 5
5 5 5
5 5 5
>> magic(3)
ans =
8 1 6
3 5 7
4 9 2
>> B = rand(2, 4)
B=
0.2311 0.4860 0.7621 0.0185
0.6068 0.8913 0.4565 0.8214
© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com
Digital Image Processing Using MATLAB®
M-Function Programming
Components
1. The function definition line: function [outputs] = name(inputs)
2. The H1 line
3. Help text
4. The function body
5. Comments
M-Function Programming
function [p, pmax, pmin, pn] = improd(f, g) >> help improd
%IMPROD Computes the product of two images.
% [P, PMAX, PMIN, PN] = IMPROD(F, G) >> f = [1 2; 3 4];
>> g = [1 2; 2 1];
fd = double(f); >> [p, pmax, pmin, pn] = improd(f, g)
gd = double(g); p=
p = fd.*gd; 1 4
pmax = max(p(:)); 6 4
pmin = min(p(:)); pmax =
pn = mat2gray(p); % Scaled to [0, 1] 6
pmin =
% The End 1
pn =
0 0.6000
1.0000 0.6000
© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com
Digital Image Processing Using MATLAB®
M-Function Programming
Exercise:
Operators
Example: >> A = [1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
f= 4 5 6
1 2 7 8 9
3 4
>> A(2:end, end:-2:1)
ans =
>> v = f(:) 6 4
9 7
v= >> C3 = A(:, 3)
1 C3 =
3 3
6
2 9
4
© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com
Digital Image Processing Using MATLAB®
Arithmetic Operations
Example:
>> imshow(imcomplement(f))
© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com
Digital Image Processing Using MATLAB®
Relational Operators
Example:
>> A = [1 2; 3 4]
A=
1 2
3 4
>> B = [0 2; 3 5;]
B=
0 2
3 5
>> A>=B
ans =
1 1
1 0
Functions Returning 1 or 0
Code Optimization
1. Vectorizing loops
2. Preallocating arrays
Example: Suppose we want to generate a 1-D
function: >> A=2; M=4;
>> x=0:M-1
f(x) = A sin(x/2) for x = 0, 1, 2,…, M-1 x=
0 1 2 3
Method 1: for x = 1:M >> f = A*sin( x / (2*pi) )
f(x) = A*sin((x-1)/(2*pi)); f=
end 0 0.3170 0.6259 0.9191
Method 2: x = 0:M-1;
f = A*sin(x/(2*pi));
>> C = 0 : 3
tic % Start timing C=
r = 0:M-1; 0 1 2 3
c = 0:N-1;
[C, R] = meshgrid(c, r); >> [C,R] = meshgrid(C,R)
C=
g = A*sin(u0*R + v0*C); 0 1 2 3
t2 = toc; % End timing. 0 1 2 3
0 1 2 3
% Compute the ratio of the two times. 0 1 2 3
0 1 2 3
R=
rt = t1/(t2+eps); % Use eps in case t2 is close to 0. 0 0 0 0
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com
Digital Image Processing Using MATLAB®
Interactive I/O
>> t = input('Enter your data: ', 's')
Enter your data: 2 3 4
t=
>> t = input('Enter your data: ', 's')
234
Enter your data: 12.6, x2y, z
>> class(t)
t=
ans =
12.6, x2y, z
char
>> [a, b, c] = strread(t, '%f%q%q', 'delimiter', ',')
n = str2num(t)
a=
>> class(n)
12.6000
ans =
b=
double
'x2y'
>> n(2)
c=
ans =
'z'
3
>> doc strread
>> doc input
© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com
Digital Image Processing Using MATLAB®
String Comparison
Function strcmp is used to compare strings.
Syntax: TF = strcmp('str1', 'str2')