Learn Python

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

OpenCV Introduction

OpenCV Image Read & Write

OpenCV Image Channels

OpenCV Color Conversion

OpenCV Rotation & Crop

OpenCV Text & Shape

OpenCV Video

OpenCV Arithmetics Operation

OpenCV Thresholding

OpenCV Blur & Blending

OpenCV Edge Detection

OpenCV Contours

OpenCV Hough Transformation Lines

OpenCV Moments & Convexhull

OpenCV Morphological Transformation

OpenCV Template Matching

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Morphological transformation in opencv

To detect the shape of any object morphological transformation is performed. Morphological transformation is performed on binary images and their objects must be in white pixel and background in black pixel.
It needs two inputs 1. original image 2. Kernel.

To basic morphological transformations are:
1. Erosion:
Erosion works on an image object's edges or you can say boundary .Erosion will fix or sharpen the boundary of any object present in the image. Suppose you have a binary image where the background color is black and the object color is white. Now the boundary is not properly visible or you can say that some objects don't have the proper boundary. It means some black pixels are on the border. In this type of case, erosion will try to remove the black pixel and will try to create a proper white border of that object.
2. Dialation:
Dialation works inside of an object. It means erosion fixes/sharp the damage boundary of the objects and dialation fix the damage present inside of an object.
Suppose you have a binary image where the background color is black and the object color is white. Now inside the object, we have some black pixels but there must be white pixels. For this reason, we can say that object inside the area is not properly visible. In this type of case, dialation will try to remove the black pixel and will try to create a proper white area inside of that object.

Erosion

Input
import cv2

path=r"E:\picture\prac_deep\color_boll.jfif"

img=cv2.imread(path,0)
#Here we will read the image and convert the image into binary

ret,Threshold=cv2.threshold(img,231,255,cv2.THRESH_BINARY_INV)

#Let's create a kernel
kernel=np.ones((5,5),np.uint8)
#Here we take the kernel size 5x5 but you can change it.

#Let's perform erosion
Erosion=cv2.erode(Threshold,kernel)

cv2.imshow("my image",Erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output

Dialation

Input
import cv2

path=r"E:\picture\prac_deep\color_boll.jfif"

img=cv2.imread(path,0)
#Here we will read the image and convert the image into binary

ret,Threshold=cv2.threshold(img,231,255,cv2.THRESH_BINARY_INV)

#Let's create a kernel
kernel=np.ones((3,3),np.uint8)
#Here we take the kernel size 3x3 but you can change it.

#Let's perform Dialation
Dialation=cv2.dilate(Threshold,kernel)

cv2.imshow("my image",Threshold)
cv2.imshow("my image",Dialation)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output

Let's see some morphological transformation practical using OpenCV

Input
import cv2

path=r"E:\picture\prac_deep\color_boll.jfif"

img=cv2.imread(path,0)
#Here we will read the image and convert the image into binary

ret,Threshold=cv2.threshold(img,231,255,cv2.THRESH_BINARY_INV)

#Let's create a kernel
kernel=np.ones((3,3),np.uint8)

Opening=cv2.morphologyEx(Threshold,cv2.MORPH_OPEN,kernel)
Close=cv2.morphologyEx(Threshold,cv2.MORPH_CLOSE,kernel)
Tophat=cv2.morphologyEx(Threshold,cv2.MORPH_TOPHAT,kernel)
Gradient=cv2.morphologyEx(Threshold,cv2.MORPH_GRADIENT,kernel)
Dialate=cv2.morphologyEx(Threshold,cv2.MORPH_DILATE,kernel)
Cross=cv2.morphologyEx(Threshold,cv2.MORPH_CROSS,kernel)
Ellipse=cv2.morphologyEx(Threshold,cv2.MORPH_ELLIPSE,kernel)
Hitmiss=cv2.morphologyEx(Threshold,cv2.MORPH_HITMISS,kernel)
Rect=cv2.morphologyEx(Threshold,cv2.MORPH_RECT,kernel)
Blackhat=cv2.morphologyEx(Threshold,cv2.MORPH_BLACKHAT,kernel)

cv2.imshow("Opening",Opening)
cv2.imshow("Close",Close)
cv2.imshow("Tophat",Tophat)
cv2.imshow("Gradient",Gradient)
cv2.imshow("Dialate",Dialate)
cv2.imshow("Cross",Cross)
cv2.imshow("Ellipse",Ellipse)
cv2.imshow("Hitmiss",Hitmiss)
cv2.imshow("Rect",Rect)
cv2.imshow("Blackhat",Blackhat)

cv2.waitKey(0)
cv2.destroyAllWindows()
Output

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.