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

運(yùn)用Python3實(shí)現(xiàn)Two-Pass算法檢測區(qū)域連通性

 更新時(shí)間:2021年08月18日 17:16:38   作者:DECHIN  
如何高效的檢測出連通區(qū)域的流動(dòng)性是大家一直關(guān)注的話題,這篇文章主要介紹了運(yùn)用Python3實(shí)現(xiàn)Two-Pass算法檢測區(qū)域連通性,感興趣的朋友可以一起來看看

技術(shù)背景

連通性檢測是圖論中常常遇到的一個(gè)問題,我們可以用五子棋的思路來理解這個(gè)問題五子棋中,橫、豎、斜相鄰的兩個(gè)棋子,被認(rèn)為是相連接的,而一樣的道理,在一個(gè)二維的圖中,只要在橫、豎、斜三個(gè)方向中的一個(gè)存在相鄰的情況,就可以認(rèn)為圖上相連通的。比如以下案例中的python數(shù)組,3號(hào)元素和5號(hào)元素就是相連接的,5號(hào)元素和6號(hào)元素也是相連接的,因此這三個(gè)元素實(shí)際上是屬于同一個(gè)區(qū)域的:

array([[0, 3, 0],
       [0, 5, 0],
       [6, 0, 0]])

而再如下面這個(gè)例子,其中的1、2、3三個(gè)元素是相連的,4、5、6三個(gè)元素也是相連的,但是這兩個(gè)區(qū)域不存在連接性,因此這個(gè)網(wǎng)格被分成了兩個(gè)區(qū)域:

array([[1, 0, 4],
       [2, 0, 5],
       [3, 0, 6]])

那么如何高效的檢測一張圖片或者一個(gè)矩陣中的所有連通區(qū)域并打上標(biāo)簽,就是我們所關(guān)注的一個(gè)問題。

Two-Pass算法

一個(gè)典型的連通性檢測的方案是Two-Pass算法,該算法可以用如下的一張動(dòng)態(tài)圖來演示:

該算法的核心在于用兩次的遍歷,為所有的節(jié)點(diǎn)打上分區(qū)的標(biāo)簽,如果是不同的分區(qū),就會(huì)打上不同的標(biāo)簽。其基本的算法步驟可以用如下語言進(jìn)行概述:

  1. 遍歷網(wǎng)格節(jié)點(diǎn),如果網(wǎng)格的上、左、左上三個(gè)格點(diǎn)不存在元素,則為當(dāng)前網(wǎng)格打上新的標(biāo)簽,同時(shí)標(biāo)簽編號(hào)加一;
  2. 當(dāng)上、左、左上的網(wǎng)格中存在一個(gè)元素時(shí),將該元素值賦值給當(dāng)前的網(wǎng)格作為標(biāo)簽;
  3. 當(dāng)上、左、左上的網(wǎng)格中有多個(gè)元素時(shí),取最低值作為當(dāng)前網(wǎng)格的標(biāo)簽;
  4. 在標(biāo)簽賦值時(shí),留意標(biāo)簽上邊和左邊已經(jīng)被遍歷過的4個(gè)元素,將4個(gè)元素中的最低值與這四個(gè)元素分別添加到Union的數(shù)據(jù)結(jié)構(gòu)中(參考鏈接1);
  5. 再次遍歷網(wǎng)格節(jié)點(diǎn),根據(jù)Union數(shù)據(jù)結(jié)構(gòu)中的值刷新網(wǎng)格中的標(biāo)簽值,最終得到劃分好區(qū)域和標(biāo)簽的元素矩陣。

測試數(shù)據(jù)的生成

這里我們以Python3為例,可以用Numpy來產(chǎn)生一系列隨機(jī)的0-1矩陣,這里我們產(chǎn)生一個(gè)20*20大小的矩陣:

# two_pass.py

import numpy as np
import matplotlib.pyplot as plt

if __name__ == "__main__":
    np.random.seed(1)
    graph = np.random.choice([0,1],size=(20,20))
    print (graph)

    plt.figure()
    plt.imshow(graph)
    plt.savefig('random_bin_graph.png')

