Morphological Operations

It’s been a while, but we’re back with a new activity! This post will be on Morphological Operations.

Morphology refers to shape or structure. So when we say morphological operations, we basically mean that we’re gonna to apply functions or operations to shapes. In image processing, basic morphological operations are done on binary images to improve the image or for further processing.

There are two basic operations in morphological operations. Other functions are just combinations of these two operations. These are Dilation and Erosion.

Morphological Operations involves Set Theory, but we will simplify what happens to the shapes for our purposes. Each operation involves a shape or object, which we’ll call object A, and a structuring object, which we’ll call SE. Now remember that our object is a binary image, so A is just an array of ones that form its shape. SE is a smaller shape that will define the effect of our morphological operation.

Erosion is when each point of the object A is overlayed with our SE, and if the SE is completely defined at that point (meaning that the SE is covered by the ones of the object A), then that point is retained. All other points where the SE is not completely defined are removed. This mean the final product is a smaller image reduced by the shape SE.

Dilation is the reverse. If the SE at each point is completely defined, then nothing happens. But if the SE at a point is not completely defined, then points are added until the SE is completely defined at that point. Hence the effect is that the object A grows by the shape SE.

There are three basic morphological operations that we can use in Matlab, OpenClose, and Tophat. These operations are all using various combinations of Erosion and Dilation. The Open function is Erosion followed by Dilation using the same SE. The Close function is Dilation followed by Erosion. Tophat uses Erosion then minuses the result from the original object.

We first apply Erosion and Dilation to some objects and using various SEs. Here is the list of objects and SEs that we used:

Objects

  1. 5×5 Square
  2. Right Triangle (base=4 boxes,height=3 boxes)
  3. Hollow 10×10 Square, 2 boxes thick
  4. A Plus sign, 1 box thick, 5 boxes long

SEs

  1. 2×2 ones
  2. 2×1 ones
  3. 1×2 ones
  4. cross, 3 boxes long, one box thick
  5. diagonal line, 2 boxes long

We first did them manually on graphing paper, which you can see here below:

12180040_10153243952551964_1111469667_n.jpg
Figure 1. The Objects and the SEs
12179829_10153243945286964_1638286989_n.jpg
Figure 2. The Erosion and Dilation. The Yellow Highlight shows the added boxes as a result of Dilation. The Red boxes show the deleted boxes as a result of Erosion.

We then repeated the exercise in Matlab to verify our results.

morph_montage.jpg
Figure 3. Results of using Erosion and Dilation. First 20 images show Erosion. Second 20 images show Dilation.

The results look very much like our drawings, so we applied erosion and dilation correctly.

Morphological Operations are also useful when trying to analyze several ROIs (Regions of Interest) in a single image, such as cell counting or vehicle tracking. Morphological operations can be used to clean artifacts from the image, separate overlapping or touching ROIs, or fill in holes of an ROI. For example, we can use morphological operations to identify ‘cancer cells’ in an image. The ‘cancer cells’ are represented here by larger circles.

Circles002.jpg
Figure 4. The image of normal ‘cells’
Circles with cancer.jpg
Figure 5. The image of normal and ‘cancer cells’

We first split the image without any cancer cells into uniform 256×256 subimages (even if they overlap). We then apply a threshold to remove some of the artifacts from the image and also the Open function in order to further clean up the image and separate any touching circles.

 

c1.jpg
Figure 6. Subimage after Thresholding

cs1.jpg

After this, we can identify the circles and find their areas. By doing this for all the subimages, we can obtain a mean and standard deviation value for the area of the circles. Since cancer cells are larger than the normal cells, we just want to remove those greater than the mean + standard deviation.

To remove those from our image, we need a radius for the SE that we will use to find the cells larger than our average area. We calculate this from the average area and round it down since we need an integer for the radius. Then it is just a matter of using the Tophat function to remove the cancer cells from the image.

Our results were:

mean = 609.2189

standard dev. = 245.7036

average area = mean + standard dev = 854.9225

radius = round(sqrt(average area/pi)-1) = 15

You can see the normal image of the normal and cancer cells below:

cs_c.jpg
Figure 8. The image after applying thresholding and Open function
test.jpg
Figure 9. Identified Cancer cells are the white circular outlines. The code identifies 5 cells to be cancer cells.

You can see how very useful this can be in medical research. The possibilities are endless for applying this technique in order to gain more insight in other fields not only in medical research.

 

