When we say morphing, we remember this guy

morphing.gif

right? …Oh is it just me? Well, that’s lame. How about this guy right here?

morphling.jpg

For Dota 2 players like me, I guess we’re on the same page now. For other demographic, I’m really sorry. Fret not, because what I’m talking about here is not the Mighty Morphin Power Rangers nor the disgustingly strong Morphling (have you seen DC.Resolut1on on his Morph?!) but morphological operations instead.

binary.jpg

Example of a binary shape

MORPHOLOGICAL OPERATIONS, from its name, are operations that changes the morphology or shape of the objects it is operated on. In image processing, this operations are done on binary images where shapes are defined by the aggregation of ‘1’ value. For example, this one on the right.

Morphological operations are used to expand, thin out, connect, and/or fill gaps of images with respect to a structuring element (SE). These feats are possible because morphological operations make use of Set Theory.


SET THEORY

According to Google,setheory.JPG

I know most are familiar to Set Theory, but if the google definition is not enough, then here’s something more familiar…

set.jpg

To review

point a is an element of A, and is denoted by

a ∈ A

point b however is not an element of A. This is denoted as

b ∉ A

Since all elements of C are elements of A, then C is then referred to as a subset of A. This is denoted by

C ⊂ A

The union of A and B is ALL the element inside A and B. On the other hand, the intersection of A and B is ALL COMMON element of the two sets. These are denoted by

A ∪ B

A ∩ B

If two sets are mutually exclusive, then their intersection is a null set. Null set by the way is denoted as (let’s take an empty set and name it D)

D = Ø

Delving deeper, we can get the complimentary, reflection, or translation of a set, and difference between two sets.

For example, the complimentary set for A is a set of all elements that is not A

A^C = \{ w:w \notin A \}

The reflection of a set is the negative of each element of A. Visually this can be seen as if the set is reflected along the line y = x.

\hat{A} = \{ w:w=-a, for a \in A \}

Translation is just how it sounds, movement of the set by some unit z. It is denoted as

(A)_z = \{ c:c=a+z, for a \in A \}

The difference of two sets, A and B for instance denoted by A-B, is the set of all elements of A excluding elements of B.

A - C = \{ w:w \in A, w \notin C \}

Visually, these looks like this

img.png


 

Back to the main topic, I now introduce you to dilation and erosion morphological operations. Dilation is the expansion of an image A by B and is denoted by

A \oplus B = \{ z|(\hat{B})_z  \cap A \neq \emptyset \}

While erosion works on the reverse, it diminishes the image A by B and it is denoted by

A \ominus B = \{ z| B_z \subseteq A\}

The B set is called the structuring element. Structuring element is more like a reference or guide on how the image is going to morph. So how does it really work? Using the definition and some Googling, I tried to predict how an image will be morphed by a structuring element.

Image SE Erosion (Red) / Dilation (Blue)
5x5 square.png 2x2.jpg  5x5 square.jpg
2x1.jpg 5x5 square.jpg
1x2.jpg  5x5 square.jpg
cross_.jpg  5x5 square.jpg
diag.jpg  5x5 square.jpg
triangle.png 2x2.jpg  triangle.jpg
2x1.jpg  triangle.jpg
1x2.jpg  triangle.jpg
cross_.jpg  triangle.jpg
diag.jpg  triangle.jpg
10x10.png  2x2.jpg  10x10.jpg
2x1.jpg  10x10.jpg
1x2.jpg 5x5 square.jpg
cross_.jpg 10x10.jpg
diag.jpg 10x10.jpg
cross.png 2x2.jpg cross.jpg
2x1.jpg cross.jpg
1x2.jpg cross.jpg
cross_.jpg cross.jpg
diag.jpg cross.jpg
TIPS: Essentially, you set a reference point for the SE first. Then for erosion, just remember that “IF IT FITS, IT SITS”. If the SE fits in the image, then the reference point remains. For dilation, you have to reflect the image first. Then if any of the elements of SE overlaps with the image, the reference point sits. However, I find it convenient to overlap the reference point of the SE to the image and fill all the elements of the SE with no overlap.

How do I know it is correct? I ran a program, duh. Here’s the one I ran for the square image and 2×2 square SE.

sqr = zeros(7,7); sqr(2:6,2:6) = 1;

SE1 = CreateStructureElement(‘custom’, [%t, %t;%t,%t]);

dilate_sqr_se1 = DilateImage(sqr, SE1)

erode_sqr_se1 = ErodeImage(sqr, SE1)

Running all images and SEs, here’s what I got.

For the

For the Triangle,

For the 10×10 hollow square,

For the cross,

It may seem different at first, but that’s because the reference point I used is always at the bottom left and SciLab’s seems to be on the top left. However, the general shape is identical.

Application

Circles002.jpg
This image

We can use the erosion and dilation to clean images. For example, we can remove salt noises in an image using an opening operator. It accomplishes it by eroding the image first then dilating it back. Shapes with smaller and/or different shape compared to the SE gets erased and the target shape is retained. If the target shape has holes in it, then a closing operator may be useful. What it does is dilating the image, causing it to connect with nearby objects, and then eroding the image with the same SE. Top hat operator is another tool that uses dilation and erosion. What it does is extract small details that are brighter than the background. It accomplishes this by subtracting the dilated image to the original image.

 

Let’s look at this image for example. Segmenting the image to get the circles, we can apply the morphological operations to obtain just the circles. But what operation to use?

I applied the three on a patch of the image and got this.

Circles with cancer.jpgWe can see that the opening operator is the best to use here as it removes the noise but keeping the original sizes of each circles. What these circles actually are representation of the sizes of normal sized cells. Using pixel counting, the average area of normal cells are computed after performing opening operator to the segmented image.

Using the obtained average area, the potential cancer cells were picked out from the image on the left.

See? Aren’t morphological operations useful? Next post, I’ll use morphological operations again to play music. *wink wink*. SEE YOU!

For this activity, I would like to rate myself 10/10 because I successfully located the cancer cells from an image. I hope I explained morphological operations in a simple way too! Shout out to Louie, Patrick, Carlo, and kuya Roland for helping me out figure stuff out. Special shout out to Kuya Ron for his blog and to Trixia for pushing me to be better and for being my sanctuary when I get frustrated and tired.

Advertisements