Sie sind auf Seite 1von 20

A MATLAB Tour of

Morphological Filtering

EE465: Introduction to Digital Image Processing 1


Dilation

Y  X  B   X b   Bx B  X
bB xX

mask B
Y  X B

>X=zeros(6,7);X(2:3,4:6)=1;X(4:5,3:5)=1;
>se=[0 1;1 1];
>Y=imdilate(X,se);

EE465: Introduction to Digital Image Processing 2


Erosion

Y=X _ B  {x : Bx  X }

Erosion

mask B

X Y=X _ B

>X=zeros(6,7);X(2:3,4:6)=1;X(4:5,3:5)=1;
>se=[0 1;1 1];
>Y=imerode(X,se);

EE465: Introduction to Digital Image Processing 3


Relationship Between Dilation and Erosion

(X _ B)c =XcB^

>se1=[0 1;1 1];


>se2=fliplr(flipud(se1));
>X1=imdilate((1-X),se2)
>X2=1-imerode(X,se1)

EE465: Introduction to Digital Image Processing 4


Opening

X  B  (X _ B) +B

X
>X=zeros(6,9);X(3:5,2:4)=1;
>X(3:5,6:8)=1;X(5,5)=1;X(2,7)=1
mask B >se=strel('square',3);
>Y=imdilate(imerode(X,se),se);
%Y=bwmorph(X,’open’);

X B

EE465: Introduction to Digital Image Processing 5


Closing

X  B  (X + B) _ B

X
>X=zeros(6,9);X(3:5,2:4)=1;
>X(3:5,6:8)=1;X(5,5)=1;X(2,7)=1
mask B >se=strel('square',3);
>Y=imerode(imdilate(X,se),se);
%Y=bwmorph(X,’close’);

X B

EE465: Introduction to Digital Image Processing 6


Relationship Between Opening and Closing

>se=strel('line',10,45);
>se2=fliplr(flipud(se));
( X  B)C  X C  Bˆ >X1=~imopen(X,se);
>X2=imclose(~X, se2);
>isequal(X1,X2)
( X  B)C  X C  Bˆ >X1=~imclose(X,se);
>X2=imopen(~X, se2);
>isequal(X1,X2)

EE465: Introduction to Digital Image Processing 7


Hit-Miss Operator

X * B= (X _ B1)(Xc _ B2)

>bw = [0 0 0 0 0 0;
0 0 1 1 0 0;
0 1 1 1 1 0;
0 1 1 1 1 0;
0 0 1 1 0 0;
X X*B 0 0 1 0 0 0]
origin
>se = [0 -1 -1;
1 1 -1; 0 1 0];
>bw2 = bwhitmiss(bw,se)
mask B1 mask B2
EE465: Introduction to Digital Image Processing 8
Boundary Extraction

X=X-(X _ B)

X X

>X=zeros(8,11);X(2:4,4:8)=1;X(5:7,2:10)=1;
> se=strel('square',3);
Y=X-imerode(X,se);
%Y=imdilate(X,se)-X;
EE465: Introduction to Digital Image Processing 9
Image Example

X X

EE465: Introduction to Digital Image Processing 10


Region Filling

Pseudo Iterations:
Codes expansion stop at the boundary
of
Region Y0=P
Filling
Yk=(Yk-1B)Xc, k=1,2,3…
Terminate when Yk=Yk-1,output YkX

MATLAB
Codes
of
Region
Filling

EE465: Introduction to Digital Image Processing 11


Image Example

Y Z

>se=strel('square',3);
>r=round(size(Y,1)/2);
> c=round(size(Y,2)/2);
>Z=region_fill(Y,[r,c],se);

EE465: Introduction to Digital Image Processing 12


Thinning

x x x x x x
x x
x x x x x x x x
B1 B2 B3 B4 B5 B6 B7 B8

X0=X
Xk=(…( (Xk-1  B1)  B2 …  B8)
where X  B=X – X
*B
Stop the iteration when Xk=Xk-1

Question unanswered on the blackboard:


For B2,,…,B8, do they operate on Xk-1B1 or Xk-1?

EE465: Introduction to Digital Image Processing 13


MATLAB Implementation

function y=thinning(x,iter)

se{1}=[-1 -1 -1;0 1 0;1 1 1]; y=x;z=x; Scheme


se{2}=[0 -1 -1;1 1 -1;1 1 0]; for i=1:iter A
se{3}=fliplr(rot90(se{1})); for k=1:8
se{4}=flipud(se{2}); %y=y&~bwhitmiss(y,se{k});
se{5}=flipud(se{1}); y=y&~bwhitmiss(z,se{k});
se{6}=fliplr(se{4}); end
se{7}=fliplr(se{3}); z=y; Scheme
se{8}=fliplr(se{2}); end B

EE465: Introduction to Digital Image Processing 14


Which One is Right?

Original Scheme Scheme


image A B

EE465: Introduction to Digital Image Processing 15


Result by Using BWMORPH

Scheme
A

Scheme
> y=bwmorph(x,’thin’,inf);
B

EE465: Introduction to Digital Image Processing 16


Summary of Morphological Filtering

MATLAB codes
circshift(A,z)

fliplr(flipud(B))
~A or 1-A
A &~B
imdilate(A,B)
imerode(A,B)

imopen(A,B)

imclose(A,B)

EE465: Introduction to Digital Image Processing 17


Summary (Con’d)

bwhitmiss(A,B)

A&~(imerode(A,B))

region_fill.m

bwmorph(A,’thin’);

EE465: Introduction to Digital Image Processing 18


MATLAB Programming Tip #1

• Use functions provided by MATLAB


• Although we can implement any function from the scratch, the ones offered
by MATLAB are optimized in terms of efficiency and robustness.
• If you write your own function, it is safer to use a different name from the
prioritized MATLAB function (>which -all)

EE465: Introduction to Digital Image Processing 19


MATLAB Programming Tip #2

• Use as few loops as possible


• Example: implementation of histogram calculation
• Scheme 1: loop over every position in the image, i.e., i=1-M, j=1-N
• Scheme 2: loop over every intensity value, i.e., 0-255

EE465: Introduction to Digital Image Processing 20

Das könnte Ihnen auch gefallen