Code used for morphological operations (Erosion and Dilation):

IM1 = ones(5,5);

IM2 = zeros(4,4);

IM2(2,1) = 1;

IM2(3,1) = 1;

IM2(3,2) = 1;

IM2(3,3) = 1;

IM2(4,:) = 1;

IM3  = zeros(10,10);

IM3(3:8,3:8) = 1;

IM4 = zeros(5,5);

IM4(3,:) = 1;

IM4(:,3) = 1;

 

SE1 = strel(‘square’,2);

SE2 = strel(‘line’,2,0);

SE3 = strel(‘line’,2,90);

A = zeros(3,3);

A(2,:) = 1;

A(:,2) = 1;

SE4 = strel(A);

SE5 = strel(‘pair’,[-1,1]);

 

I1ESE1 = imerode(IM1,SE1);

figure();

imagesc(I1ESE1);

colormap(gray);

print(‘I1ESE1.png’, ‘-dpng’)

I1ESE2 = imerode(IM1,SE2);

figure();

imagesc(I1ESE2);

colormap(gray);

print(‘I1ESE2.png’, ‘-dpng’)

I1ESE3 = imerode(IM1,SE3);

figure();

imagesc(I1ESE3);

colormap(gray);

print(‘I1ESE3.png’, ‘-dpng’)

I1ESE4 = imerode(IM1,SE4);

figure();

imagesc(I1ESE4);

colormap(gray);

print(‘I1ESE4.png’, ‘-dpng’)

I1ESE5 = imerode(IM1,SE5);

figure();

imagesc(I1ESE5);

colormap(gray);

print(‘I1ESE5.png’, ‘-dpng’)

I2ESE1 = imerode(IM2,SE1);

figure();

imagesc(I2ESE1);

colormap(gray);

print(‘I2ESE1.png’, ‘-dpng’)

I2ESE2 = imerode(IM2,SE2);

figure();

imagesc(I2ESE2);

colormap(gray);

print(‘I2ESE2.png’, ‘-dpng’)

I2ESE3 = imerode(IM2,SE3);

figure();

imagesc(I2ESE3);

colormap(gray);

print(‘I2ESE3.png’, ‘-dpng’)

I2ESE4 = imerode(IM2,SE4);

figure();

imagesc(I2ESE4);

colormap(gray);

print(‘I2ESE4.png’, ‘-dpng’)

I2ESE5 = imerode(IM2,SE5);

figure();

imagesc(I2ESE5);

colormap(gray);

print(‘I2ESE5.png’, ‘-dpng’)

I3ESE1 = imerode(IM3,SE1);

figure();

imagesc(I3ESE1);

colormap(gray);

print(‘I3ESE1.png’, ‘-dpng’)

I3ESE2 = imerode(IM3,SE2);

figure();

imagesc(I3ESE2);

colormap(gray);

print(‘I3ESE2.png’, ‘-dpng’)

I3ESE3 = imerode(IM3,SE3);

figure();

imagesc(I3ESE3);

colormap(gray);

print(‘I3ESE3.png’, ‘-dpng’)

I3ESE4 = imerode(IM3,SE4);

figure();

imagesc(I3ESE4);

colormap(gray);

print(‘I3ESE4.png’, ‘-dpng’)

I3ESE5 = imerode(IM3,SE5);

figure();

imagesc(I3ESE5);

colormap(gray);

print(‘I3ESE5.png’, ‘-dpng’)

I4ESE1 = imerode(IM4,SE1);

figure();

imagesc(I4ESE1);

colormap(gray);

print(‘I4ESE1.png’, ‘-dpng’)

I4ESE2 = imerode(IM4,SE2);

figure();

imagesc(I4ESE2);

colormap(gray);

print(‘I4ESE2.png’, ‘-dpng’)

I4ESE3 = imerode(IM4,SE3);

figure();

imagesc(I4ESE3);

colormap(gray);

print(‘I4ESE3.png’, ‘-dpng’)

I4ESE4 = imerode(IM4,SE4);

figure();

imagesc(I4ESE4);

colormap(gray);

print(‘I4ESE4.png’, ‘-dpng’)

I4ESE5 = imerode(IM4,SE5);

figure();

imagesc(I4ESE5);

colormap(gray);

print(‘I4ESE5.png’, ‘-dpng’)

 

I1DSE1 = imdilate(IM1,SE1);

figure();

imagesc(I1DSE1);

colormap(gray);

