官术网_书友最值得收藏!

Erosion and dilation

Erosion and dilation are morphological image processing operations. Morphological image processing basically deals with modifying geometric structures in the image. These operations are primarily defined for binary images, but we can also use them on grayscale images. Erosion basically strips out the outermost layer of pixels in a structure, where as dilation adds an extra layer of pixels on a structure.

Let's see what these operations look like:

Erosion and dilation

Following is the code to achieve this:

import cv2
import numpy as np

img = cv2.imread('input.png', 0)

kernel = np.ones((5,5), np.uint8)

img_erosion = cv2.erode(img, kernel, iterations=1)
img_dilation = cv2.dilate(img, kernel, iterations=1)

cv2.imshow('Input', img)
cv2.imshow('Erosion', img_erosion)
cv2.imshow('Dilation', img_dilation)

cv2.waitKey(0)

Afterthought

OpenCV provides functions to directly erode and dilate an image. They are called erode and dilate, respectively. The interesting thing to note is the third argument in these two functions. The number of iterations will determine how much you want to erode/dilate a given image. It basically applies the operation successively to the resultant image. You can take a sample image and play around with this parameter to see what the results look like.

主站蜘蛛池模板: 肇庆市| 融水| 宁安市| 定襄县| 孝感市| 咸阳市| 石嘴山市| 淄博市| 色达县| 五寨县| 黎城县| 杭州市| 南宫市| 温州市| 平潭县| 海阳市| 竹溪县| 阳信县| 延津县| 仪征市| 仙居县| 游戏| 正定县| 克拉玛依市| 若羌县| 旺苍县| 攀枝花市| 迭部县| 黎平县| 东兴市| 肃宁县| 广东省| 叶城县| 吉首市| 吉木乃县| 大悟县| 山丹县| 台州市| 曲松县| 鸡西市| 东兴市|