Python+OpenCV之形態(tài)學(xué)操作詳解
一、 腐蝕與膨脹
1.1 腐蝕操作
import cv2
import numpy as np
img = cv2.imread('DataPreprocessing/img/dige.png')
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
dige.png原圖1展示(注: 沒有原圖的可以截圖下來保存本地。):

腐蝕1輪次之后~ (iterations = 1)
kernel = np.ones((3, 3), np.uint8)
erosion = cv2.erode(img, kernel, iterations=1)
cv2.imshow('erosion', erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
腐蝕結(jié)果展示圖2:

腐蝕圓多次的效果,以及腐蝕原理
pie = cv2.imread('DataPreprocessing/img/pie.png')
cv2.imshow('pie', pie)
cv2.waitKey(0)
cv2.destroyAllWindows()
pie.png原圖3:

腐蝕原理, 其中濾波器的大小越大腐蝕的程度越大 圖4:

kernel = np.ones((30, 30), np.uint8)
erosion_1 = cv2.erode(pie, kernel, iterations=1)
erosion_2 = cv2.erode(pie, kernel, iterations=2)
erosion_3 = cv2.erode(pie, kernel, iterations=3)
res = np.hstack((erosion_1, erosion_2, erosion_3))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
圓腐蝕三次結(jié)果展示圖5:

1.2 膨脹操作
kernel = np.ones((3, 3), np.uint8)
dige_dilate = erosion
dige_dilate = cv2.dilate(erosion, kernel, iterations=1)
cv2.imshow('dilate', dige_dilate)
cv2.waitKey(0)
cv2.destroyAllWindows()
膨脹之前圖2,發(fā)現(xiàn)線條變粗,跟原圖對(duì)比的線條相差無幾,但是沒了那些長須裝的噪音,圖6:

膨脹圓多次的效果,以及膨脹原理與腐蝕相反,有白色點(diǎn)的濾波器則濾波器內(nèi)數(shù)據(jù)全變?yōu)榘咨?/p>
pie = cv2.imread('DataPreprocessing/img/pie.png')
kernel = np.ones((30, 30), np.uint8)
dilate_1 = cv2.dilate(pie, kernel, iterations=1)
dilate_2 = cv2.dilate(pie, kernel, iterations=2)
dilate_3 = cv2.dilate(pie, kernel, iterations=3)
res = np.hstack((dilate_1, dilate_2, dilate_3))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
膨脹圓3次的結(jié)果展示,圖7:

二、 開運(yùn)算與閉運(yùn)算
2.1 開運(yùn)算
# 開:先腐蝕,再膨脹
img = cv2.imread('DataPreprocessing/img/dige.png')
kernel = np.ones((5, 5), np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
cv2.imshow('opening', opening)
cv2.waitKey(0)
cv2.destroyAllWindows()
將原圖1,先腐蝕,再膨脹,得到開運(yùn)算結(jié)果圖8:

2.2 閉運(yùn)算
# 閉:先膨脹,再腐蝕
img = cv2.imread('DataPreprocessing/img/dige.png')
kernel = np.ones((5, 5), np.uint8)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
cv2.imshow('closing', closing)
cv2.waitKey(0)
cv2.destroyAllWindows()
將原圖1,先膨脹,再腐蝕,得到開運(yùn)算結(jié)果圖9:

三、梯度運(yùn)算
拿原圖3的圓,做5次膨脹,5次腐蝕,相減得到其輪廓。
# 梯度=膨脹-腐蝕
pie = cv2.imread('DataPreprocessing/img/pie.png')
kernel = np.ones((7, 7), np.uint8)
dilate = cv2.dilate(pie, kernel, iterations=5)
erosion = cv2.erode(pie, kernel, iterations=5)
res = np.hstack((dilate, erosion))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
gradient = cv2.morphologyEx(pie, cv2.MORPH_GRADIENT, kernel)
cv2.imshow('gradient', gradient)
cv2.waitKey(0)
cv2.destroyAllWindows()
得到梯度運(yùn)算結(jié)果圖10:


四、禮帽與黑帽
4.1 禮帽
禮帽 = 原始輸入-開運(yùn)算結(jié)果
# 禮帽
img = cv2.imread('DataPreprocessing/img/dige.png')
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
cv2.imshow('tophat', tophat)
cv2.waitKey(0)
cv2.destroyAllWindows()
得到禮帽結(jié)果圖11:

4.2 黑帽
黑帽 = 閉運(yùn)算-原始輸入
# 黑帽
img = cv2.imread('DataPreprocessing/img/dige.png')
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
cv2.imshow('blackhat ', blackhat)
cv2.waitKey(0)
cv2.destroyAllWindows()
得到禮帽結(jié)果圖12:

以上就是Python+OpenCV之形態(tài)學(xué)操作詳解的詳細(xì)內(nèi)容,更多關(guān)于Python OpenCV形態(tài)學(xué)操作的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
django從請(qǐng)求到響應(yīng)的過程深入講解
這篇文章主要給大家介紹了關(guān)于django從請(qǐng)求到響應(yīng)的過程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
python簡(jiǎn)單實(shí)現(xiàn)基數(shù)排序算法
這篇文章主要介紹了python簡(jiǎn)單實(shí)現(xiàn)基數(shù)排序算法,僅用4行代碼即可實(shí)現(xiàn)基數(shù)排序算法,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-05-05
Python基礎(chǔ)之函數(shù)用法實(shí)例詳解
這篇文章主要介紹了Python中函數(shù)用法,包括了函數(shù)的創(chuàng)建、定義、參數(shù)等,需要的朋友可以參考下2014-09-09
Python3通過chmod修改目錄或文件權(quán)限的方法示例
這篇文章主要介紹了Python3通過chmod修改目錄或文件權(quán)限的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
python中l(wèi)xml.etree 和 ElementTree 的區(qū)別解析
lxml.etree 提供了更多的功能,例如 XPath、XSLT、Relax NG、 和 XML 模式支持,etree 對(duì) Python unicode 字符串的想法與 ElementTree 不同,本文給大家介紹python中l(wèi)xml.etree 和 ElementTree 的區(qū)別,感興趣的朋友一起看看吧2024-01-01
Python 字符串轉(zhuǎn)換為整形和浮點(diǎn)類型的方法
今天小編就為大家分享一篇Python 字符串轉(zhuǎn)換為整形和浮點(diǎn)類型的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07
深入理解Pytorch微調(diào)torchvision模型
PyTorch是一個(gè)基于Torch的Python開源機(jī)器學(xué)習(xí)庫,用于自然語言處理等應(yīng)用程序。它主要由Facebookd的人工智能小組開發(fā),不僅能夠 實(shí)現(xiàn)強(qiáng)大的GPU加速,同時(shí)還支持動(dòng)態(tài)神經(jīng)網(wǎng)絡(luò),這一點(diǎn)是現(xiàn)在很多主流框架如TensorFlow都不支持的2021-11-11

