A MATLAB Tour of Morphological Filtering EE 465
A MATLAB Tour of Morphological Filtering EE 465: Introduction to Digital Image Processing 1
Dilation mask B X >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); EE 465: Introduction to Digital Image Processing 2
Examples bw = imread('text. png'); se = strel('line', 11, 90); bw 2 = imdilate(bw, se); imshow(bw), title('Original') figure, imshow(bw 2), title('Dilated') EE 465: Introduction to Digital Image Processing 3
Erosion Y=X _ B Erosion mask B Y=X _ B X >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); EE 465: Introduction to Digital Image Processing 4
Example: original. BW = imread('circles. png'); se = strel('disk', 11); eroded. BW = imerode(original. BW, se); imshow(original. BW), figure, imshow(eroded. BW) EE 465: Introduction to Digital Image Processing 5
Relationship Between Dilation and Erosion (X _ B)c =Xc B^ >se 1=[0 1; 1 1]; >se 2=fliplr(flipud(se 1)); >X 1=imdilate((1 -X), se 2) >X 2=1 -imerode(X, se 1) EE 465: Introduction to Digital Image Processing 6
Opening (X _ B) +B X mask B >X=zeros(6, 9); X(3: 5, 2: 4)=1; >X(3: 5, 6: 8)=1; X(5, 5)=1; X(2, 7)=1 >se=strel('square', 3); >Y=imdilate(imerode(X, se); %Y=bwmorph(X, ’open’); EE 465: Introduction to Digital Image Processing 7
Closing _ (X + B) B X mask B >X=zeros(6, 9); X(3: 5, 2: 4)=1; >X(3: 5, 6: 8)=1; X(5, 5)=1; X(2, 7)=1 >se=strel('square', 3); >Y=imerode(imdilate(X, se); %Y=bwmorph(X, ’close’); EE 465: Introduction to Digital Image Processing 8
Relationship Between Opening and Closing >se=strel('line', 10, 45); >se 2=fliplr(flipud(se)); >X 1=~imopen(X, se); >X 2=imclose(~X, se 2); >isequal(X 1, X 2) >X 1=~imclose(X, se); >X 2=imopen(~X, se 2); >isequal(X 1, X 2) EE 465: Introduction to Digital Image Processing 9
Hit-Miss Operator (X _ B 1) (Xc _ B 2) X * B= X mask B 1 origin X*B >bw = [0 0 0 0; 0 0 1 1 0 0; 0 1 1 1 1 0; 0 0 1 1 0 0; 0 0 1 0 0 0] >se = [0 -1 -1; 1 1 -1; 0 1 0]; >bw 2 = bwhitmiss(bw, se) mask B 2 EE 465: Introduction to Digital Image Processing 10
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; EE 465: Introduction to Digital Image Processing 11
Image Example X X EE 465: Introduction to Digital Image Processing 12
Region Filling Pseudo Codes of Region Filling Iterations: expansion stop at the boundary Y 0=P Yk=(Yk-1 B) Xc, k=1, 2, 3… Terminate when Yk=Yk-1, output Yk X MATLAB Codes of Region Filling EE 465: Introduction to Digital Image Processing 13
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); EE 465: Introduction to Digital Image Processing 14
Thinning x x x B 1 B 2 x B 3 x x B 4 x B 5 x x B 6 x B 7 x x B 8 X 0=X Xk=(…( (Xk-1 B 1) B 2 … B 8) where X B=X – X * B Stop the iteration when Xk=Xk-1 Question unanswered on the blackboard: For B 2, , …, B 8, do they operate on Xk-1 B 1 or Xk-1? EE 465: Introduction to Digital Image Processing 15
MATLAB Implementation function y=thinning(x, iter) se{1}=[-1 -1 -1; 0 1 0; 1 1 1]; se{2}=[0 -1 -1; 1 1 0]; se{3}=fliplr(rot 90(se{1})); se{4}=flipud(se{2}); se{5}=flipud(se{1}); se{6}=fliplr(se{4}); se{7}=fliplr(se{3}); se{8}=fliplr(se{2}); y=x; z=x; for i=1: iter for k=1: 8 %y=y&~bwhitmiss(y, se{k}); y=y&~bwhitmiss(z, se{k}); end z=y; end EE 465: Introduction to Digital Image Processing Scheme A Scheme B 16
Which One is Right? Original image Scheme A EE 465: Introduction to Digital Image Processing Scheme B 17
Result by Using BWMORPH Scheme A Scheme B > y=bwmorph(x, ’thin’, inf); EE 465: Introduction to Digital Image Processing 18
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) EE 465: Introduction to Digital Image Processing 19
Summary (Con’d) bwhitmiss(A, B) A&~(imerode(A, B)) region_fill. m bwmorph(A, ’thin’); EE 465: Introduction to Digital Image Processing 20
MATLAB Programming Tip #1 l 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. EE 465: Introduction to Digital Image Processing 21
MATLAB Programming Tip #2 l l 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 EE 465: Introduction to Digital Image Processing 22
- Slides: 22