Image processing 8
Example 1
Structural elements that can be used in morphological operations:
se1=strel('pair',[3,-2]); % displacement against the central pixel for a vector [3,-2]
figure; imshow(getnhood(se1),
'InitialMagnification', 'fit');
se2=strel('line',10,60); % number of pixels, angle towards x axis
figure; imshow(getnhood(se2), 'InitialMagnification', 'fit');
se3=strel('disk',10); % radius
figure; imshow(getnhood(se3), 'InitialMagnification', 'fit');
se4=strel('diamond',3); % radius
figure; imshow(getnhood(se4), 'InitialMagnification', 'fit');
se5=strel('periodicline',2,[-2,1]); % twice, displacement for a vector [-2,1]
figure; imshow(getnhood(se5), 'InitialMagnification', 'fit');
se6=strel('ball',15,3); % circle radius
figure; imshow(getnhood(se4), 'InitialMagnification', 'fit');
ss=getheight(se4);
ss(isinf(ss))=0;
figure; imshow(mat2gray(ss), 'InitialMagnification', 'fit');
Example 2
Binary image erosion:
L=imread('bacteria.bmp');
figure; imshow(L);
SE=ones([3, 3]);
E=imerode(L,SE);
figure; imshow(E)
L2=L-E; % a difference between the source image and the eroded one (pixels that
have been removed)
figure; imshow(L2);
Exercise 1
Function ‘imdilate’ allows making image dilation. Please use this function for the image
“bacteria.bmp” (binarize image before). Then, show pixels that have been removed.
Exercise 2
Try to use another structural element size, for example 3x1. Conduct the operation five
times.
Exercise 3
Make erosion and dilation using any grayscale and colourful image. The result will be
more visible if we repeat the operation several times.
Example 3
Using dilation and erosion for objects edges detection:
[L1] = imread('bacteria.bmp');
%L1=rgb2gray(L1);
figure; imshow(L1);
SE=ones(3);
gradL1=(imdilate(L1,SE)-imerode(L1,SE));
figure; imshow(gradL1,[]);
[L1] = imread('bacteria.bmp');
%L1=rgb2gray(L1);
figure; imshow(L1);
SE=ones(3);
laplaceL1=(imdilate(L1,SE)+imerode(L1,SE)-2*L1);
figure; imshow(laplaceL1,[]);
Example 4
Closing operation made for a binarized image:
L1 = imread('bacteria.bmp');
L1=L1>128;
figure; imshow(L1);
SE=ones(3);
L2=imclose(L1,SE);
figure; imshow(L2);
Exercise 4
For the same image, please, conduct the operation of opening (‘imopen’) and compare
the results (with example 5). Show the difference of these images.
Exercise 5
Please, check the results of opening and closing for grayscale and colour images. Try
to use another structural element. Show the difference between the resulting images.
Example 5
Grayscale image thinning (different size of structural element):
L1=imread('vessels.jpg');
L1=(L1)<150;
figure; imshow(L1);
L2a=bwmorph(L1,'shrink',5);
figure; imshow(L2a);
L2b=bwmorph(L1,'shrink',10);
figure; imshow(L2b);
L2c=bwmorph(L1,'shrink',40);
figure; imshow(L2c);
Exercise 6
Thinning can be executed also by employing “thin” function, and thickening – using
“thicken”. The structural element size is defined as above. Please, try to use these
functions for the image “drawing.bmp”.
Example 6
Image skeletonization with “branches” cutting and elimination of the isolated pixels:
L1=imread('vessels.jpg');
L1=(L1)<150;
figure; imshow(L1);
L2=bwmorph(L1,'skel',Inf);
figure; imshow(L2);
L3=bwmorph(L2,'spur',Inf);
figure; imshow(L3);
L4=bwmorph(L3,'clean',Inf);
figure; imshow(L4);
Exercise 7
„Bwmorph” enables to conduct several operations, for example: 'bridge' (joining
pixels with the bridge), 'hbreak' (removing of junctions in the shape of H letter),
'remove' (inner pixels removing), 'fill’ (1-pixel holes filling or isolated black pixels
removing). Please try to use this function with different arguments. Use images
“drawing.bmp” and “vessels. jpg”.
Example 7
Contours detection:
[L1]=imread('cells1.bmp');
L1=L1<180;
figure; imshow(L1);
SE=ones([3,3]);
E=imerode(L1,SE);
L2=L1-E;
figure; imshow(L2);
Another way:
L3 = bwperim(L1,8); % neighbourhood type
figure; imshow(L3)
Exercise 8
Please, try to find contours in any image with distinct edges, for example
„Krakow_3.jpg”.
Example 8
Detection of pixels exact configuration (here: horizontal lines, 10 pixels long):
L1=imread('drawing.bmp');
figure; imshow(L1);
SE=ones([1 10]);
L2=bwhitmiss(L1,SE);
figure; imshow(L2);
Example 9
In the “hit-and-miss” operation the structural element has mixed character. While
construct it, remember, that:
• pixels, that should be hit (pixels of an object) should be marked with 1
• pixels, that should be missed (pixels of a background) should have value of -1
• pixels, that are not taken into consideration (insignificant) – are marked with 0.
In the example below, the structural element was used that indicates object’s corners.
L1=imread('vessels1.bmp');
figure; imshow(L1, 'InitialMagnification', 'fit');
SE=[0,1,0;-1,1,1;-1,-1,0]
L2=bwhitmiss(L1,SE);
figure; imshow(L2, 'InitialMagnification', 'fit');
Exercise 9
Try to find different pixels’ configurations employing “hit-and-miss” structural element.
Use any binary image with low resolution.
x 1 x
0 1 1
0 0 x