Image Morphology

Updated: Dec 23, 2019




Image morphology involves modifying images using a smaller 3x3, 5x5 or any other size of restructuring rectangular, elliptical or cross hair sections and applying them to entire image by sliding from left to right and top to bottom.

You can achieve interesting effects like highlighting license plate numbers, isolating tumor in medical image processing, cleaning of images. This can be achieved with few lines of code in python and opencv.



Erosion: Erosion effects starts eroding white part of image from out to in. It uses 3x3 rectangular area and decided center pixel intensity based on its neighborhood. If all pixels surrounding it are white , it turns center pixel white else it turns it black. It then repeated the behavior across entire image by sliding 3x3 matrix on entire image. As you can see, it has started eroding corners of letters in following logo. It also gets rid of small white dots.

Here is the code

# apply a series of erosions for i in range(0, 10):  eroded = cv2.erode(gray.copy(), None, iterations=i + 1)  displayGrayScaleImage(eroded, “Eroded {} times”.format(i + 1))



2. Dilation : Dilation is opposite of erosion. Here again with 3x3 area, center pixel is set white if any of pixel is white else turns it black. It results in increasing the size of foreground object. It also helps in removing pepper noise as shown in image below

# apply a series of dilations for i in range(0, 10):  dilated = cv2.dilate(gray.copy(), None, iterations=i + 1)  displayGrayScaleImage(dilated, “dilated {} times”.format(i + 1))



3. Opening: Opening is erosion followed by dilation. This effect is very useful for removing salt noise without impacting overall image

filterSize = (3,3)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, filterSize)  opening = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel)


4. Closing: Closing is dilation followed by erosion. This effect is very useful for removing pepper noise without impacting overall image

filterSize = (3,3)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, filterSize)  opening = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, kernel)


5. Morphological Gradient: Morphological gradient is difference between dilation and erosion. This effect is used for detecting outline of image

filterSize = (3,3)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, filterSize)  opening = cv2.morphologyEx(gray, cv2.MORPH_GRADIENT, kernel)



6. White hat / Top Hat: White hat is difference between opening and original image. It is used for highlighting white object against dark background. In picture below, road sign is white board against dark background (ignore letters for now) and hence it gets highlighted in output

filterSize = (3,3)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, filterSize)  opening = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, kernel)



7. Black Hat:

Black hat is difference between closing and original image. It is used for highlighting dark object against white background. In picture below, letters “For 14 miles” are black against white background and hence letters gets highlighted in output

filterSize = (3,3)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, filterSize)  opening = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, kernel)



Benefits:

Learning programatic way of performing these operations allows you to quickly scale and apply these effects to millions of images in matter of few seconds. Imaging doing the same thing in photoshop, one image at a time.



Next Steps:

If you would like to learn step by step on how to start using these operations, you can sign up for my course at link below.

https://www.udemy.com/course/fundamentals-of-image-morphology/?referralCode=5160BB5CEFB3D17D28FF

I am offering free enrollment for next 3 days for those who can email me their interests at evergreenllc2020@gmail.com for free coupons.


About Author Evergreen Technologies:

Active in teaching online courses in Computer vision , Natural Language Processing and SaaS system developmentOver 20 years of experience in fortune 500 companies

•Blog: https://www.evergreentech.online

•Linked in: @evergreenllc2020

•Twitter: @tech_evergreen

•Udemy: Evergreen Technologies

•Github: https://github.com/evergreenllc2020/