How to resolve the algorithm Bitmap/Flood fill step by step in the Julia programming language
How to resolve the algorithm Bitmap/Flood fill step by step in the Julia programming language
Table of Contents
Problem Statement
A flood fill is a way of filling an area using color banks to define the contained area or a target color which "determines" the area (the valley that can be flooded; Wikipedia uses the term target color). It works almost like a water flooding from a point towards the banks (or: inside the valley): if there's a hole in the banks, the flood is not contained and all the image (or all the "connected valleys") get filled. To accomplish the task, you need to implement just one of the possible algorithms (examples are on Wikipedia). Variations on the theme are allowed (e.g. adding a tolerance parameter or argument for color-matching of the banks or target color). Testing: the basic algorithm is not suitable for truecolor images; a possible test image is the one shown on the right box; you can try to fill the white area, or the black inner circle.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Bitmap/Flood fill step by step in the Julia programming language
This code implements a flood fill algorithm to fill a region of a binary image with a given color. The input image is a binary image represented as a matrix of Boolean values, and the output is the same image with the specified region filled with the given color.
The algorithm works by starting at the given initial node, and then recursively filling all adjacent nodes of the same color until the edge of the region is reached. The algorithm uses a queue to keep track of the nodes that need to be filled, and it pops the next node from the queue and fills its adjacent nodes until the queue is empty.
The constants north
, south
, east
, and west
represent the four directions that the algorithm can move in. The checkbounds
function checks if a given node is within the bounds of the image.
The main function floodfill
takes the input image, the initial node, the target color, and the replacement color as input. It first checks if the color of the node is equal to the target color and returns the image if it is. Otherwise, it initializes the queue with the initial node and starts the flood fill algorithm.
The algorithm pops the next node from the queue, and if the color of the node is equal to the target color, it sets the color of the node to the replacement color. It then checks if the nodes to the north, south, east, and west of the current node are within the bounds of the image and have the target color. If so, it adds them to the queue.
The algorithm continues until the queue is empty. It then returns the filled image.
The code also includes an example of how to use the floodfill
function to fill a region of a binary image with a given color. The input image is loaded from a file, and the filled image is saved to a file.
Source code in the julia programming language
using Images, FileIO
function floodfill!(img::Matrix{<:Color}, initnode::CartesianIndex{2}, target::Color, replace::Color)
img[initnode] != target && return img
# constants
north = CartesianIndex(-1, 0)
south = CartesianIndex( 1, 0)
east = CartesianIndex( 0, 1)
west = CartesianIndex( 0, -1)
queue = [initnode]
while !isempty(queue)
node = pop!(queue)
if img[node] == target
wnode = node
enode = node + east
end
# Move west until color of node does not match target color
while checkbounds(Bool, img, wnode) && img[wnode] == target
img[wnode] = replace
if checkbounds(Bool, img, wnode + north) && img[wnode + north] == target
push!(queue, wnode + north)
end
if checkbounds(Bool, img, wnode + south) && img[wnode + south] == target
push!(queue, wnode + south)
end
wnode += west
end
# Move east until color of node does not match target color
while checkbounds(Bool, img, enode) && img[enode] == target
img[enode] = replace
if checkbounds(Bool, img, enode + north) && img[enode + north] == target
push!(queue, enode + north)
end
if checkbounds(Bool, img, enode + south) && img[enode + south] == target
push!(queue, enode + south)
end
enode += east
end
end
return img
end
img = Gray{Bool}.(load("data/unfilledcircle.png"))
floodfill!(img, CartesianIndex(100, 100), Gray(false), Gray(true))
save("data/filledcircle.png", img)
You may also check:How to resolve the algorithm XML/Input step by step in the C# programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the RPL programming language
You may also check:How to resolve the algorithm Exponentiation order step by step in the Rust programming language
You may also check:How to resolve the algorithm Amicable pairs step by step in the Factor programming language
You may also check:How to resolve the algorithm Longest common subsequence step by step in the Egison programming language