From b66a75ffccc98f96ce31f76afa039c02ac820b44 Mon Sep 17 00:00:00 2001 From: LexvanGastel <l.vangastel@studen.utwente.nl> Date: Thu, 3 Apr 2025 12:57:21 +0200 Subject: [PATCH] vraag 27 --- Q2main.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Q2main.py b/Q2main.py index efa8753..9d8bee0 100644 --- a/Q2main.py +++ b/Q2main.py @@ -55,7 +55,20 @@ def detect_blobs(mask, min_sigma=25, max_sigma=50, num_sigma = 10, threshold=0.2 #print('blobs detected') if len(blobs) > 0: blobs[:,2] = blobs[:, 2]*np.sqrt(2) + + return blobs +def detect_traffic_light(red_blobs, yellow_blobs, green_blobs): + blobs_couts ={ + "Red": len(red_blobs), + "Yellow": len(yellow_blobs), + "Green": len(green_blobs) + } + max_colour = max(blobs_couts, key=blobs_couts.get) + + if blobs_couts[max_colour] > 0: + return f"Traffic light is {max_colour}" + def plot_detected_blobs(mask, blobs, title): fig, ax = plt.subplots(figsize=(6,6)) @@ -175,16 +188,22 @@ class Plotter: plt.show() + + image_path = "traffic_light_image_1.png" processor = ImageProcessor(image_path) red, green, blue = processor.get_color_channels() + red_mask, yellow_mask, green_mask = processor.get_color_masks() red_blobs = detect_blobs(red_mask) yellow_blobs = detect_blobs(yellow_mask) green_blobs = detect_blobs(green_mask) +traffic_light_status = detect_traffic_light(red_blobs, yellow_blobs, green_blobs) +print(traffic_light_status) + print(f"Red Blobs: {len(red_blobs)}") print(f"Yellow Blobs: {len(yellow_blobs)}") print(f"Green Blobs: {len(green_blobs)}") -- GitLab