亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

引言

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

讓我們開始吧,同樣我們需要導(dǎo)入必需的庫。

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)建一個(gè)容易操作的形狀--一個(gè)簡單的圓。

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

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

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

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

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

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

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()

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

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()

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

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()

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

你可能會(huì)奇怪,為什么使用垂直內(nèi)核,會(huì)得到一個(gè)水平方向細(xì)長的圓呢?

因?yàn)楦g函數(shù)是分別尋找垂直和水平的線條,并慢慢把它們削掉。膨脹函數(shù)將會(huì)讓我們更清晰的理解這一點(diǎn)。

使用下面的函數(shù)設(shè)置處理的圖像、膨脹內(nèi)核以及迭代次數(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)在已經(jīng)越過了紅線,這清楚地表明它已經(jīng)擴(kuò)大了?,F(xiàn)在讓我們對水平和垂直擴(kuò)張進(jì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)在可以非常清楚地看到,水平擴(kuò)張?jiān)黾恿藞D像寬度,而垂直擴(kuò)張?jiān)黾恿藞D像高度。

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

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

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

為了得到圓,我們可以先腐蝕垂直的線,再腐蝕水平的線。但要記住最后要對圖像進(jìn)行膨脹,因?yàn)楦g函數(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)建一個(gè)新的內(nèi)核。

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()

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

總結(jié)

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

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

相關(guān)文章

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

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

    本篇文章主要介紹了python爬蟲之BeautifulSoup 使用select方法詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    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 編寫一個(gè)簡單的網(wǎng)絡(luò)爬蟲,從網(wǎng)頁中提取有用的數(shù)據(jù),需要的朋友可以參考下
    2023-04-04
  • python實(shí)現(xiàn)基于兩張圖片生成圓角圖標(biāo)效果的方法

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

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

    python3 pathlib庫Path類方法總結(jié)

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

    五個(gè)Python迷你版小程序附代碼

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

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

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

    Python 面向?qū)ο笾恈lass和對象基本用法示例

    這篇文章主要介紹了Python 面向?qū)ο笾恈lass和對象基本用法,結(jié)合實(shí)例形式詳細(xì)分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中類class和對象基本概念、原理、使用方法與操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • Python變量和數(shù)據(jù)類型和數(shù)據(jù)類型的轉(zhuǎn)換

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

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

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

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

    python tkinter 獲得按鈕的文本值

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

最新評論