欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python圖像處理之膨脹與腐蝕的操作

 更新時間:2021年02月07日 15:05:58   作者:Warmer_Sweeter  
這篇文章主要介紹了Python圖像處理之膨脹與腐蝕的操作,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

引言

膨脹與腐蝕是圖像處理中兩種最基本的形態(tài)學操作,膨脹將目標點融合到背景中,向外部擴展,腐蝕與膨脹意義相反,消除連通的邊界,使邊界向內收縮。在本文中我們將了解使用內核的圖像膨脹與腐蝕的基本原理。

讓我們開始吧,同樣我們需要導入必需的庫。

import numpy as np
import matplotlib.pyplot as plt
from skimage.io import imread, imshow
from skimage.draw import circle
from skimage.morphology import erosion, dilation

首先讓我們創(chuàng)建一個容易操作的形狀--一個簡單的圓。

circ_image = np.zeros((100, 100))
circ_image[circle(50, 50, 25)] = 1
imshow(circ_image);

現(xiàn)在讓我們定義一個內核。

cross = np.array([[0,1,0],
   [1,1,1],
   [0,1,0]])
imshow(cross, cmap = 'gray');

將腐蝕函數(shù)應用到創(chuàng)建的圓上。

eroded_circle = erosion(circ_image, cross)
imshow(eroded_circle);

圖像看起來幾乎一模一樣。要看到那些微小的差異,我們必須仔細查看圖像。

linecolor = 'red'
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
ax[0].imshow(circ_image, cmap = 'gray');
ax[0].set_title('Original', fontsize = 19)
ax[0].axvline(x = 25, color = linecolor)
ax[0].axvline(x = 75, color = linecolor)
ax[0].axhline(y = 25, color = linecolor)
ax[0].axhline(y = 75, color = linecolor)
ax[1].imshow(eroded_circle, cmap = 'gray');
ax[1].set_title('Eroded', fontsize = 19)
ax[1].axvline(x = 25, color = linecolor)
ax[1].axvline(x = 75, color = linecolor)
ax[1].axhline(y = 25, color = linecolor)
ax[1].axhline(y = 75, color = linecolor)
fig.tight_layout()

我們可以看到,被腐蝕的圓已經略微縮小了。這就是腐蝕一個對象的意義。如果我們對腐蝕函數(shù)進行迭代,它的效果會變得非常明顯。

def multi_erosion(image, kernel, iterations):
 for i in range(iterations):
 image = erosion(image, kernel)
 return image
ites = [2,4,6,8,10,12,14,16,18,20]
fig, ax = plt.subplots(2, 5, figsize=(17, 5))
for n, ax in enumerate(ax.flatten()):
 ax.set_title(f'Iterations : {ites[n]}', fontsize = 16)
 new_circle = multi_erosion(circ_image, cross, ites[n])
 ax.imshow(new_circle, cmap = 'gray');
 ax.axis('off')
fig.tight_layout()

上圖清楚地顯示了圖像是如何被腐蝕的?,F(xiàn)在讓我們嘗試改變內核,如果我們使用水平線和垂直線內核代替交叉內核會怎樣呢?

h_line = np.array([[0,0,0],
   [1,1,1],
   [0,0,0]])
v_line = np.array([[0,1,0],
   [0,1,0],
   [0,1,0]])
fig, ax = plt.subplots(1, 2, figsize=(15, 5))
ax[0].imshow(h_line, cmap='gray');
ax[1].imshow(v_line, cmap='gray');
fig.tight_layout()

ites = [2,4,6,8,10,12,14,16,18,20]
fig, ax = plt.subplots(2, 5, figsize=(17, 5))
for n, ax in enumerate(ax.flatten()):
 ax.set_title(f'Horizontal Iterations : {ites[n]}', fontsize = 12)
 new_circle = multi_erosion(circ_image, h_line, ites[n])
 ax.imshow(new_circle, cmap = 'gray');
 ax.axis('off')
fig.tight_layout()
fig, ax = plt.subplots(2, 5, figsize=(17, 5))
for n, ax in enumerate(ax.flatten()):
 ax.set_title(f'Vertical Iterationss : {ites[n]}', fontsize = 12)
 new_circle = multi_erosion(circ_image, v_line, ites[n])
 ax.imshow(new_circle, cmap = 'gray');
 ax.axis('off')
fig.tight_layout()

