我一直在问几个问题,如何从软件上的照片中定位和提取迷宫, 但我从不同的照片中得到的答案都不适用,甚至连4张测试照片也不适用。 每次我调整代码使其适用于一张照片时,由于扭曲的角落/部分或光线等原因,其余照片都会失败。我觉得我需要找到一种对扭曲的图像和不同强度的光线或迷宫墙的不同颜色(迷宫内的线条)不敏感的方法
我一直在努力让它在没有运气的情况下工作了三个星期。在我放弃这个想法之前,我想问一下,是否可以只使用图像处理而不使用人工智能来定位和提取照片中的迷宫?如果是的话,你能告诉我怎么做吗
以下是代码和照片:
import cv2
import numpy as np
from skimage.exposure import rescale_intensity
from skimage.feature import corner_harris, corner_subpix, corner_peaks
from skimage.io import imread, imshow
from skimage.morphology import reconstruction, binary_erosion, skeletonize, dilation, square
from skimage.morphology.convex_hull import convex_hull_image
from skimage.util import invert
from skmpe import parameters, mpe, OdeSolverMethod
maze=cv2.imread("simple.jpg",0)
ret, maze=cv2.threshold(maze,100,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
h, w = maze.shape
seed = np.zeros_like(maze)
size = 40
hh = h // 2
hw = w // 2
seed[hh-size:hh+size, hw-size:hw+size] = maze[hh-size:hh+size, hw-size:hw+size]
rec1 = reconstruction(seed, maze)
seed2 = np.ones_like(rec1)
ker = np.ones((2,2))
rec1_thicker = cv2.erode(rec1, ker, iterations=1)
seed2 = seed2 * 255
size2 = 240
lhh = hh - size2
hhh = hh + size2
lhw = hw - size2
hhw = hw + size2
seed2[lhh:hhh, lhw:hhw]=rec1_thicker[lhh:hhh, lhw:hhw]
rec2 = reconstruction(seed2,rec1_thicker, method='erosion')
rec2_inv = invert(rec2 / 255.)
hull = convex_hull_image(rec2_inv)
hull_eroded = binary_erosion(hull, selem=np.ones((5,5)))
coords = corner_peaks(corner_harris(hull_eroded), min_distance=5, threshold_rel=0.02)
import matplotlib.pyplot as plt
fig, axe = plt.subplots(1,4,figsize=(16,8))
axe[0].imshow(maze, 'gray')
axe[1].imshow(rec1, 'gray')
axe[2].imshow(rec2, 'gray')
axe[3].imshow(hull, 'gray')
正如您可以看到的,第三个图是提取的迷宫,这段代码工作得很好,但就这两张照片而言,在本例中它们是simple.jpg
和“maze.jpg”
我已将所有4张测试照片上传到OneDrive,供有兴趣的人试用
更新1
我画出了所有的面具,看看每个面具都做了什么
mask = (sat < 16).astype(np.uint8) * 255
mask1 = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_RECT, (31, 31)))
mask2 = cv2.copyMakeBorder(mask1, 10, 10, 10, 10, cv2.BORDER_CONSTANT, 0)
mask3 = cv2.morphologyEx(mask2, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_RECT, (201, 201)))
plt.figure(figsize=(18, 8))
plt.subplot(1, 6, 1), plt.imshow(maze[..., ::-1]), plt.title('White balanced image')
plt.subplot(1, 6, 2), plt.imshow(sat, 'gray'), plt.title('Saturation channel')
plt.subplot(1, 6, 3), plt.imshow(mask, 'gray'), plt.title('sat < 16')
plt.subplot(1, 6, 4), plt.imshow(mask1, 'gray'), plt.title('closed')
plt.subplot(1, 6, 5), plt.imshow(mask2, 'gray'), plt.title('border')
plt.subplot(1, 6, 6), plt.imshow(mask3, 'gray'), plt.title('rect')
plt.tight_layout(), plt.show()
因此,在我看来,在整个图像周围形成边界的mask2是不必要的。 我们为什么需要mask2
我还发现mask2和mask3的分辨率在每个维度上都大2个像素:
maze.shape, sat.shape, mask.shape, mask1.shape, mask2.shape, mask3.shape
((4000, 1840, 3),
(4000, 1840),
(4000, 1840),
(4000, 1840),
(4002, 1842),
(4002, 1842))
为什么?
你真的想买这些6.9美元的菜吗,他
对于给定的四幅图像,我可以使用以下工作流获得相当好的结果:
reconstruction
方法李>结果如下:
maze.jpg
simple.jpg
middle.jpg
hard.jpg
这是完整的代码:
当然,这并不能保证,这种方法将适用于接下来的五幅左右的图像,您正在处理。通常,尝试标准化图像采集(旋转、照明),以获得更一致的图像。否则,您将需要某种机器学习方法
相关问题 更多 >
编程相关推荐