-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorph_cross_element.py
More file actions
34 lines (30 loc) · 1.19 KB
/
morph_cross_element.py
File metadata and controls
34 lines (30 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Python program to demonstrate erosion and
# dilation of images.
import cv2
import numpy as np
# Reading the input image
img = cv2.imread('p.jpg', 0)
#using cross element
# Taking a matrix of size 5*5 as the kernel#
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5))
# The first parameter is the original image,
# kernel is the matrix with which image is
# convolved and third parameter is the number
# of iterations, which will determine how much
# you want to erode/dilate a given image.
img_erosion = cv2.erode(img, kernel, iterations=1)
img_dilation = cv2.dilate(img, kernel, iterations=1)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel,iterations=4)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel,iterations=2)
Morphologicalgradient=cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
cv2.imshow('Input', img)
cv2.imshow('Erosion', img_erosion)
cv2.imshow('Dilation', img_dilation)
cv2.imshow('opening',opening)
cv2.imshow('closing',closing)
cv2.imshow('Gradient',Morphologicalgradient)
cv2.imshow('TOPHAT',tophat)
cv2.imshow('Blackhat',blackhat)
cv2.waitKey(0)