Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
larm-astro
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
Inès EL HADRI
larm-astro
Commits
733925d0
Commit
733925d0
authored
Jan 12, 2024
by
Inès EL HADRI
💤
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Vision test scripts
parent
e2c7ee26
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
92 additions
and
5 deletions
+92
-5
extract_regions.py
tuto_vision/test/extract_regions.py
+56
-0
mask_tuner.py
tuto_vision/test/mask_tuner.py
+36
-5
No files found.
tuto_vision/test/extract_regions.py
0 → 100644
View file @
733925d0
import
cv2
import
numpy
as
np
import
matplotlib.pyplot
as
plt
from
skimage.measure
import
label
,
regionprops
import
math
filename
=
'./mask.jpg'
image
=
cv2
.
imread
(
filename
)
# Passage en niveau de gris
if
(
filename
!=
'./mask.jpg'
):
gray
=
cv2
.
cvtColor
(
image
,
cv2
.
COLOR_BGR2GRAY
)
###### extration des régions avec la lib skimage
# Binarisation de l'image
if
(
filename
!=
'./mask.jpg'
):
ret
,
thresh
=
cv2
.
threshold
(
gray
,
127
,
255
,
1
)
else
:
thresh
=
image
cv2
.
imshow
(
"image seuillée"
,
thresh
)
cv2
.
waitKey
(
0
)
# extraction des régions et des propriétés des régions
label_img
=
label
(
thresh
)
regions
=
regionprops
(
label_img
)
print
(
f
'{regions=}'
)
cv2
.
waitKey
(
0
)
# affichage des régions et des boites englobantes
fig
,
ax
=
plt
.
subplots
()
ax
.
imshow
(
thresh
,
cmap
=
plt
.
cm
.
gray
)
for
props
in
regions
:
y0
,
x0
=
props
.
centroid
orientation
=
props
.
orientation
x1
=
x0
+
math
.
cos
(
orientation
)
*
0.5
*
props
.
minor_axis_length
y1
=
y0
-
math
.
sin
(
orientation
)
*
0.5
*
props
.
minor_axis_length
x2
=
x0
-
math
.
sin
(
orientation
)
*
0.5
*
props
.
major_axis_length
y2
=
y0
-
math
.
cos
(
orientation
)
*
0.5
*
props
.
major_axis_length
ax
.
plot
((
x0
,
x1
),
(
y0
,
y1
),
'-r'
,
linewidth
=
2.5
)
ax
.
plot
((
x0
,
x2
),
(
y0
,
y2
),
'-r'
,
linewidth
=
2.5
)
ax
.
plot
(
x0
,
y0
,
'.g'
,
markersize
=
15
)
minr
,
minc
,
maxr
,
maxc
=
props
.
bbox
bx
=
(
minc
,
maxc
,
maxc
,
minc
,
minc
)
by
=
(
minr
,
minr
,
maxr
,
maxr
,
minr
)
ax
.
plot
(
bx
,
by
,
'-b'
,
linewidth
=
2.5
)
ax
.
axis
((
0
,
600
,
600
,
0
))
plt
.
show
()
cv2
.
waitKey
(
0
)
\ No newline at end of file
tuto_vision/mask_tuner.py
→
tuto_vision/
test/
mask_tuner.py
View file @
733925d0
...
@@ -37,15 +37,28 @@ cv2.setMouseCallback('Camera', souris)
...
@@ -37,15 +37,28 @@ cv2.setMouseCallback('Camera', souris)
hsv_px
=
[
0
,
0
,
0
]
hsv_px
=
[
0
,
0
,
0
]
# Creating morphological kernel
# Creating morphological kernel
kernel
=
np
.
ones
((
3
,
3
),
np
.
uint8
)
erodeKernel
=
np
.
ones
((
3
,
3
),
np
.
uint8
)
dilateKernel
=
np
.
ones
((
3
,
3
),
np
.
uint8
)
while
True
:
while
True
:
ret
,
frame
=
cap
.
read
()
ret
,
frame
=
cap
.
read
()
base_img
=
frame
.
copy
()
image
=
cv2
.
cvtColor
(
frame
,
cv2
.
COLOR_BGR2HSV
)
image
=
cv2
.
cvtColor
(
frame
,
cv2
.
COLOR_BGR2HSV
)
mask
=
cv2
.
inRange
(
image
,
lo
,
hi
)
mask
=
cv2
.
erode
(
mask
,
kernel
,
iterations
=
1
)
mask
=
image
mask
=
cv2
.
dilate
(
mask
,
kernel
,
iterations
=
1
)
# Seuillage par colorimétrie
mask
=
cv2
.
inRange
(
mask
,
lo
,
hi
)
beforeErosion
=
mask
# Réduction du bruit
mask
=
cv2
.
erode
(
mask
,
erodeKernel
,
iterations
=
4
)
mask
=
cv2
.
dilate
(
mask
,
dilateKernel
,
iterations
=
4
)
# Application du masque
image2
=
cv2
.
bitwise_and
(
frame
,
frame
,
mask
=
mask
)
image2
=
cv2
.
bitwise_and
(
frame
,
frame
,
mask
=
mask
)
cv2
.
putText
(
frame
,
"Couleur: {:d}"
.
format
(
color
),
(
10
,
30
),
cv2
.
FONT_HERSHEY_DUPLEX
,
1
,
color_info
,
1
,
cv2
.
LINE_AA
)
cv2
.
putText
(
frame
,
"Couleur: {:d}"
.
format
(
color
),
(
10
,
30
),
cv2
.
FONT_HERSHEY_DUPLEX
,
1
,
color_info
,
1
,
cv2
.
LINE_AA
)
# Affichage des composantes HSV sous la souris sur l'image
# Affichage des composantes HSV sous la souris sur l'image
...
@@ -54,11 +67,29 @@ while True:
...
@@ -54,11 +67,29 @@ while True:
cv2
.
putText
(
frame
,
"px HSV: "
+
pixel_hsv
,
(
10
,
260
),
cv2
.
putText
(
frame
,
"px HSV: "
+
pixel_hsv
,
(
10
,
260
),
font
,
1
,
(
255
,
255
,
255
),
1
,
cv2
.
LINE_AA
)
font
,
1
,
(
255
,
255
,
255
),
1
,
cv2
.
LINE_AA
)
# Segmentation
## Min enclosing circle method
elements
=
cv2
.
findContours
(
mask
,
cv2
.
RETR_EXTERNAL
,
cv2
.
CHAIN_APPROX_SIMPLE
)[
-
2
]
if
len
(
elements
)
>
0
:
c
=
max
(
elements
,
key
=
cv2
.
contourArea
)
((
x
,
y
),
rayon
)
=
cv2
.
minEnclosingCircle
(
c
)
if
rayon
>
30
:
cv2
.
circle
(
frame
,
(
int
(
x
),
int
(
y
)),
int
(
rayon
),
color_info
,
2
)
cv2
.
circle
(
frame
,
(
int
(
x
),
int
(
y
)),
5
,
color_info
,
10
)
cv2
.
line
(
frame
,
(
int
(
x
),
int
(
y
)),
(
int
(
x
)
+
150
,
int
(
y
)),
color_info
,
2
)
cv2
.
putText
(
frame
,
"Objet !!!"
,
(
int
(
x
)
+
10
,
int
(
y
)
-
10
),
cv2
.
FONT_HERSHEY_DUPLEX
,
1
,
color_info
,
1
,
cv2
.
LINE_AA
)
cv2
.
imshow
(
'Camera'
,
frame
)
cv2
.
imshow
(
'Camera'
,
frame
)
cv2
.
imshow
(
'image2'
,
image2
)
cv2
.
imshow
(
'Before erosion'
,
beforeErosion
)
# cv2.imshow('image2', image2)
cv2
.
imshow
(
'Mask'
,
mask
)
cv2
.
imshow
(
'Mask'
,
mask
)
if
cv2
.
waitKey
(
1
)
&
0xFF
==
ord
(
'q'
):
if
cv2
.
waitKey
(
1
)
&
0xFF
==
ord
(
'q'
):
break
break
cv2
.
imwrite
(
'mask.jpg'
,
mask
)
cv2
.
imwrite
(
'rgb.jpg'
,
base_img
)
cap
.
release
()
cap
.
release
()
cv2
.
destroyAllWindows
()
cv2
.
destroyAllWindows
()
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment