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)文章
Python使用scipy進(jìn)行曲線擬合的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Python使用scipy進(jìn)行曲線擬合的相關(guān)資料,Scipy優(yōu)化和擬合采用的是optimize模塊,該模塊提供了函數(shù)最小值(標(biāo)量或多維)、曲線擬合和尋找等式的根的有用算法,需要的朋友可以參考下2022-05-05Python從列表推導(dǎo)到zip()函數(shù)的5種技巧總結(jié)
在本篇文章里小編給大家整理的是關(guān)于Python從列表推導(dǎo)到zip()函數(shù)的5種技巧的相關(guān)知識(shí)點(diǎn)和代碼,需要的朋友們參考學(xué)習(xí)下。2019-10-10python機(jī)器學(xué)習(xí)darts時(shí)間序列預(yù)測(cè)和分析
這篇文章主要介紹了python機(jī)器學(xué)習(xí)darts時(shí)間序列預(yù)測(cè)和分析使用實(shí)例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Python如何讀寫二進(jìn)制數(shù)組數(shù)據(jù)
這篇文章主要介紹了Python如何讀寫二進(jìn)制數(shù)組數(shù)據(jù),文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-08-08