Skip to content
Snippets Groups Projects
Commit aee1a867 authored by LexvanGastel's avatar LexvanGastel
Browse files

Vraag 26

parent 3dc9397b
No related branches found
No related tags found
No related merge requests found
import cv2
import matplotlib.pyplot as plt
import numpy as np
from skimage.color import rgb2gray
from skimage.feature import blob_log
class ImageProcessor:
def __init__(self, image_path):
......@@ -45,7 +47,28 @@ class ImageProcessor:
green_mask = cv2.inRange(self.image_hsv, lower_green, upper_green)
return red_mask, yellow_mask, green_mask
def detect_blobs(mask, min_sigma=3, max_sigma=15, num_sigma = 3, threshold=0.5):
mask = mask.astype(np.float64) / 255.0
#print('mask made')
blobs = blob_log(mask, min_sigma=min_sigma, max_sigma=max_sigma, num_sigma=num_sigma, threshold=threshold)
#print('blobs detected')
if len(blobs) > 0:
blobs[:,2] = blobs[:, 2]*np.sqrt(2)
return blobs
def plot_detected_blobs(mask, blobs, title):
fig, ax = plt.subplots(figsize=(6,6))
ax.imshow(mask, cmap='gray')
ax.set_title(title)
ax.axis("off")
for blob in blobs:
y, x, r = blob
circle =plt.Circle((x,y), r, color = 'red', linewidth = 2, fill = False)
ax.add_patch(circle)
plt.show()
class Plotter:
@staticmethod
......@@ -121,6 +144,36 @@ class Plotter:
axes[2].axis("off")
plt.show()
@staticmethod
def plot_blobs(red_mask, yellow_mask, green_mask, red_blobs, yellow_blobs, green_blobs):
fig, axes = plt.subplots(1, 3, figsize=(18,6))
axes[0].imshow(red_mask, cmap ='gray')
axes[0].set_title("Red blobs")
axes[0].axis("off")
for blob in red_blobs:
y, x, r = blob
circle = plt.Circle((x,y), r, color ='red', linewidth = 2, fill = False)
axes[0].add_patch(circle)
axes[1].imshow(yellow_mask, cmap ='gray')
axes[1].set_title("Yellow blobs")
axes[1].axis("off")
for blob in yellow_blobs:
y, x, r = blob
circle = plt.Circle((x,y), r, color ='yellow', linewidth = 2, fill = False)
axes[1].add_patch(circle)
axes[2].imshow(green_mask, cmap ='gray')
axes[2].set_title("Green blobs")
axes[2].axis("off")
for blob in green_blobs:
y, x, r = blob
circle = plt.Circle((x,y), r, color ='green', linewidth = 2, fill = False)
axes[2].add_patch(circle)
plt.show()
image_path = "traffic_light_image_1.png"
processor = ImageProcessor(image_path)
......@@ -128,19 +181,27 @@ red, green, blue = processor.get_color_channels()
red_mask, yellow_mask, green_mask = processor.get_color_masks()
snr_red = processor.calculate_snr(red)
snr_green = processor.calculate_snr(green)
snr_blue = processor.calculate_snr(blue)
red_blobs = detect_blobs(red_mask)
yellow_blobs = detect_blobs(yellow_mask)
green_blobs = detect_blobs(green_mask)
print(f"Red Blobs: {len(red_blobs)}")
print(f"Yellow Blobs: {len(yellow_blobs)}")
print(f"Green Blobs: {len(green_blobs)}")
# snr_red = processor.calculate_snr(red)
# snr_green = processor.calculate_snr(green)
# snr_blue = processor.calculate_snr(blue)
#print(f"SNR for Red Channel {snr_red:.2f}")
#print(f"SNR for Green Channel {snr_green:.2f}")
#print(f"SNR for Blue Channel {snr_blue:.2f}")
filtered_image = processor.apply_median_filter(kernel_size = 5)
# filtered_image = processor.apply_median_filter(kernel_size = 5)
#Plotter.plot_image_channels(processor.image_rgb,red,green,blue)
#Plotter.plot_histograms(red, green, blue)
#Plotter.plot_filtered_image(processor.image_rgb, filtered_image)
Plotter.plot_color_masks(processor.image_rgb, red_mask, yellow_mask, green_mask)
print(processor.image.shape)
\ No newline at end of file
#Plotter.plot_color_masks(processor.image_rgb, red_mask, yellow_mask, green_mask)
Plotter.plot_blobs(red_mask, yellow_mask, green_mask, red_blobs, yellow_blobs, green_blobs)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment