- OpenCV with Python By Example
- Prateek Joshi
- 184字
- 2021-07-16 19:57:06
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:

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.
- OpenStack Cloud Computing Cookbook(Third Edition)
- Vue.js設(shè)計(jì)與實(shí)現(xiàn)
- 解構(gòu)產(chǎn)品經(jīng)理:互聯(lián)網(wǎng)產(chǎn)品策劃入門寶典
- Boost C++ Application Development Cookbook(Second Edition)
- 深入理解Django:框架內(nèi)幕與實(shí)現(xiàn)原理
- 零基礎(chǔ)學(xué)MQL:基于EA的自動化交易編程
- Java EE 7 Development with NetBeans 8
- Xamarin.Forms Projects
- AutoCAD VBA參數(shù)化繪圖程序開發(fā)與實(shí)戰(zhàn)編碼
- 快速念咒:MySQL入門指南與進(jìn)階實(shí)戰(zhàn)
- D3.js 4.x Data Visualization(Third Edition)
- Node.js Design Patterns
- FPGA Verilog開發(fā)實(shí)戰(zhàn)指南:基于Intel Cyclone IV(進(jìn)階篇)
- jQuery炫酷應(yīng)用實(shí)例集錦
- IBM Cognos Business Intelligence 10.1 Dashboarding cookbook