Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用BFS标记连通元件网_Python_Algorithm_Python 2.7_Computer Vision_Graph Theory - Fatal编程技术网

Python 用BFS标记连通元件网

Python 用BFS标记连通元件网,python,algorithm,python-2.7,computer-vision,graph-theory,Python,Algorithm,Python 2.7,Computer Vision,Graph Theory,我正在使用BFS算法解决连接组件标记算法。原始图像im将标记为输出图像 当blob很小时,此代码可以工作。但是,当我将起点更改为有一个大blob时,代码要么达到递归的最大深度,要么出现分段错误。如何避免这些问题 import cv2 import numpy as np from collections import deque import sys import copy sys.setrecursionlimit(10000000) def bfs(queue, im, out, lab

我正在使用BFS算法解决连接组件标记算法。原始图像im将标记为输出图像

当blob很小时,此代码可以工作。但是,当我将起点更改为有一个大blob时,代码要么达到递归的最大深度,要么出现分段错误。如何避免这些问题

import cv2
import numpy as np
from collections import deque
import sys
import copy

sys.setrecursionlimit(10000000)

def bfs(queue, im, out, label):
    if len(queue) > 0:
        pixel = queue.pop()
        print pixel
        out[pixel] = label

        M, N = im.shape
        for n in neighbors(pixel, M, N):
            if out[n] == 0 and im[n] == im[pixel]:
                queue.append(n)
        out = bfs(queue, im, out, label)
    return out

def neighbors(pixel, M, N):
    if pixel[0] == M - 1 and pixel[1] == N - 1:
        return [(M-2, N-1), (M-1, N-2)]
    elif pixel == (0,0):
        return [(0,1),(1,0)]
    elif pixel == (M - 1, 0):
        return [(M-1, 1), (M-2, 0)]
    elif pixel == (0, N - 1):
        return [(1, N-1), (0, N-2)]
    elif pixel[0] == 0:
        return [(1,pixel[1]), (0, pixel[1]-1), (0 ,pixel[1] + 1)]
    elif pixel[1] == 0:
        return [(pixel[0], 1), (pixel[0]-1, 0), (pixel[0] + 1, 0)]
    elif pixel[0] == M - 1:
        return [(pixel[0], pixel[1] + 1), (pixel[0] - 1, pixel[1]), (pixel[0], pixel[1] - 1)]
    elif pixel[1] == N - 1:
        return [(pixel[0] + 1, pixel[1]), (pixel[0], pixel[1] - 1), (pixel[0] - 1, pixel[1])]
    else: 
        return [(pixel[0] + 1, pixel[1]), (pixel[0], pixel[1] + 1),\
                (pixel[0] - 1, pixel[1]), (pixel[0], pixel[1] - 1)]


im = cv2.imread('33039.png', cv2.IMREAD_GRAYSCALE)
out = np.zeros(im.shape)

queue = deque()
queue.append((10,10))
out = bfs(queue, im, out, 1)

BFS可以很容易地以迭代的方式实现。以下是CPP中的一个示例:

void
bfs(const vector< vector<int> > &g)
{
        // visit self, then neighbors, store neighbors using stack.
        vector<bool> visited(g.size(), false);
        vector<int> s;
        Queue Q;
        Q.enqueue(6);
        while (Q.size() > 0)
        {
                int t = Q.dequeue();
                if (!visited[t])
                {
                        cout << "visit node: " << t << endl;
                        visited[t] = true;
                        for (int i = 0; i < g[t].size(); ++i)
                        {
                                if (!visited[g[t][i]])
                                {
                                        Q.enqueue(g[t][i]);
                                }
                        }
                }
        }        
}
void
bfs(常数向量<向量>&g)
{
//访问自己,然后访问邻居,使用堆栈存储邻居。
访问向量(g.size(),false);
向量s;
队列Q;
Q.排队(6);
而(Q.size()>0)
{
int t=Q.dequeue();
如果(!已访问[t])
{

cout对于python解决方案,我经常使用这种非递归解决方案:

def bfs(graph,start):
    # Breadth-first search to find pixels connected
    # graph is array with some pixels true and others false
    # start is x, y
    if not graph[start[0]][start[1]]:
        return
    visited = []
    w, h = len(graph)-1, len(graph[0])-1
    queue = [start]
    while queue:
        x, y = queue.pop(0)
        neighbors = []
        if x<w:
            neighbors.append((x+1,y))
        if x>0:
            neighbors.append((x-1,y))
        if y<h:
            neighbors.append((x,y+1))
        if y>0:
            neighbors.append((x,y-1))   
        for n in neighbors:
            if n not in visited and graph[n[0]][n[1]]:
                visited.append(n)
                queue.append(n)
    return visited
def bfs(图形,开始):
#宽度优先搜索以查找连接的像素
#图形是一个数组,其中一些像素为真,另一些像素为假
#起点是x,y
如果不是图形[start[0]][start[1]]:
返回
已访问=[]
w、 h=len(图)-1,len(图[0])-1
队列=[开始]
排队时:
x、 y=queue.pop(0)
邻居=[]
如果x0:
邻域追加((x-1,y))
如果y0:
邻域追加((x,y-1))
对于邻居中的n:
如果n不在已访问和图形中[n[0][n[1]]:
已访问。追加(n)
queue.append(n)
回访
这个版本是我为以前的项目编写的,它以一个2-d python数组作为输入,并探索相邻的真像素或一个像素