gpt4 book ai didi

python - 在OpenCv中从图像提取墙

转载 作者:行者123 更新时间:2023-12-02 17:07:57 25 4
gpt4 key购买 nike

我有一个迷宫的以下图像:

输入:

image

墙壁是白色的,道路是黑色的。如何在opencv的单独图像中提取其中一堵墙?

这是我想要的输出

输出:

Output

最佳答案

您可以使用flood fill algorithm的概念来解决此问题。

在输入图像中,请注意“墙”是如何区分的,并被黑色像素(路径)所相邻。当您在此“墙”内的任意一个像素处初始化算法时,它们将与图像的其余部分分开。

代码:

path = r'C:\Users\Desktop'
filename = 'input.png'

img = cv2.imread(os.path.join(path, filename))
cv2.imshow('Original', img)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 100, 255,cv2.THRESH_BINARY_INV)
cv2.imshow('thresh1', thresh)

enter image description here
im_floodfill = thresh.copy()

h, w = thresh.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)

cv2.floodFill(im_floodfill, mask, (0,0), 255)
cv2.imshow('im_floodfill', im_floodfill)

enter image description here
cv2.imshow('fin', cv2.bitwise_not(cv2.bitwise_not(im_floodfill) + thresh))

enter image description here

还有另一个示例,详细介绍 THIS POST上的此功能

关于python - 在OpenCv中从图像提取墙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50550654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com