Graphics & Image Processing

Tutorials 8 & 9: Morphological Image Processing

Developed by Dr Valentina Plekhanova

 

I. Matlab Section

I-1.

Use the Matlab help system to read about the following commands: strel, imopen, imclose

I-2.

Reminder: impixel can return values and/or coordinates (in ROW/COL)

Use the Matlab help system to read more about this command.

 

II-1. Examples

Make m-files

 

II-1.

function MorDisk

% Create morphological structuring element with a disk of the given radious

clear, close all,

A = imread('bean.jpg');

A=rgb2gray(A);

BW = edge(A,'sobel',0.04);

imshow(BW);

Title('BW Image');

[L,num] = bwlabel(BW,8); % Label components

RGB=label2rgb(L);

figure, imshow(RGB);

Title('RGB Image');

num

figure,

subplot(2,2,1), imshow(A);

Title('Original');

% Perform a morphological opening operation by calling imopen with a disk-shaped

% structuring element with radiouses of 10, 25, 30, 40.

% The structuring element is created by the strel function.

% The morphological opening has the effect of removing objects that cannot

% completely contain a disk of the given radious.

% try RL=imopen(A,strel('disk', 40));

% RL=imopen(A,strel('disk', 25));

RL=imopen(A,strel('disk', 3));

subplot(2,2,2), imshow(RL)

Title('RL Image');

Explain the program and results.

 

II-2.

function MorSquare

% Create morphological structuring element with a sguare

clear, close all,

A = imread('bean.jpg');

A=rgb2gray(A);

BW = edge(A,'sobel',0.04);

imshow(BW);

Title('BW Image');

[L,num] = bwlabel(BW,8); % Label components

RGB=label2rgb(L);

figure, imshow(RGB);

Title('RGB Image');

num

figure,

subplot(2,2,1), imshow(A);

Title('Original');

% Perform a morphological opening operation by calling imopen with a sguare structuring element

% SE = strel('square',W) creates a square structuring element whose width is W pixels. W must be a nonnegative integer scalar.

% try RL = imopen(A,strel('square',100));

% RL = imopen(A,strel('square',10));

RL = imopen(A,strel('square',40));

subplot(2,2,2), imshow(RL)

Title('RL Image');

Explain the program and results.

 

II-3.

function TestMor

% Step 1: Threshold the image

A = imread('bean.jpg');

BW = ~im2bw(A,graythresh(A));

% try BW = im2bw(A,graythresh(A));

imshow(A), title('Original')

figure, imshow(BW);

Title('Step 1: Thresholded Image')

%Step 2: Create morphological structuring element with a disk-shaped

% structuring element with a given radius

MR = strel('disk',6);

% Step 3: Close with a disk of radius 6 to merge

% together small features that are close together.

BW2 = imclose(BW,MR);

figure, imshow(BW2);

Title('Step 3: Closing')

% Step 4: Follow with an opening to remove the isolated white pixels.

BW3 = imopen(BW2,MR);

figure, imshow(BW3);

Title('Step 4: Opening')

Explain the program and results.

II-4.

function MorNum

% Step 1: Threshold the image

A = imread('bean.jpg');

BW = ~im2bw(A,graythresh(A));

imshow(A), title('Original')

figure, imshow(BW);

Title('Step 1: Thresholded Image')

%Step 2: Create morphological structuring element with a disk-shaped

% structuring element with a given radius

MR = strel('disk',6);

% Step 3: Close with a disk of radius 6 to merge

% together small features that are close together.

BW2 = imclose(BW,MR);

figure, imshow(BW2);

Title('Step 3: Closing')

% Step 4: Follow with an opening to remove the isolated white pixels.

BW3 = imopen(BW2,MR);

figure, imshow(BW3);

Title('Step 4: Opening')

% Determine the Number of Objects in the Image

[L,num] = bwlabel(BW2,8);

% compare the number of beans on the image with num that you have received

% after opening process

num

STATS = regionprops(L,'BoundingBox','MajorAxisLength');

for j=1:num

if STATS(j).MajorAxisLength>100

maxobject=j;

end

end

maxobject

boxsize=STATS(maxobject).BoundingBox;

boxsize

xb1 = round(boxsize(1));

xb2= round(boxsize(1)+boxsize(3));

yb1=round(boxsize(2));

yb2= round(boxsize(2)+boxsize(4));

BWW=BW(yb1:yb2, xb1:xb2);

figure, imshow(BWW)

Explain the program and results.

 

II-5.

function MorColor

A = imread('bean.jpg');

BW = ~im2bw(A,graythresh(A));

imshow(A), title('Original')

figure, imshow(BW);

Title('Step 1: Thresholded Image')

% Step 2: Create morphological structuring element

MR = strel('disk',6);

BW2 = imclose(BW,MR);

figure, imshow(BW2);

Title('Step 3: Closing')

BW3 = imopen(BW2,MR);

figure, imshow(BW3);

Title('Step 4: Opening')

[L,num] = bwlabel(BW2,8);

num

% Now draw the outline profile (note that an outline profile is a graphic summary of the object presented by the line by which the object is defined or bounded; contour)

imwrite(BW2,'BW.jpg'); % saves the image

D = imread('BW.jpg');

ED = edge(D,'sobel'); % creates a binary image using the Sobel approximation

imshow(ED); % displays image

Title('Outline profiles - Image');

%

RGB=label2rgb(L);

figure, imshow(RGB);

Title('RGB Image');

Explain the program and results.

 

II-6. Run the following program with the processes: Closing-Opening, i.e.

function MorCO

A = imread('Egik.jpg');

BW = ~im2bw(A,graythresh(A));

MR = strel('disk',6);

BW2 = imclose(BW,MR);

BW3 = imopen(BW2,MR);

figure,

subplot (2,2,1), imshow(A);

Title('Original')

subplot(2,2,2), imshow(BW);

Title('Step 1: Thresholded Image')

% Note that Step 2: Create morphological structuring element

subplot(2,2,3), imshow(BW2);

Title('Step 3: Closing')

subplot(2,2,4), imshow(BW3);

Title('Step 4: Opening')

[L,num] = bwlabel(BW2,4);

num

Explain the program and results.

 

II-7. Now we modify and run the program II-6 with the processes: Opening-Closing, i.e.

function MorOC

A = imread('Egik.jpg');

BW = ~im2bw(A,graythresh(A));

MR = strel('disk',6);

BW2 = imopen(BW,MR);

BW3 = imclose(BW2,MR);

figure,

subplot (2,2,1), imshow(A);

Title('Original')

subplot(2,2,2),imshow(BW);

Title('Step 1: Thresholded Image')

% Note that Step 2: Create morphological structuring element

subplot(2,2,3),imshow(BW2);

Title('Step 3: Opening')

subplot(2,2,4), imshow(BW3);

Title('Step 4: Closing')

[L,num] = bwlabel(BW2,4);

num

Explain the program and results.

 

II-8. Run the following program with the processes: Closing-Opening, i.e.

function MorCOBean

A = imread('bean.jpg');

BW = ~im2bw(A,graythresh(A));

MR = strel('disk',6);

BW2 = imclose(BW,MR);

BW3 = imopen(BW2,MR);

[L,num] = bwlabel(BW2,4);

num

% find All Beans

beandata=regionprops(L,'basic');

allbeans=[beandata.Area];

% find max & min Beans

maxbean=max(allbeans);

maxbean

minbean=min(allbeans);

minbean

% find Big Bean

bigbean=find(allbeans==maxbean);

bigbean

% find Small Bean

smallbean=find(allbeans==minbean);

smallbean

Explain the program and results (find in Matlab Command Window).

 

II-9. Now we modify and run the program II-8 with the processes: Opening-Closing, i.e.

function MorOCBean

A = imread('bean.jpg');

BW = ~im2bw(A,graythresh(A));

MR = strel('disk',6);

BW2 = imopen(BW,MR);

BW3 = imclose(BW2,MR);

[L,num] = bwlabel(BW2,4);

num

% find All Beans

beandata=regionprops(L,'basic');

allbeans=[beandata.Area];

% find max & min Beans

maxbean=max(allbeans);

maxbean

minbean=min(allbeans);

minbean

% find Big Bean

bigbean=find(allbeans==maxbean);

bigbean

% find Small Bean

smallbean=find(allbeans==minbean);

smallbean

Explain the program and results (find in Matlab Command Window).

 

II-10.

function CoordinateGenerator

% generate a Vector Model of the object profile

clear, close all,

A = imread('bean.jpg');

BW = ~im2bw(A,graythresh(A));

MR = strel('disk',6);

BW2 = imclose(BW,MR);

[L,num] = bwlabel(BW2,8);

num

STATS = regionprops(L,'BoundingBox','MajorAxisLength');

for j=1:num

if STATS(j).MajorAxisLength>100

maxobject=j;

end

end

maxobject

boxsize=STATS(maxobject).BoundingBox;

boxsize

xb1 = round(boxsize(1));

xb2 = round(boxsize(1)+boxsize(3));

yb1 = round(boxsize(2));

yb2= round(boxsize(2)+boxsize(4));

BWW=BW2(yb1:yb2, xb1:xb2);

figure,

subplot(2,2,1), imshow(BWW)

% to digitise an object (e.g. image, profile) means to convert this object into numbers

% to digitise the outline profile generate Coordinates for a Vector Model as follows:

% define/set a number of steps, e.g. 100

ystepsnum= 100;

% to define the step size use a hight of the box,

% i.e. boxsize(4)-see Tutorial 7, Example II-5.

step=round((boxsize(4))/ystepsnum);

% to generate coordinates of the bean use impixel

% Note: a Vector Model of the bean profile is defined by a set of coordinates

for I=1:ystepsnum

ystep=1+(I-1)*step;

for K=1:boxsize(3)

Px=impixel(BWW,K,ystep);

if Px>0

X(I)= K- boxsize(3)/2;

Y(I)= boxsize(4)- ystep;

break

end

end

end

% use theVector Model to plot the profile of the bean

subplot (2,2,2), plot(X,Y)

axis equal

 

 

III. Tasks

III-1. Explain and compare the results from Examples II-1 and II-2 (Tutorials 8 & 9).

III.2. Compare the number of beans that you received in Example II-4 (Tutorial 7) with the number of beans on the image that you have received in Example II-3 (Tutorials 8 & 9). Explain the result.

III-3. Compare RGB image in the Example II-4 (Tutorial 7) with the RGB image in the Example II-5 (Tutorials 8 & 9). Explain the result.

III-4. Explain and compare the results from Examples II-6 and II-7 (Tutorials 8 & 9).

III-5. Explain and compare the results from Examples II-8 and II-9 (Tutorials 8 & 9).