嘿,我用Opencv3.3和Pyhton2.7来识别图像中的迷宫。我必须在图像中找到迷宫的最外面的极限。我试着关闭迷宫的入口和出口缝隙,找到最外面的形状。我在this上工作以弥补空白,但这对我的问题是无用的,因为我需要这些空白来解决迷宫。
这是原图

我想找到迷宫最外面的极限。
这就是我想要的

如何提取最外层轮廓?
发布于 2018-08-30 11:34:29
我会用numpy而不是OpenCV来实现这一点,但是两者是兼容的,所以无论如何都可以进行混合和匹配,或者,一旦您了解了我是如何处理它的,就可以将这种技术应用于OpenCV。
该策略是将每一行的所有像素相加,并生成一个像素宽的图像(如下图所示),即每一行中所有像素的总和。然后,我在该列中找到最大值,除以它,将一切正常化到0..100范围。现在,在这个单一像素宽的图像中,任何小于30个像素的像素都意味着相应的行在原始图像中的白色像素小于30%,也就是说,它基本上是黑色的。
然后,对所有列进行相同的求和,以生成列和--在下面的图像底部显示:

我认为有些人把这种技术称为“投影”,如果你想谷歌的话。
因此,代码看起来如下:
#!/usr/local/bin/python3
import numpy as np
from PIL import Image
# Load image - you can use OpenCV "imread()" just the same and convert to grayscale
im = np.array(Image.open('maze.jpg').convert('L'))
# Get height and width
h,w = im.shape[0:2]
# Make a single pixel wide column, same height as image to store row sums in
rowsums=np.empty((h))
# Sum all pixels in each row
np.sum(im,axis=1,out=rowsums)
# Normalize to range 0..100, if rowsum[i] < 30 that means fewer than 30% of the pixels in row i are white
rowsums /= np.max(rowsums)/100
# Find first and last row that is largely black
first = last = -1
for r in range(h):
if first < 0 and rowsums[r] < 30:
first = r
if rowsums[r] < 30:
last = r
print(first,last)
# Make a single pixel tall row, same width as image to store col sums in
colsums=np.empty((w))
# Sum all pixels in each col
np.sum(im,axis=0,out=colsums)
# Normalize to range 0..100, if colsum[i] < 30 that means fewer than 30% of the pixels in col i are white
colsums /= np.max(colsums)/100
# Find first and last col that is largely black
first = last = -1
for c in range(w):
if first < 0 and colsums[c] < 30:
first = c
if colsums[c] < 30:
last = c
print(first,last)产出:
62 890
36 1509迷宫的顶部是第62行,下面的是第890行。迷宫的左列是36列,最右边的列是col 1509。
如果我用一个80%的透明红色矩形来匹配这些位置,我会得到:

https://stackoverflow.com/questions/52080875
复制相似问题