【发布时间】:2016-05-15 10:14:47
【问题描述】:
我有一个二值图像 - 黑色像素 (0) 和白色 (1)。
我想编写一个函数来获取图像和黑色像素的位置 - x 和 y 并返回与给定像素连接(连接 = 8 连接)的所有像素的位置,以及所有像素的总计数连接的像素。
例如,如果这是我的图像:
1 1 1 1
1 0 0 1
1 1 0 1
1 1 1 1
我给函数像素 (1,1) 的输出将是:
count=3
x=[1,2,2]
y=[1,1,2]
这就是我所做的:
def find_connectivity(img,i,j,count,x,y):
sys.setrecursionlimit(50000)
if(img[i][j]==0):
img[i][j]=2
count=count+1
xx=x
yy=y
xx=xx.append(i)
yy=yy.append(j)
rows=[-1, 0 ,1]
cols=[-1, 0 ,1]
for r in rows:
for c in cols:
if(img[i+r][j+c]==0):
count,x,y= find_connectivity(img,i+r,j+c,count,x,y)
return count,x,y
当我初始计数为 0,x 和 y 为 []。 例如在前面的例子中,对函数的调用是:
[count,x,y]=find_connectivity(img,1,1,0,[],[])
现在,此功能适用于相当大的群体 (5300+) - 但除此之外,我收到一条消息“python.exe 已停止工作”。 带有以下错误详细信息:
Application Timestamp: 5665e7c3
Fault Module Name: ntdll.dll
Fault Module Version: 6.1.7601.19045
Fault Module Timestamp: 56259295
Exception Code: c00000fd
Exception Offset: 00000000000510a1
OS Version: 6.1.7601.2.1.0.256.48
Locale ID: 1033
Additional Information 1: 2a7e
Additional Information 2: 2a7edff1635197ae43266443b8d73ae8
Additional Information 3: 6edf
Additional Information 4: 6edf0945fe78222ec4c56cecff238926
我猜是因为递归 - 但我想不出另一种有效的方法。
openCV/numpy/other 中是否有可以帮助我的函数? 或者有没有办法通过递归来防止崩溃?
谢谢!
【问题讨论】:
-
嘿,
2是否表示您访问过该节点?如果你知道怎么做,我建议你尝试广度优先搜索,它会运行得更快。 -
为什么不使用 OpenCV connectedComponents 或 findContours?没有必要重新发明轮子
标签: python opencv image-processing recursion crash