print(‘I1DSE1.png’, ‘-dpng’)

I1DSE2 = imdilate(IM1,SE2);

figure();

imagesc(I1DSE2);

colormap(gray);

print(‘I1DSE2.png’, ‘-dpng’)

I1DSE3 = imdilate(IM1,SE3);

figure();

imagesc(I1DSE3);

colormap(gray);

print(‘I1DSE3.png’, ‘-dpng’)

I1DSE4 = imdilate(IM1,SE4);

figure();

imagesc(I1DSE4);

colormap(gray);

print(‘I1DSE4.png’, ‘-dpng’)

I1DSE5 = imdilate(IM1,SE5);

figure();

imagesc(I1DSE5);

colormap(gray);

print(‘I1DSE5.png’, ‘-dpng’)

I2DSE1 = imdilate(IM2,SE1);

figure();

imagesc(I2DSE1);

colormap(gray);

print(‘I2DSE1.png’, ‘-dpng’)

I2DSE2 = imdilate(IM2,SE2);

figure();

imagesc(I2DSE2);

colormap(gray);

print(‘I2DSE2.png’, ‘-dpng’)

I2DSE3 = imdilate(IM2,SE3);

figure();

imagesc(I2DSE3);

colormap(gray);

print(‘I2DSE3.png’, ‘-dpng’)

I2DSE4 = imdilate(IM2,SE4);

figure();

imagesc(I2DSE4);

colormap(gray);

print(‘I2DSE4.png’, ‘-dpng’)

I2DSE5 = imdilate(IM2,SE5);

figure();

imagesc(I2DSE5);

colormap(gray);

print(‘I2DSE5.png’, ‘-dpng’)

I3DSE1 = imdilate(IM3,SE1);

figure();

imagesc(I3DSE1);

colormap(gray);

print(‘I3DSE1.png’, ‘-dpng’)

I3DSE2 = imdilate(IM3,SE2);

figure();

imagesc(I3DSE2);

colormap(gray);

print(‘I3DSE2.png’, ‘-dpng’)

I3DSE3 = imdilate(IM3,SE3);

figure();

imagesc(I3DSE3);

colormap(gray);

print(‘I3DSE3.png’, ‘-dpng’)

I3DSE4 = imdilate(IM3,SE4);

figure();

imagesc(I3DSE4);

colormap(gray);

print(‘I3DSE4.png’, ‘-dpng’)

I3DSE5 = imdilate(IM3,SE5);

figure();

imagesc(I3DSE5);

colormap(gray);

print(‘I3DSE5.png’, ‘-dpng’)

I4DSE1 = imdilate(IM4,SE1);

figure();

imagesc(I4DSE1);

colormap(gray);

print(‘I4DSE1.png’, ‘-dpng’)

I4DSE2 = imdilate(IM4,SE2);

figure();

imagesc(I4DSE2);

colormap(gray);

print(‘I4DSE2.png’, ‘-dpng’)

I4DSE3 = imdilate(IM4,SE3);

figure();

imagesc(I4DSE3);

colormap(gray);

print(‘I4DSE3.png’, ‘-dpng’)

I4DSE4 = imdilate(IM4,SE4);

figure();

imagesc(I4DSE4);

colormap(gray);

print(‘I4DSE4.png’, ‘-dpng’)

I4DSE5 = imdilate(IM4,SE5);

figure();

imagesc(I4DSE5);

colormap(gray);

print(‘I4DSE5.png’, ‘-dpng’)

 

filenames = {

‘I1ESE1.png’

‘I1ESE2.png’

‘I1ESE3.png’

‘I1ESE4.png’

‘I1ESE5.png’

‘I2ESE1.png’

‘I2ESE2.png’

‘I2ESE3.png’

‘I2ESE4.png’

‘I2ESE5.png’

‘I3ESE1.png’

‘I3ESE2.png’

‘I3ESE3.png’

‘I3ESE4.png’

‘I3ESE5.png’

‘I4ESE1.png’

‘I4ESE2.png’

‘I4ESE3.png’

‘I4ESE4.png’

‘I4ESE5.png’

‘I1DSE1.png’

‘I1DSE2.png’

‘I1DSE3.png’

‘I1DSE4.png’

‘I1DSE5.png’

‘I2DSE1.png’

‘I2DSE2.png’

‘I2DSE3.png’

‘I2DSE4.png’

‘I2DSE5.png’

‘I3DSE1.png’

‘I3DSE2.png’

‘I3DSE3.png’

‘I3DSE4.png’

‘I3DSE5.png’

‘I4DSE1.png’

‘I4DSE2.png’

‘I4DSE3.png’

‘I4DSE4.png’

‘I4DSE5.png’ };

 

