Python數(shù)學形態(tài)學實例分析
更新時間:2019年09月06日 09:24:31 作者:cakincqm
這篇文章主要介紹了Python數(shù)學形態(tài)學,結合實例形式分析了Python數(shù)學運算與圖形繪制相關操作技巧,需要的朋友可以參考下
本文實例講述了Python數(shù)學形態(tài)學。分享給大家供大家參考,具體如下:
一 原始隨機圖像
1、代碼
import numpy as np import matplotlib.pyplot as plt square = np.zeros((32,32))#全0數(shù)組 square[10:20,10:20]=1#把其中一部分設置為1 x, y =(32*np.random.random((2,15))).astype(np.int)#隨機位置 square[x,y]=1#把隨機位置設置為1 plt.imshow(square)#原始隨機圖像 plt.show()
2、運行結果

二 開運算
1、代碼
import numpy as np import matplotlib.pyplot as plt from scipy import ndimage square = np.zeros((32,32))#全0數(shù)組 square[10:20,10:20]=1#把其中一部分設置為1 x, y =(32*np.random.random((2,15))).astype(np.int)#隨機位置 square[x,y]=1#把隨機位置設置為1 open_square = ndimage.binary_opening(square)#開運算 plt.imshow(open_square) plt.show()
2、運行結果

三 膨脹運算
1、代碼
import numpy as np import matplotlib.pyplot as plt from scipy import ndimage square = np.zeros((32,32))#全0數(shù)組 square[10:20,10:20]=1#把其中一部分設置為1 x, y =(32*np.random.random((2,15))).astype(np.int)#隨機位置 square[x,y]=1#把隨機位置設置為1 eroded_square = ndimage.binary_erosion(square)#膨脹運算 plt.imshow(eroded_square) plt.show()
2、運行結果

四 閉運算
1、代碼
import numpy as np import matplotlib.pyplot as plt from scipy import ndimage square = np.zeros((32,32))#全0數(shù)組 square[10:20,10:20]=1#把其中一部分設置為1 x, y =(32*np.random.random((2,15))).astype(np.int)#隨機位置 square[x,y]=1#把隨機位置設置為1 closed_square = ndimage.binary_closing(square)#閉運算 plt.imshow(closed_square) plt.show()
2、運行結果

更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學運算技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設計有所幫助。
相關文章
python操作數(shù)據(jù)庫獲取結果之fetchone和fetchall的區(qū)別說明
這篇文章主要介紹了python操作數(shù)據(jù)庫獲取結果之fetchone和fetchall的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
2021-04-04 
