Sie sind auf Seite 1von 41

Digital Image Processing Using MATLAB®

Chapter 2

Fundamentals
of
DIP & Matlab

Improve this with www.csie.ntnu.edu.tw/~violet/IP93/Chapter01.ppt


© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com
Digital Image Processing Using MATLAB®

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®
Chapter 2
Fundamentals

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

Image Formats

http://en.wikipedia.org/wiki/Image_formats

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

Displaying Images

>> f = imread(‘rose.tif’);
>> imshow(f)
>> g = imread(‘x-ray.tif’);

>> figure, imshow(g)


>> imshow(f), figure, imshow(g)

>> doc imshow

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

Displaying Images

>> g = imread(‘xray.tif’);
>> imshow(g)
>> imshow(g, [10 100])
>> imshow(g, [ ])

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

Displaying Images Info.


>> f = imread(‘rose.tif’);
>> imshow(f)
>> impixelinfo

>> size(f)
ans =
1024 1024

>> [M, N] = size(f);

>> whos f
Name Size Bytes Class Attributes

r 1024x1024 1048576 uint8

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

Displaying Images Info.


>> imfinfo bubbles.jpg
File size of original image?
ans = Compression ratio?
DPI?
Filename: 'bubble.jpg'
FileModDate: '14-Jan-2008 17:08:08'
Image size?
FileSize: 7904
>> K = imfinfo('bubbles.jpg');
Format: 'jpg'
FormatVersion: ‘'
>> K.FileSize
Width: 720
Height: 688 ans =
BitDepth: 8
ColorType: 'grayscale' 7904
FormatSignature: ‘'
NumberOfSamples: 1
CodingMethod: 'Huffman' Try this
CodingProcess: 'Sequential'
Comment: {} >> imageinfo('rose.tif')
© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com
Digital Image Processing Using MATLAB®

Writing Images
>> b = imread(‘bubbles.tif’);
>> imwrite(b,'bubbles.png')
>> imwrite(b,'bubbles.jpg')
>> imwrite(b,'bubbles', 'jpg')
>> imwrite(b,'bubbles.jpg','quality',50)

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

Writing Images: * -> tif

'compression': 'none'
'packbits'
'ccitt'
'resolution': dpi

>> ckt = imread('cktboard.jpg');


>> imwrite(ckt,'cktboard-1.tif','compression','none','resolution',[300 300])
>> imwrite(ckt,'cktboard-2.tif','compression','none','resolution',300)
>> imwrite(ckt,'cktboard-3.tif','compression','packbits')
>> imshow(ckt)
>> print
© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com
Digital Image Processing Using MATLAB®

Class Exercise 1

Write Matlab code to get the compression


ratio of an image
Hint: compression ratio = actual size in bites / size on disk in bytes
Actual size in bytes = Height x Width x Bit_depth /8

info = imfinfo(‘imagename.ext’);
Compression_ratio = (info.Height x info.Width x info.BitDepth /8) /
info.FileSize;

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

Class Exercise 2

What will be the size of the image in


inches if
a) image of size 450 x 450 is saved with 200 dpi
b) image of size 450 x 450 is saved with 300 dpi

a) 2.25 x 2.25 inches


b) 1.5 x 1.5 inches

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

Data Classes

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

Converting Between
Data Classes

Syntax: B = data_class_name(A)

Example: Suppose

A is an array of class uint8


C is an array of class double

B = double(A)
D = uint8(C)

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

Image Types

1. Intensity images
2. Binary images
3. RGB images
4. Indexed images

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®
Converting Between
Image Classes & Types

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®
Converting Between
Image Classes & Types

Example: Consider the following 2 X 2 image f of class double:

>> f = [-0.5 0.5; 0.75 1.5]


f=
-0.5 0.5
0.75 1.5

>> g = im2uint8(f) >> h = uint8([25 50; 128 200]);


>> g = im2double(h);
g= g=
0 128 0.0980 0.1961
191 255 0.4706 0.7843
© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com
Digital Image Processing Using MATLAB®
Converting Between
Image Classes & Types
Example: Conversion of arbitrary array of class double to an array
of class double scaled to the range [0,1]:

g = mat2gray(A, [Amin, Amax])

>> g = mat2gray(A); i.e., Amin = min(A(:)) & Amax = max(A(:))

?
Example:

>> f = [1 2; 3 4]; Can you formulate such a conversion


>> g = mat2gray(f)
g=
0 0.3333
0.6667 1.0000
© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com
Digital Image Processing Using MATLAB®
Converting Between
Image Classes & Types
Conversion between binary and intensity image types:
Syntax:
g = im2bw(f, T) where T has a range [0, 1]
g = im2bw(f, T) T = 0.5 by default

Reading: Example 2.4

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

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

Guess the output of: v(3:end) v(1:2:end) v(end:-2:1) v([1 4 5])


© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com
Digital Image Processing Using MATLAB®

Matrix Indexing
Example:

>> A = [1 2 3; 4 5 6; 7 8 9] >> A(2:end, end:-2:1)


A= ans =
1 2 3 6 4
4 5 6
7 8 9
9 7

>> A(2, 3) >> C3 = A(:, 3)


ans = C3 =
6 3
6
>> R2 = A(2, :) 9
R2 =
4 5 6
Guess the output of: A(1:2, 1:3) A(end, end-2) A(:, 3) = 0
© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com
Digital Image Processing Using MATLAB®

Matrix Indexing

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

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, :))

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

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

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

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:

Write a program to display the dpi and image size


information of any type of image.

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

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

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

Logical Operators and Functions

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

Functions Returning 1 or 0

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

Variables & Constants

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

Flow Control Statements

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

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));

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

Code Optimization: Example 2.13


function [rt, f, g] = twodsin(A, u0, v0, M, N)
%TWODSIN Compares for loops vs. vectorization.
% The comparison is based on implementing the function
% f(x, y) = Asin(u0x + v0y) for x = 0, 1, 2,...,M-1 and y = 0, 1, 2,...,N-1
% The inputs to the function are M & N and the constants in the function.

% First implement using for loops.

tic % Start timing


for r = 1:M
u0x = u0*(r-1);
for c = 1:N
v0y = v0*(c-1);
f(r,c) = A*sin(u0x + v0y);
end
end
t1 = toc; % End timing.

© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com


Digital Image Processing Using MATLAB®

Code Optimization: Example 2.13


>> R = 0 : 4
% Now implementing using vectorization. R=
% Call the image g. 0 1 2 3 4

>> 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®

Code Optimization: Example 2.13

>> [rt, f, g] = twodsin(1, 1/(4*pi), 1/(4*pi), 512, 512);


rt =
5.8061 function [rt, f, g] = twodsin(A, u0, v0, M, N)
>> imshow(f)
>> imshow(g)

© 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')

Exercise: Write an M-function g = imnorm(f, param)


that accepts an image, f, and a parameter param (either in
lower or upper case) that can have one of two forms:

‘norm1’: To be scaled to the range [0, 1] with output of


class double
‘norm255’: To be scaled to the range [0, 255] with output
of class uint8
© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins www.imageprocessingbook.com

Das könnte Ihnen auch gefallen