0% found this document useful (0 votes)
12 views4 pages

32-04-Ngô Huỳnh Kiều Nga

This document describes two algorithms for boundary fill: a recursive approach and a queue-based approach. The recursive approach uses depth-first search to replace all connected target color pixels with the replacement color. The queue-based approach uses breadth-first search, maintaining a queue of pixel coordinates to explore and replacing target colors as they are visited. Both algorithms take an image, starting x/y coordinates, target color, and replacement color as input.

Uploaded by

nhknga17032004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

32-04-Ngô Huỳnh Kiều Nga

This document describes two algorithms for boundary fill: a recursive approach and a queue-based approach. The recursive approach uses depth-first search to replace all connected target color pixels with the replacement color. The queue-based approach uses breadth-first search, maintaining a queue of pixel coordinates to explore and replacing target colors as they are visited. Both algorithms take an image, starting x/y coordinates, target color, and replacement color as input.

Uploaded by

nhknga17032004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

def flood_fill_recursive(image, x, y, target_color, replacement_color):


if x < 0 or x >= len(image) or y < 0 or y >= len(image[0]):
return
if image[x][y] != target_color:
return

image[x][y] = replacement_color

flood_fill_recursive(image, x + 1, y, target_color, replacement_color)


# Right
flood_fill_recursive(image, x - 1, y, target_color, replacement_color) #
Left
flood_fill_recursive(image, x, y + 1, target_color, replacement_color)
# Down
flood_fill_recursive(image, x, y - 1, target_color, replacement_color) #
Up

def boundary_fill_recursive(image, x, y, target_color, boundary_color,


replacement_color):
if x < 0 or x >= len(image) or y < 0 or y >= len(image[0]):
return
if image[x][y] == boundary_color or image[x][y] ==
replacement_color:
return

if image[x][y] == target_color:
image[x][y] = replacement_color

boundary_fill_recursive(image, x + 1, y, target_color, boundary_color,


replacement_color) # Right
boundary_fill_recursive(image, x - 1, y, target_color, boundary_color,
replacement_color) # Left
boundary_fill_recursive(image, x, y + 1, target_color, boundary_color,
replacement_color) # Down
boundary_fill_recursive(image, x, y - 1, target_color, boundary_color,
replacement_color) # Up

def boundary_fill(image, start_x, start_y, target_color,


replacement_color):
boundary_color = image[start_x][start_y]
flood_fill_recursive(image, start_x, start_y, target_color,
boundary_color) # Flood fill to create a mask
boundary_fill_recursive(image, start_x, start_y, target_color,
boundary_color, replacement_color) # Boundary fill the masked area

# Example Usage:
image = [
[1, 1, 1, 1, 1],
[1, 0, 0, 1, 1],
[1, 0, 0, 0, 1],
[1, 1, 1, 1, 1],
]

boundary_fill(image, 1, 2, 0, 2)

for row in image:


print(row)
from queue import Queue

def boundary_fill_recursive(image, x, y, target_color, boundary_color,


replacement_color):
if x < 0 or x >= len(image) or y < 0 or y >= len(image[0]):
return
if image[x][y] == boundary_color or image[x][y] ==
replacement_color:
return

if image[x][y] == target_color:
image[x][y] = replacement_color

boundary_fill_recursive(image, x + 1, y, target_color, boundary_color,


replacement_color) # Right
boundary_fill_recursive(image, x - 1, y, target_color, boundary_color,
replacement_color) # Left
boundary_fill_recursive(image, x, y + 1, target_color, boundary_color,
replacement_color) # Down
boundary_fill_recursive(image, x, y - 1, target_color, boundary_color,
replacement_color) # Up

def boundary_fill_queue(image, start_x, start_y, target_color,


replacement_color):
boundary_color = image[start_x][start_y]
visited = set()

q = Queue()
q.put((start_x, start_y))

while not q.empty():


x, y = q.get()

if (x, y) in visited or x < 0 or x >= len(image) or y < 0 or y >=


len(image[0]):
continue

visited.add((x, y))

if image[x][y] == target_color:
image[x][y] = replacement_color

q.put((x + 1, y)) # Right


q.put((x - 1, y)) # Left
q.put((x, y + 1)) # Down
q.put((x, y - 1)) # Up

# Example Usage:
image = [
[1, 1, 1, 1, 1],
[1, 0, 0, 1, 1],
[1, 0, 0, 0, 1],
[1, 1, 1, 1, 1],
]

start_x, start_y = 1, 2
target_color = 0
replacement_color = 2

# Using recursive approach


boundary_fill_recursive(image, start_x, start_y, target_color,
image[start_x][start_y], replacement_color)

# Reset image for the queue-based approach


image = [
[1, 1, 1, 1, 1],
[1, 0, 0, 1, 1],
[1, 0, 0, 0, 1],
[1, 1, 1, 1, 1],
]
# Using queue-based approach
boundary_fill_queue(image, start_x, start_y, target_color,
replacement_color)

# Print the resulting image


for row in image:
print(row)

You might also like