Commit 733925d0 authored by Inès EL HADRI's avatar Inès EL HADRI 💤

Vision test scripts

parent e2c7ee26
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
...@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment