日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

python數(shù)字圖像處理之基本形態(tài)學(xué)濾波_python

作者:denny402 ? 更新時(shí)間: 2022-08-21 編程語言

引言

對(duì)圖像進(jìn)行形態(tài)學(xué)變換。變換對(duì)象一般為灰度圖或二值圖,功能函數(shù)放在morphology子模塊內(nèi)。

1、膨脹(dilation)

原理:一般對(duì)二值圖像進(jìn)行操作。找到像素值為1的點(diǎn),將它的鄰近像素點(diǎn)都設(shè)置成這個(gè)值。1值表示白,0值表示黑,因此膨脹操作可以擴(kuò)大白色值范圍,壓縮黑色值范圍。一般用來擴(kuò)充邊緣或填充小的孔洞。

功能函數(shù):skimage.morphology.dilation(image,?selem=None)

selem表示結(jié)構(gòu)元素,用于設(shè)定局部區(qū)域的形狀和大小。

from skimage import data
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=data.checkerboard()
dst1=sm.dilation(img,sm.square(5))  #用邊長(zhǎng)為5的正方形濾波器進(jìn)行膨脹濾波
dst2=sm.dilation(img,sm.square(15))  #用邊長(zhǎng)為15的正方形濾波器進(jìn)行膨脹濾波
plt.figure('morphology',figsize=(8,8))
plt.subplot(131)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.subplot(132)
plt.title('morphological image')
plt.imshow(dst1,plt.cm.gray)
plt.subplot(133)
plt.title('morphological image')
plt.imshow(dst2,plt.cm.gray)

分別用邊長(zhǎng)為5或15的正方形濾波器對(duì)棋盤圖片進(jìn)行膨脹操作,結(jié)果如下:

可見濾波器的大小,對(duì)操作結(jié)果的影響非常大。一般設(shè)置為奇數(shù)。

除了正方形的濾波器外,濾波器的形狀還有一些,現(xiàn)列舉如下:

morphology.square: 正方形

morphology.disk: ?平面圓形

morphology.ball: 球形

morphology.cube: 立方體形

morphology.diamond: 鉆石形

morphology.rectangle: 矩形

morphology.star: 星形

morphology.octagon: 八角形

morphology.octahedron: 八面體

注意,如果處理圖像為二值圖像(只有0和1兩個(gè)值),則可以調(diào)用:

skimage.morphology.binary_dilation(image,?selem=None)

用此函數(shù)比處理灰度圖像要快。

2、腐蝕(erosion)

函數(shù):skimage.morphology.erosion(image,?selem=None)

selem表示結(jié)構(gòu)元素,用于設(shè)定局部區(qū)域的形狀和大小。

和膨脹相反的操作,將0值擴(kuò)充到鄰近像素。擴(kuò)大黑色部分,減小白色部分。可用來提取骨干信息,去掉毛刺,去掉孤立的像素。

from skimage import data
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=data.checkerboard()
dst1=sm.erosion(img,sm.square(5))  #用邊長(zhǎng)為5的正方形濾波器進(jìn)行膨脹濾波
dst2=sm.erosion(img,sm.square(25))  #用邊長(zhǎng)為25的正方形濾波器進(jìn)行膨脹濾波
plt.figure('morphology',figsize=(8,8))
plt.subplot(131)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.subplot(132)
plt.title('morphological image')
plt.imshow(dst1,plt.cm.gray)
plt.subplot(133)
plt.title('morphological image')
plt.imshow(dst2,plt.cm.gray)

注意,如果處理圖像為二值圖像(只有0和1兩個(gè)值),則可以調(diào)用:

skimage.morphology.binary_erosion(image,?selem=None)

用此函數(shù)比處理灰度圖像要快。

3、開運(yùn)算(opening)

函數(shù):skimage.morphology.openning(image,?selem=None)

selem表示結(jié)構(gòu)元素,用于設(shè)定局部區(qū)域的形狀和大小。

先腐蝕再膨脹,可以消除小物體或小斑塊。

from skimage import io,color
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=color.rgb2gray(io.imread('d:/pic/mor.png'))
dst=sm.opening(img,sm.disk(9))  #用邊長(zhǎng)為9的圓形濾波器進(jìn)行膨脹濾波
plt.figure('morphology',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.axis('off')
plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst,plt.cm.gray)
plt.axis('off')

注意,如果處理圖像為二值圖像(只有0和1兩個(gè)值),則可以調(diào)用:

skimage.morphology.binary_opening(image,?selem=None)

用此函數(shù)比處理灰度圖像要快。

4、閉運(yùn)算(closing)

函數(shù):skimage.morphology.closing(image,?selem=None)

selem表示結(jié)構(gòu)元素,用于設(shè)定局部區(qū)域的形狀和大小。

先膨脹再腐蝕,可用來填充孔洞。

from skimage import io,color
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=color.rgb2gray(io.imread('d:/pic/mor.png'))
dst=sm.closing(img,sm.disk(9))  #用邊長(zhǎng)為5的圓形濾波器進(jìn)行膨脹濾波
plt.figure('morphology',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.axis('off')
plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst,plt.cm.gray)
plt.axis('off')

注意,如果處理圖像為二值圖像(只有0和1兩個(gè)值),則可以調(diào)用:

skimage.morphology.binary_closing(image,?selem=None)

用此函數(shù)比處理灰度圖像要快。

5、白帽(white-tophat)

函數(shù):skimage.morphology.white_tophat(image,?selem=None)

selem表示結(jié)構(gòu)元素,用于設(shè)定局部區(qū)域的形狀和大小。

將原圖像減去它的開運(yùn)算值,返回比結(jié)構(gòu)化元素小的白點(diǎn)

from skimage import io,color
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=color.rgb2gray(io.imread('d:/pic/mor.png'))
dst=sm.white_tophat(img,sm.square(21))  
plt.figure('morphology',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.axis('off')
plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst,plt.cm.gray)
plt.axis('off')

6、黑帽(black-tophat)

函數(shù):skimage.morphology.black_tophat(image,?selem=None)

selem表示結(jié)構(gòu)元素,用于設(shè)定局部區(qū)域的形狀和大小。

將原圖像減去它的閉運(yùn)算值,返回比結(jié)構(gòu)化元素小的黑點(diǎn),且將這些黑點(diǎn)反色。

from skimage import io,color
import skimage.morphology as sm
import matplotlib.pyplot as plt
img=color.rgb2gray(io.imread('d:/pic/mor.png'))
dst=sm.black_tophat(img,sm.square(21))  
plt.figure('morphology',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.axis('off')
plt.subplot(122)
plt.title('morphological image')
plt.imshow(dst,plt.cm.gray)
plt.axis('off')

原文鏈接:https://www.cnblogs.com/denny402/p/5132677.html

欄目分類
最近更新