正如我們所看到的,水平和垂直的腐蝕以不同的方式影響著圖像。使用水平內核我們得到一個垂直方向細長的圓;而使用垂直內核我們得到一個水平方向細長的圓。

你可能會奇怪,為什么使用垂直內核,會得到一個水平方向細長的圓呢?

因為腐蝕函數(shù)是分別尋找垂直和水平的線條,并慢慢把它們削掉。膨脹函數(shù)將會讓我們更清晰的理解這一點。

使用下面的函數(shù)設置處理的圖像、膨脹內核以及迭代次數(shù)。

def multi_dilation(image, kernel, iterations):
 for i in range(iterations):
 image = dilation(image, kernel)
 return image

讓我們看一下處理后的圖像有什么不同。

dilated_circle = multi_dilation(circ_image, cross, 1)
linecolor = 'red'
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
ax[0].imshow(circ_image, cmap = 'gray');
ax[0].set_title('Original', fontsize = 19)
ax[0].axvline(x = 25, color = linecolor)
ax[0].axvline(x = 75, color = linecolor)
ax[0].axhline(y = 25, color = linecolor)
ax[0].axhline(y = 75, color = linecolor)
ax[1].imshow(dilated_circle, cmap = 'gray');
ax[1].set_title('Dilated', fontsize = 19)
ax[1].axvline(x = 25, color = linecolor)
ax[1].axvline(x = 75, color = linecolor)
ax[1].axhline(y = 25, color = linecolor)
ax[1].axhline(y = 75, color = linecolor)
fig.tight_layout()

可以清楚地看到圓現(xiàn)在已經越過了紅線,這清楚地表明它已經擴大了。現(xiàn)在讓我們對水平和垂直擴張進行迭代。

ites = [2,4,6,8,10,12,14,16,18,20]
fig, ax = plt.subplots(2, 5, figsize=(17, 5))
for n, ax in enumerate(ax.flatten()):
 ax.set_title(f'Horizontal Iterations : {ites[n]}', fontsize = 
   12)
 new_circle = multi_dilation(circ_image, h_line, ites[n])
 ax.imshow(new_circle, cmap = 'gray');
 ax.axis('off')
fig.tight_layout()
fig, ax = plt.subplots(2, 5, figsize=(17, 5))
for n, ax in enumerate(ax.flatten()):
 ax.set_title(f'Vertical Iterationss : {ites[n]}', fontsize = 12)
 new_circle = multi_dilation(circ_image, v_line, ites[n])
 ax.imshow(new_circle, cmap = 'gray');
 ax.axis('off')
fig.tight_layout()

現(xiàn)在可以非常清楚地看到,水平擴張增加了圖像寬度,而垂直擴張增加了圖像高度。

現(xiàn)在我們已經了解了膨脹與腐蝕的基本原理,下面來看一個相對復雜的圖像。

complex_image = imread('complex_image.png')
imshow(complex_image);

在上面的圖像中,我們看到了水平線、垂直線和圓的混合物。我們可以使用膨脹和腐蝕函數(shù)孤立地觀察每一種形狀。

為了得到圓,我們可以先腐蝕垂直的線,再腐蝕水平的線。但要記住最后要對圖像進行膨脹,因為腐蝕函數(shù)同樣腐蝕了圓。

step_1 = multi_erosion(complex_image, h_line,3)
step_2 = multi_erosion(step_1, v_line,3)
step_3 = multi_dilation(step_2, h_line,3)
step_4 = multi_dilation(step_3, v_line,3)
steps = [step_1, step_2, step_3, step_4]
names = ['Step 1', 'Step 2', 'Step 3', 'Step 4']
fig, ax = plt.subplots(2, 2, figsize=(10, 10))
for n, ax in enumerate(ax.flatten()):
 ax.set_title(f'{names[n]}', fontsize = 22)
 ax.imshow(steps[n], cmap = 'gray');
 ax.axis('off')
fig.tight_layout()

同樣,下面的代碼將得到水平的線。

step_1 = multi_erosion(complex_image, cross, 20)
step_2 = multi_dilation(step_1, h_line, 20)
step_3 = multi_dilation(step_2, v_line,2)
steps = [step_1, step_2, step_3]
names = ['Step 1', 'Step 2', 'Step 3']
fig, ax = plt.subplots(1, 3, figsize=(10, 10))
for n, ax in enumerate(ax.flatten()):
 ax.set_title(f'{names[n]}', fontsize = 22)
 ax.imshow(steps[n], cmap = 'gray');
 ax.axis('off')