執(zhí)行的輸出結(jié)果如下:

$ python3 two_pass.py 
[[1 1 0 0 1 1 1 1 1 0 0 1 0 1 1 0 0 1 0 0]
 [0 1 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 0 0 0]
 [1 1 1 1 1 1 0 1 1 0 0 1 0 0 1 1 1 0 1 0]
 [0 1 1 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1 0]
 [1 0 0 1 1 0 1 1 0 1 0 0 1 1 1 0 1 1 0 1]
 [1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0]
 [0 1 1 1 1 1 1 0 0 1 1 0 0 1 0 0 0 1 1 1]
 [1 1 0 1 0 1 0 0 0 1 1 1 0 1 0 0 0 0 1 0]
 [1 0 1 1 1 0 0 0 0 0 0 1 0 0 1 0 0 1 1 0]
 [0 0 1 0 0 0 0 1 0 0 0 0 1 1 0 0 1 1 1 0]
 [0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 0 1 1 1 0]
 [1 1 1 1 0 1 0 0 1 0 1 0 1 1 0 1 1 0 1 1]
 [1 0 1 0 1 0 1 1 1 1 1 1 0 0 1 1 0 0 0 1]
 [1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 0 0 0 1]
 [0 1 0 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 0 1]
 [0 1 0 0 0 1 0 1 0 1 1 1 0 1 0 1 1 1 1 0]
 [0 1 0 0 0 0 1 1 0 1 1 0 0 1 1 1 1 1 1 1]
 [0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 0 0 0]
 [1 0 1 0 1 0 0 0 0 0 0 1 0 0 0 1 0 1 1 0]
 [0 1 1 0 1 0 1 0 1 1 0 0 1 0 0 0 0 0 1 1]]

同時(shí)會(huì)生成一張網(wǎng)格的圖片:


其實(shí)從這個(gè)圖片中我們可以看出,圖片的上面部分幾乎都是連接在一起的,只有最下面存在幾個(gè)獨(dú)立的區(qū)域。

Two-Pass算法的實(shí)現(xiàn)

這里需要說明的是,因?yàn)槲覀儾]有使用Union的數(shù)據(jù)結(jié)構(gòu),而是只使用了Python的字典數(shù)據(jù)結(jié)構(gòu),因此代碼寫起來會(huì)比較冗余而且不是那么美觀,但是這里我們主要的目的是先用代解決這一實(shí)際問題,因此代碼亂就亂一點(diǎn)吧。

# two_pass.py

import numpy as np
import matplotlib.pyplot as plt
from copy import deepcopy

def first_pass(g) -> list:
    graph = deepcopy(g)
    height = len(graph)
    width = len(graph[0])
    label = 1
    index_dict = {}
    for h in range(height):
        for w in range(width):
            if graph[h][w] == 0:
                continue
            if h == 0 and w == 0:
                graph[h][w] = label
                label += 1
                continue
            if h == 0 and graph[h][w-1] > 0:
                graph[h][w] = graph[h][w-1]
                continue
            if w == 0 and graph[h-1][w] > 0:
                if graph[h-1][w] <= graph[h-1][min(w+1, width-1)]:
                    graph[h][w] = graph[h-1][w]
                    index_dict[graph[h-1][min(w+1, width-1)]] = graph[h-1][w]
                elif graph[h-1][min(w+1, width-1)] > 0:
                    graph[h][w] = graph[h-1][min(w+1, width-1)]
                    index_dict[graph[h-1][w]] = graph[h-1][min(w+1, width-1)]
                continue
            if h == 0 or w == 0:
                graph[h][w] = label
                label += 1
                continue
            neighbors = [graph[h-1][w], graph[h][w-1], graph[h-1][w-1], graph[h-1][min(w+1, width-1)]]
            neighbors = list(filter(lambda x:x>0, neighbors))
            if len(neighbors) > 0:
                graph[h][w] = min(neighbors)
                for n in neighbors:
                    if n in index_dict:
                        index_dict[n] = min(index_dict[n], min(neighbors))
                    else:
                        index_dict[n] = min(neighbors)
                continue
            graph[h][w] = label
            label += 1
    return graph, index_dict

def remap(idx_dict) -> dict:
    index_dict = deepcopy(idx_dict)
    for id in idx_dict:
        idv = idx_dict[id]
        while idv in idx_dict:
            if idv == idx_dict[idv]:
                break
            idv = idx_dict[idv]
        index_dict[id] = idv
    return index_dict

def second_pass(g, index_dict) -> list:
    graph = deepcopy(g)
    height = len(graph)
    width = len(graph[0])
    for h in range(height):
        for w in range(width):
            if graph[h][w] == 0:
                continue
            if graph[h][w] in index_dict:
                graph[h][w] = index_dict[graph[h][w]]
    return graph

def flatten(g) -> list:
    graph = deepcopy(g)
    fgraph = sorted(set(list(graph.flatten())))
    flatten_dict = {}
    for i in range(len(fgraph)):
        flatten_dict[fgraph[i]] = i
    graph = second_pass(graph, flatten_dict)
    return graph

if __name__ == "__main__":
    np.random.seed(1)
    graph = np.random.choice([0,1],size=(20,20))
    graph_1, idx_dict = first_pass(graph)
    idx_dict = remap(idx_dict)
    graph_2 = second_pass(graph_1, idx_dict)
    graph_3 = flatten(graph_2)
    print (graph_3)

    plt.subplot(131)
    plt.imshow(graph)
    plt.subplot(132)
    plt.imshow(graph_3)
    plt.subplot(133)
    plt.imshow(graph_3>0)
    plt.savefig('random_bin_graph.png')

完整代碼的輸出如下所示:

$ python3 two_pass.py 
[[1 1 0 0 1 1 1 1 1 0 0 1 0 1 1 0 0 1 0 0]
 [0 1 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 0 0 0]
 [1 1 1 1 1 1 0 1 1 0 0 1 0 0 1 1 1 0 1 0]
 [0 1 1 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1 0]
 [1 0 0 1 1 0 1 1 0 1 0 0 1 1 1 0 1 1 0 1]
 [1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0]
 [0 1 1 1 1 1 1 0 0 1 1 0 0 1 0 0 0 1 1 1]
 [1 1 0 1 0 1 0 0 0 1 1 1 0 1 0 0 0 0 1 0]
 [1 0 1 1 1 0 0 0 0 0 0 1 0 0 1 0 0 1 1 0]
 [0 0 1 0 0 0 0 1 0 0 0 0 1 1 0 0 1 1 1 0]
 [0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 0 1 1 1 0]
 [1 1 1 1 0 1 0 0 1 0 1 0 1 1 0 1 1 0 1 1]
 [1 0 1 0 1 0 1 1 1 1 1 1 0 0 1 1 0 0 0 1]
 [1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 0 0 0 1]
 [0 1 0 2 0 0 0 0 1 1 0 0 0 1 0 1 1 0 0 1]
 [0 1 0 0 0 1 0 1 0 1 1 1 0 1 0 1 1 1 1 0]
 [0 1 0 0 0 0 1 1 0 1 1 0 0 1 1 1 1 1 1 1]
 [0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 0 0 0]
 [3 0 3 0 4 0 0 0 0 0 0 5 0 0 0 1 0 1 1 0]
 [0 3 3 0 4 0 6 0 7 7 0 0 5 0 0 0 0 0 1 1]]

同樣的我們可以看看此時(shí)得到的新的圖像:


這里我們并列的畫了三張圖,第一張圖是原圖,第二張圖是劃分好區(qū)域和標(biāo)簽的圖,第三張是對(duì)第二張圖進(jìn)行二元化的結(jié)果,以確保在運(yùn)算過程中沒有丟失原本的信息。經(jīng)過確認(rèn)這個(gè)標(biāo)簽的結(jié)果劃分是正確的,但是因?yàn)樯婕暗揭恍┧惴▽?shí)現(xiàn)的細(xì)節(jié),這里我們還是需要展開來介紹一下。

算法的執(zhí)行流程

if __name__ == "__main__":
    np.random.seed(1)
    graph = np.random.choice([0,1],size=(20,20))
    graph_1, idx_dict = first_pass(graph)
    idx_dict = remap(idx_dict)
    graph_2 = second_pass(graph_1, idx_dict)
    graph_3 = flatten(graph_2)

這個(gè)部分是算法的核心框架,在本文中的算法實(shí)現(xiàn)流程為:先用first_pass遍歷一遍網(wǎng)格節(jié)點(diǎn),按照上一個(gè)章節(jié)中介紹的Two-Pass算法打上標(biāo)簽,并獲得一個(gè)映射關(guān)系;然后用remap將上面得到的映射關(guān)系做一個(gè)重映射,確保每一個(gè)級(jí)別的映射都對(duì)應(yīng)到了最根部(可以聯(lián)系參考鏈接1的內(nèi)容進(jìn)行理解,雖然這里沒有使用Union的數(shù)據(jù)結(jié)構(gòu),但是本質(zhì)上還是一個(gè)樹形的結(jié)構(gòu),需要做一個(gè)重映射);然后用second_pass執(zhí)行Two-Pass算法的第二次遍歷,得到一組打上了新的獨(dú)立標(biāo)簽的網(wǎng)格節(jié)點(diǎn);最后需要用flatten將標(biāo)簽進(jìn)行壓平,因?yàn)榍懊嬗成涞年P(guān)系,有可能導(dǎo)致標(biāo)簽不連續(xù),所以我們這里又做了一次映射,確保標(biāo)簽是連續(xù)變化的,實(shí)際應(yīng)用中可以不使用這一步。

標(biāo)簽的重映射

關(guān)于節(jié)點(diǎn)的遍歷,大家可以直接看算法代碼,這里需要額外講解的是標(biāo)簽的重映射模塊的代碼:

def remap(idx_dict) -> dict:
    index_dict = deepcopy(idx_dict)
    for id in idx_dict:
        idv = idx_dict[id]
        while idv in idx_dict:
            if idv == idx_dict[idv]:
                break
            idv = idx_dict[idv]
        index_dict[id] = idv
    return index_dict

這里的算法是先對(duì)得到的標(biāo)簽進(jìn)行遍歷,在字典中獲取當(dāng)前標(biāo)索引所對(duì)應(yīng)的值,作為新的索引,直到鍵跟值一致為止,相當(dāng)于在一個(gè)樹形的數(shù)據(jù)結(jié)構(gòu)中重復(fù)尋找父節(jié)點(diǎn)直到找到根節(jié)點(diǎn)。

其他的測試用例

這里我們可以再額外測試一些案例,比如增加幾個(gè)0元素使得網(wǎng)格節(jié)點(diǎn)更加稀疏:

graph = np.random.choice([0,0,0,1],size=(20,20))

得到的結(jié)果圖片如下所示:


還可以再稀疏一些:

graph = np.random.choice([0,0,0,0,0,1],size=(20,20))

得到的結(jié)果如下圖所示:


越是稀疏的圖,得到的分組結(jié)果就越分散。

總結(jié)概要

在本文中我們主要介紹了利用Two-Pass的算法來檢測區(qū)域連通性,并給出了Python3的代碼實(shí)現(xiàn),當(dāng)然在實(shí)現(xiàn)的過程中因?yàn)闆]有使用到Union這樣的數(shù)據(jù)結(jié)構(gòu),僅僅用了字典來存儲(chǔ)標(biāo)簽之間的關(guān)系,因此效率和代碼可讀性都會(huì)低一些,單純作為用例的演示和小規(guī)模區(qū)域劃分的計(jì)算是足夠用了。在該代碼實(shí)現(xiàn)方案中,還有一點(diǎn)與原始算法不一致的是,本實(shí)現(xiàn)方案中打新的標(biāo)簽是讀取上、上左和左三個(gè)方向的格點(diǎn),但是存儲(chǔ)標(biāo)簽的映射關(guān)系時(shí),是讀取了上、上左、上右和左這四個(gè)方向的格點(diǎn)。

