Morphological Image Processing Examples 1 Colorado School of Mines

advertisement
Morphological Image Processing
Examples
1
Colorado School of Mines
Department of Electrical Engineering and Computer Science
Example 1
• Estimate the
number of balls
(the number
won’t be exact)
• You will have to
shrink the balls
so that they
don’t touch
2
Colorado School of Mines
Department of Electrical Engineering and Computer Science
Example 1 (continued)
clear all
close all
I = imread('balls.gif');
imshow(I,[])
B = im2bw(I, graythresh(I));
B = ~B;
% Complement image
S = strel('disk', 5, 0);
I2 = imerode(B,S);
figure, imshow(I2);
[L, n] = bwlabel(I2);
blobs = regionprops(L);
for i=1:n
rectangle('Position', blobs(i).BoundingBox, 'EdgeColor', 'r');
end
Found 1647 balls
3
Colorado School of Mines
Department of Electrical Engineering and Computer Science
Example 1 (continued)
• Plot the
density of the
balls
– As a
function of x
– As a
function of y
4
Colorado School of Mines
Department of Electrical Engineering and Computer Science
Example 1 (continued)
allCentroids = cat(1,blobs(:).Centroid);
% Get all centroids
% Plot distribution of centroids
histX = hist(allCentroids(:,1));
figure, plot(histX)
histY = hist(allCentroids(:,2));
figure, plot(histY)
200
240
190
220
180
200
170
160
180
150
160
140
140
120
130
120
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
5
Colorado School of Mines
Department of Electrical Engineering and Computer Science
Example 2
• Task:
– Segment coins from the
background
– Namely, generate a binary (or
“logical”) image which is white
(1) where there are coins, and
black (0) elsewhere
– Use morphological operators so
that:
Image “eight.tif”
• No gaps in the coins
• No extraneous white pixels in the
background
Colorado School of Mines
Department of Electrical Engineering and Computer Science
6
clear all
close all
Example 2 (continued)
I = imread('eight.tif');
imshow(I,[]);
B = im2bw(I, graythresh(I));
B = imcomplement(B);
S = strel('disk',1);
B1 = imopen(B,S);
% threshold
% we want black regions
% define a small structuring element
% get rid of small white regions
S = strel('disk', 5, 0);
% Need structuring element bigger than gaps
B2 = imclose(B1,S);
% Fill in gaps
figure, imshow(B2);
[L,n] = bwlabel(B2);
fprintf('n = %d\n', n);
figure, imshow(L, []);
RGB = label2rgb(L);
figure, imshow(RGB);
Colorado School of Mines
% find connected components
% create false color image for visualization
Department of Electrical Engineering and Computer Science
7
thresholded image, after
morphological operations
original image
labeled image
Colorado School of Mines
labeled image
with false colors
Department of Electrical Engineering and Computer Science
8
Example 3
• The image “xray.jpg” is an X-ray image of a chicken nugget with
some bone fragments inside (Figure 9.18 from the textbook).
Create a binary image using the Matlab command “B=I>200” (a little
later in the course we will see how to pick thresholds automatically).
• Apply the Matlab function bwlabel to find connected components.
How many components are there?
9
Colorado School of Mines
Department of Electrical Engineering and Computer Science
Example 3 (continued)
• Get rid of the tiny “noise” blobs by opening the image with a disk
structuring element of radius 1. Now how many components are
there?
• An automatic inspection will reject the nugget if the total area of all
large fragments (larger than 100 pixels) is more than 1000 pixels.
• Using the opened image from step 2, find all blobs with individual
areas greater than 100 pixels, and draw a rectangle around each
large blob that was found.
• What is the total area of the large blobs?
10
Colorado School of Mines
Department of Electrical Engineering and Computer Science
clear all
close all
I = imread('xray.jpg');
imshow(I, []);
B = I>200;
figure, imshow(B, []);
[L,n] = bwlabel(B);
fprintf('n = %d\n', n);
SE = strel('disk',1);
B = imopen(B,SE);
figure, imshow(B, []);
My answers:
Initial number of components = 145
Number after eliminating small components = 18
Total area of large components = 5678.000000
[L,n] = bwlabel(B);
fprintf('n = %d\n', n);
figure, imshow(L, []);
blobs = regionprops(L);
% Look at blob areas
areas = cat(1,blobs(:).Area);
indices = find(areas > 100);
% indices of large blobs
blobsLarge = blobs(indices);
% All large blobs
areasLarge = cat(1,blobsLarge(:).Area);
areaTotal = sum(areasLarge);
fprintf('Total area = %f\n', areaTotal);
for i=1:length(blobsLarge)
rectangle('Position', blobsLarge(i).BoundingBox, 'EdgeColor', 'r');
end
11
Colorado School of Mines
Department of Electrical Engineering and Computer Science
Download