To modify the code to display the output image in color format, you need to change the image reading line in the read_input() function to read the image in color format. Replace the line:
1 frames
in random_sample(img)
21 y = random.randint(0, h - 1)
22
---> 23 while img[y, x] == 0:
24 x = random.randint(0, w - 1)
25 y = random.randint(0, h - 1)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Assistant
My apologies for the confusion earlier. Since we're working with a color image, we need to change the way we're checking for obstacles in the random_sample() function. Below is the corrected code:
python
def is_obstacle(img, x, y):
return np.all(img[y, x] == (0, 0, 0))
def random_sample(img):
h, w, _ = img.shape
x = random.randint(0, w - 1)
y = random.randint(0, h - 1)
while is_obstacle(img, x, y):
x = random.randint(0, w - 1)
y = random.randint(0, h - 1)
return x, y
Now, the random_sample() function should work correctly with a color image.
def reconstruct_path(goal_node):
path = []
current = goal_node
while current is not None:
path.append((current.x, current.y))
current = current.parent
return path[::-1]
Step 10: Draw the start and goal circles, sampled nodes and links, and the final path
def draw_RRT(img, tree, start, goal, path, file_name='output_map.png'):
"""
Draws the RRT graph on the given image.
Args:
img: The image to draw the graph on.
tree: The RRT graph.
start: The start position of the graph.
goal: The goal position of the graph.
path: The path from the start to the goal position.
file_name: The file name to save the image to.
"""
Convert the colors to the BGR format.
blue = (0, 0, 255)
red = (0, 0, 255)
green = (0, 255, 0)
Draw the path in blue.
for i in range(len(path) - 1):
cv2.line(img, path[i], path[i + 1], blue, 2)