參考鏈接

  1. https://blog.csdn.net/lichengyu/article/details/13986521
  2. https://www.cnblogs.com/riddick/p/8280883.html

到此這篇關(guān)于運(yùn)用Python3實(shí)現(xiàn)Two-Pass算法檢測區(qū)域連通性的文章就介紹到這了,更多相關(guān)Python3實(shí)現(xiàn)Two-Pass算法檢測區(qū)域流通內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python 添加用戶設(shè)置密碼并發(fā)郵件給root用戶

    python 添加用戶設(shè)置密碼并發(fā)郵件給root用戶

    這篇文章主要介紹了python 添加用戶設(shè)置密碼并發(fā)郵件給root用戶的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • python遞歸調(diào)用中的坑:打印有值, 返回卻None

    python遞歸調(diào)用中的坑:打印有值, 返回卻None

    這篇文章主要介紹了python遞歸調(diào)用中的坑:打印有值, 返回卻None,本文通過問題分析給出解決方法,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 學(xué)好python基本數(shù)據(jù)類型

    學(xué)好python基本數(shù)據(jù)類型

    這篇文章主要介紹了學(xué)好python基本數(shù)據(jù)類型,學(xué)習(xí)python基本數(shù)據(jù)類型我們需要了解基本數(shù)據(jù)類型有數(shù)字int、布爾值bool、字符串str、列表list、元組tuple、字典dict等,其中包括他們的基本用法和其常用的方法,下面來看看文章的具體介紹吧
    2021-12-12
  • python爬蟲框架Scrapy基本應(yīng)用學(xué)習(xí)教程

    python爬蟲框架Scrapy基本應(yīng)用學(xué)習(xí)教程

    這篇文章主要為大家介紹了python爬蟲框架Scrapy的基本應(yīng)用學(xué)習(xí)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-11-11
  • python sorted函數(shù)原理解析及練習(xí)

    python sorted函數(shù)原理解析及練習(xí)

    這篇文章主要介紹了python sorted函數(shù)原理解析及練習(xí),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • python cv2圖像質(zhì)量壓縮的算法示例

    python cv2圖像質(zhì)量壓縮的算法示例

    使用opencv對(duì)圖像進(jìn)行編碼,一方面是圖像二進(jìn)制傳輸?shù)男枰?,另一方面?duì)圖像壓縮。本文主要介紹了python cv2圖像質(zhì)量壓縮的算法示例,感興趣的可以了解一下
    2021-06-06
  • Python中的迭代和列表生成式

    Python中的迭代和列表生成式

    這篇文章主要介紹了Python中的迭代和列表生成式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教<BR>
    2024-02-02
  • 500行代碼使用python寫個(gè)微信小游戲飛機(jī)大戰(zhàn)游戲

    500行代碼使用python寫個(gè)微信小游戲飛機(jī)大戰(zhàn)游戲

    這篇文章主要介紹了500行代碼使用python寫個(gè)微信小游戲飛機(jī)大戰(zhàn)游戲,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-10-10
  • django中模板繼承與ModelForm實(shí)例詳解

    django中模板繼承與ModelForm實(shí)例詳解

    ModelForm類是form是組件中Form的一個(gè)子類,所以也是處理表單的,下面這篇文章主要給大家介紹了關(guān)于django中模板繼承與ModelForm的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • 關(guān)于初始種子自動(dòng)選取的區(qū)域生長實(shí)例(python+opencv)

    關(guān)于初始種子自動(dòng)選取的區(qū)域生長實(shí)例(python+opencv)

    今天小編就為大家分享一篇關(guān)于初始種子自動(dòng)選取的區(qū)域生長實(shí)例(python+opencv),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01

最新評(píng)論