montage(filenames, [4 5])

 

 

 

Code used for Cancer Cells Searching:

I1 = mat2gray(imread(‘C1_01.jpg’));

C1 = I1>0.8;

S = strel(‘disk’,7,0);

CS1 = imopen(C1,S);

a1 = regionprops(‘table’,CS1,’Area’);

A1 = table2array(a1);

m1 = mean(A1);

sd1 = std(A1);

 

I2 = mat2gray(imread(‘C1_02.jpg’));

C2 = I2>0.8;

CS2 = imopen(C2,S);

a2 = regionprops(‘table’,CS2,’Area’);

A2 = table2array(a2);

m2 = mean(A2);

sd2 = std(A2);

 

I3 = mat2gray(imread(‘C1_03.jpg’));

C3 = I3>0.8;

CS3 = imopen(C3,S);

a3 = regionprops(‘table’,CS3,’Area’);

A3 = table2array(a3);

m3 = mean(A3);

sd3 = std(A3);

 

I4 = mat2gray(imread(‘C1_04.jpg’));

C4 = I4>0.8;

CS4 = imopen(C4,S);

a4 = regionprops(‘table’,CS4,’Area’);

A4 = table2array(a4);

m4 = mean(A4);

sd4 = std(A4);

 

I5 = mat2gray(imread(‘C1_05.jpg’));

C5 = I5>0.8;

CS5 = imopen(C5,S);

a5 = regionprops(‘table’,CS5,’Area’);

A5 = table2array(a5);

m5 = mean(A5);

sd5 = std(A5);

 

I6 = mat2gray(imread(‘C1_06.jpg’));

C6 = I6>0.8;

CS6 = imopen(C6,S);

a6 = regionprops(‘table’,CS6,’Area’);

A6 = table2array(a6);

m6 = mean(A6);

sd6 = std(A6);

 

I7 = mat2gray(imread(‘C1_07.jpg’));

C7 = I7>0.8;

CS7 = imopen(C7,S);

a7 = regionprops(‘table’,CS7,’Area’);

A7 = table2array(a7);

m7 = mean(A7);

sd7 = std(A7);

 

I8 = mat2gray(imread(‘C1_08.jpg’));

C8 = I8>0.8;

CS8 = imopen(C8,S);

a8 = regionprops(‘table’,CS8,’Area’);

A8 = table2array(a8);

m8 = mean(A8);

sd8 = std(A8);

 

I9 = mat2gray(imread(‘C1_09.jpg’));

C9 = I9>0.8;

CS9 = imopen(C9,S);

a9 = regionprops(‘table’,CS9,’Area’);

A9 = table2array(a9);

m9 = mean(A9);

sd9 = std(A9);

 

I10 = mat2gray(imread(‘C1_10.jpg’));

C10 = I10>0.8;

CS10 = imopen(C10,S);

a10 = regionprops(‘table’,CS10,’Area’);

A10 = table2array(a10);

m10 = mean(A10);

sd10 = std(A10);

 

I11 = mat2gray(imread(‘C1_11.jpg’));

C11 = I11>0.8;

CS11 = imopen(C11,S);

a11 = regionprops(‘table’,CS11,’Area’);

A11 = table2array(a11);

m11 = mean(A11);

sd11 = std(A11);

 

I12 = mat2gray(imread(‘C1_12.jpg’));

C12 = I12>0.8;

CS12 = imopen(C12,S);

a12 = regionprops(‘table’,CS12,’Area’);

A12 = table2array(a12);

m12 = mean(A12);

sd12 = std(A12);

 

m = (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 + m12)/12;

sd = (sd1 + sd2 + sd3 + sd4 + sd5 + sd6 + sd7 + sd8 + sd9 + sd10 + sd11 + sd12)/12;

area = m + sd;

radius = round(sqrt(area/pi)-1);

 

Sc = strel(‘disk’,radius);

 

I = mat2gray(imread(‘C_cancer.jpg’));

C_cancer = I>0.8;

CS_cancer = imopen(C_cancer,S);

test = imtophat(CS_cancer,Sc);

subplot(1,2,1), subimage(CS_cancer)

subplot(1,2,2), subimage(test)

 

Leave a comment