Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
assignment Programming 2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gastel, L. van (Lex, Student B-BMT)
assignment Programming 2
Commits
aee1a867
Commit
aee1a867
authored
5 days ago
by
LexvanGastel
Browse files
Options
Downloads
Patches
Plain Diff
Vraag 26
parent
3dc9397b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Q2main.py
+68
-7
68 additions, 7 deletions
Q2main.py
with
68 additions
and
7 deletions
Q2main.py
+
68
−
7
View file @
aee1a867
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
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment