Question: Step 1 : Create a Node A node represents the x and y position of a pixel and it's neighbours. class Node: def _ _
Step : Create a Node
A node represents the x and y position of a pixel and it's neighbours.
class Node:
def initself x y:
self.x x
self.y y
self.neighbours list
Step : Create an Image
The image with and data, the represents the black pixels and the represents the white pixels.
image
Step : Implement the flood fill algorithm
Write the flood fill algorithm using breadth first search.
The flood fill algorithm is an algorithm used determine the area connected to a given point in an image that has a similar color. It starts from a specified seed point and floods outwards.
Variables of interest:
The explored variable is a set, meaning that only unique values can be inserted into the data structure.
The queue variable is a list, but treated as a queue, meaning that the element removed from the queue was the first element added.
class Graph:
explored set
def floodFillself image, x y pixel:
start Nodex y
queue list
queue.appendstart
# Remember to add the x and y coordinates of newly discovered nodes to the explored set
###
### YOUR CODE HERE
###
# Return the modified image represented as a D array of numbers
return image
def neighboursself image, x y currentNode:
U y
D y
L x
R x
# An edge is valid if the pixel is newly discovered, ie an edge is created when the neighbour's pixel value is one.
# Write the neighbours function to find the neighbours in four directions for a given pixel.
# Append a valid Node to the neighbours of the currentNode
# Remember to do boundary checking
###
### YOUR CODE HERE
###
# Return the current node's the pixel in question neighbours, not always a maximum of four.
return currentNode.neighbours
Step : Definition of the start function
Drives the graph and executes the flood fill algorithm.
def start:
# Sample image represented as D array of numbers
graph Graph
printgraphfloodFillimage
# DO NOT REMOVE THE RETURN STATEMENT!
return graph
Step : Call the start function
By calling the start function, the function returns the graph and its properties.
This is useful to debug your algorithm.
graph start
###
### AUTOGRADER TEST DO NOT REMOVE
###
###
### AUTOGRADER TEST DO NOT REMOVE
###
###
### AUTOGRADER TEST DO NOT REMOVE
###
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
