【问题标题】:Flood fill algorithm - bug in my implementation洪水填充算法 - 我的实现中的错误
【发布时间】:2020-10-11 17:06:51
【问题描述】:

我正在尝试学习如何在 Python 中实现洪水填充算法(基于队列)。 基于Rosetta页面,我准备了我的版本。不幸的是,我花了很多时间,我无法找到我有错误的地方。你能帮帮我吗?

我从 scikit-image page 修改了原始示例:

import numpy as np
import matplotlib.pyplot as plt
from skimage import data, filters
#from skimage.segmentation import flood, flood_fill
from flood_algorithm import flood_fill # <---------------- my file

checkers = data.checkerboard()

# Fill a square near the middle with value 127, starting at index (76, 76)

#filled_checkers = flood_fill(checkers, (76, 76), 127) # <-------   skimage version
filled_checkers = flood_fill(checkers, (76, 76), 255, 127) # <-----  my version

fig, ax = plt.subplots(ncols=2, figsize=(10, 5))

ax[0].imshow(checkers, cmap=plt.cm.gray)
ax[0].set_title('Original')
ax[0].axis('off')

ax[1].imshow(filled_checkers, cmap=plt.cm.gray)
ax[1].plot(76, 76, 'wo')  # seed point
ax[1].set_title('After flood fill')
ax[1].axis('off')

plt.show()

我的实现,flood_algorithm.py:

from collections import deque

def flood_fill(input_array, start_point, source_colour, target_colour):
    if not is_inside_image(start_point, input_array.shape):
        return input_array

    if input_array[start_point[0]][start_point[1]] == source_colour:
        # create a queue and enqueue starting pixel
        q = deque()
        q.append(start_point)
    
        while q:
            point = q.popleft()
            
            if is_inside_image(point, input_array.shape):
                if input_array[start_point[0]][start_point[1]] == source_colour:
                    input_array[start_point[0]][start_point[1]] = target_colour
                    
                    q.append((start_point[0] + 1, start_point[1]))
                    q.append((start_point[0] - 1, start_point[1]))
                    q.append((start_point[0]    , start_point[1] + 1))
                    q.append((start_point[0]    , start_point[1] - 1))
    
    return input_array

def is_inside_image(point, input_array_shape):
    return (point[0] > 0) and (point[0] < input_array_shape[0]) and \
           (point[1] > 0) and (point[1] < input_array_shape[1])

【问题讨论】:

  • 您检查颜色的唯一点是start_point。你从队列中弹出的point 永远不会被用于任何事情。
  • 非常感谢。

标签: python python-3.x algorithm image-processing scikit-image


【解决方案1】:

在 DFS 循环中,您错误地使用了start_point。您应该对弹出的point 进行着色和扩展,例如:

    while q:
        point = q.popleft()
        
        if is_inside_image(point, input_array.shape):
            if input_array[point[0]][point[1]] == source_colour:
                input_array[point[0]][point[1]] = target_colour
                
                q.append((point[0] + 1, point[1]))
                q.append((point[0] - 1, point[1]))
                q.append((point[0]    , point[1] + 1))
                q.append((point[0]    , point[1] - 1))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2011-07-10
    • 1970-01-01
    • 2019-02-26
    相关资源
    最近更新 更多