fig.tight_layout()

為了得到垂直的線,我們可以創(chuàng)建一個新的內核。

long_v_line = np.array([[0,1,0],
   [0,1,0],
   [0,1,0],
   [0,1,0],
   [0,1,0]])
step_1 = multi_erosion(complex_image, long_v_line, 10)
step_2 = multi_dilation(step_1 ,long_v_line, 10)
steps = [step_1, step_2]
names = ['Step 1', 'Step 2']
fig, ax = plt.subplots(1, 2, figsize=(10, 10))
for n, ax in enumerate(ax.flatten()):
 ax.set_title(f'{names[n]}', fontsize = 22)
 ax.imshow(steps[n], cmap = 'gray');
 ax.axis('off')
fig.tight_layout()

注意,內核并不局限于本文中提到的這幾種,可以根據(jù)不同的需求自己定義合適的內核。

總結

內核腐蝕和膨脹是圖像處理領域需要理解的基本概念。它們甚至可能是任何圖像處理模塊的第一課。直觀地理解它們將是你以后在這個領域成功的關鍵。

到此這篇關于Python圖像處理之膨脹與腐蝕的操作的文章就介紹到這了,更多相關Python圖像膨脹與腐蝕內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • python爬蟲之BeautifulSoup 使用select方法詳解

    python爬蟲之BeautifulSoup 使用select方法詳解

    本篇文章主要介紹了python爬蟲之BeautifulSoup 使用select方法詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Python爬蟲之使用BeautifulSoup和Requests抓取網(wǎng)頁數(shù)據(jù)

    Python爬蟲之使用BeautifulSoup和Requests抓取網(wǎng)頁數(shù)據(jù)

    這篇文章主要介紹了Python爬蟲之使用BeautifulSoup和Requests抓取網(wǎng)頁數(shù)據(jù),本篇文章將介紹如何使用 Python 編寫一個簡單的網(wǎng)絡爬蟲,從網(wǎng)頁中提取有用的數(shù)據(jù),需要的朋友可以參考下
    2023-04-04
  • python實現(xiàn)基于兩張圖片生成圓角圖標效果的方法

    python實現(xiàn)基于兩張圖片生成圓角圖標效果的方法

    這篇文章主要介紹了python實現(xiàn)基于兩張圖片生成圓角圖標效果的方法,實例分析了Python使用pil模塊進行圖片處理的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • python3 pathlib庫Path類方法總結

    python3 pathlib庫Path類方法總結

    這篇文章主要介紹了python3 pathlib庫Path類方法總結,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • 五個Python迷你版小程序附代碼

    五個Python迷你版小程序附代碼

    在使用Python的過程中,我最喜歡的就是Python的各種第三方庫,能夠完成很多操作。下面就給大家介紹5個通過 Python 構建的實戰(zhàn)項目,來實踐 Python 編程能力。歡迎收藏學習,喜歡點贊支持
    2021-11-11
  • Python爬蟲爬取屬于自己的地鐵線路圖

    Python爬蟲爬取屬于自己的地鐵線路圖

    這篇文章主要介紹了Python爬蟲爬取屬于自己的地鐵線路圖,下面文章主要事根據(jù)自己需要對地鐵路線進行爬取的實現(xiàn)過程,需要的小伙伴可以參考一下,希望對你有所幫助
    2021-12-12
  • Python 面向對象之類class和對象基本用法示例

    Python 面向對象之類class和對象基本用法示例

    這篇文章主要介紹了Python 面向對象之類class和對象基本用法,結合實例形式詳細分析了Python面向對象程序設計中類class和對象基本概念、原理、使用方法與操作注意事項,需要的朋友可以參考下
    2020-02-02
  • Python變量和數(shù)據(jù)類型和數(shù)據(jù)類型的轉換

    Python變量和數(shù)據(jù)類型和數(shù)據(jù)類型的轉換

    這篇文章主要介紹了Python變量和數(shù)據(jù)類型和數(shù)據(jù)類型的轉換,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • python?隨機生成emoji表情的方法實現(xiàn)

    python?隨機生成emoji表情的方法實現(xiàn)

    本文主要介紹了python?隨機生成emoji表情的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-09-09
  • python tkinter 獲得按鈕的文本值

    python tkinter 獲得按鈕的文本值

    這篇文章主要介紹了python tkinter 獲得按鈕的文本值,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04

最新評論