Initial commit
This commit is contained in:
6
OpenCV/tutoriel13/README.md
Normal file
6
OpenCV/tutoriel13/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# Tutoriel OpenCV
|
||||
## Détection d'objet par soustraction
|
||||
|
||||
La vidéo du tutoriel est à l'adresse:
|
||||
https://www.youtube.com/watch?v=pkzT9MlICPE
|
||||
|
||||
BIN
OpenCV/tutoriel13/autoroute.mp4
Normal file
BIN
OpenCV/tutoriel13/autoroute.mp4
Normal file
Binary file not shown.
48
OpenCV/tutoriel13/calcul_mask.py
Normal file
48
OpenCV/tutoriel13/calcul_mask.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
import common
|
||||
|
||||
video='autoroute.mp4'
|
||||
image_fond="img-0.png"
|
||||
color_infos=(0, 0, 255)
|
||||
|
||||
nbr_old=0
|
||||
vehicule=0
|
||||
seuil=10
|
||||
|
||||
fond=common.moyenne_image(video, 100)
|
||||
|
||||
cv2.imshow('fond', fond.astype(np.uint8))
|
||||
cap=cv2.VideoCapture(video)
|
||||
|
||||
while True:
|
||||
ret, frame=cap.read()
|
||||
tickmark=cv2.getTickCount()
|
||||
mask=common.calcul_mask(frame, fond, seuil)
|
||||
elements=cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
|
||||
nbr=0
|
||||
for e in elements:
|
||||
((x, y), rayon)=cv2.minEnclosingCircle(e)
|
||||
if rayon>20:
|
||||
cv2.circle(frame, (int(x), int(y)), 5, color_infos, 10)
|
||||
nbr+=1
|
||||
if nbr>nbr_old:
|
||||
vehicule+=1
|
||||
nbr_old=nbr
|
||||
fps=cv2.getTickFrequency()/(cv2.getTickCount()-tickmark)
|
||||
cv2.putText(frame, "FPS: {:05.2f} Seuil: {:d}".format(fps, seuil), (10, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, color_infos, 1)
|
||||
cv2.imshow('video', frame)
|
||||
cv2.imshow('mask', mask)
|
||||
key=cv2.waitKey(1)&0xFF
|
||||
if key==ord('q'):
|
||||
break
|
||||
if key==ord('p'):
|
||||
seuil+=1
|
||||
if key==ord('m'):
|
||||
seuil-=1
|
||||
if key==ord('a'):
|
||||
for cpt in range(20):
|
||||
ret, frame=cap.read()
|
||||
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
29
OpenCV/tutoriel13/common.py
Normal file
29
OpenCV/tutoriel13/common.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
def moyenne_image(video, nbr):
|
||||
cap=cv2.VideoCapture(video)
|
||||
tab_image=[]
|
||||
for f in range(nbr):
|
||||
ret, frame=cap.read()
|
||||
if ret is False:
|
||||
break
|
||||
image=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
||||
tab_image.append(image)
|
||||
tab_image=np.array(tab_image)
|
||||
cap.release()
|
||||
return np.mean(tab_image, axis=0)
|
||||
|
||||
def calcul_mask(image, fond, seuil):
|
||||
image=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
height, width=image.shape
|
||||
mask=np.zeros([height, width], np.uint8)
|
||||
image=image.astype(np.int32)
|
||||
for y in range(height):
|
||||
for x in range(width):
|
||||
if abs(fond[y][x]-image[y][x])>seuil:
|
||||
mask[y][x]=255
|
||||
kernel=np.ones((5, 5), np.uint8)
|
||||
mask=cv2.erode(mask, kernel, iterations=1)
|
||||
mask=cv2.dilate(mask, kernel, iterations=3)
|
||||
return mask
|
||||
52
OpenCV/tutoriel13/compte.py
Normal file
52
OpenCV/tutoriel13/compte.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
import common
|
||||
|
||||
color_infos=(0, 0, 255)
|
||||
xmin=90
|
||||
xmax=510
|
||||
ymin=315
|
||||
ymax=360
|
||||
video='autoroute.mp4'
|
||||
|
||||
nbr_old=0
|
||||
vehicule=0
|
||||
seuil=10
|
||||
|
||||
fond=common.moyenne_image(video, 500)
|
||||
fond=fond[ymin:ymax, xmin:xmax]
|
||||
cv2.imshow('fond', fond.astype(np.uint8))
|
||||
fond=fond.astype(np.int32)
|
||||
cap=cv2.VideoCapture(video)
|
||||
|
||||
while True:
|
||||
ret, frame=cap.read()
|
||||
tickmark=cv2.getTickCount()
|
||||
mask=common.calcul_mask(frame[ymin:ymax, xmin:xmax], fond, seuil)
|
||||
elements=cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
|
||||
nbr=0
|
||||
for e in elements:
|
||||
((x, y), rayon)=cv2.minEnclosingCircle(e)
|
||||
if rayon>20:
|
||||
cv2.circle(frame, (int(x)+xmin, int(y)+ymin), 5, color_infos, 10)
|
||||
nbr+=1
|
||||
if nbr>nbr_old:
|
||||
vehicule+=1
|
||||
nbr_old=nbr
|
||||
fps=cv2.getTickFrequency()/(cv2.getTickCount()-tickmark)
|
||||
cv2.putText(frame, "FPS: {:05.2f} Seuil: {:d}".format(fps, seuil), (10, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, color_infos, 1)
|
||||
cv2.rectangle(frame, (xmin, ymin), (xmax+120, ymax), (255, 0, 0), 5)
|
||||
cv2.rectangle(frame, (xmax, ymin), (xmax+120, ymax), (255, 0, 0), cv2.FILLED)
|
||||
cv2.putText(frame, "{:04d}".format(vehicule), (xmax+10, ymin+35), cv2.FONT_HERSHEY_COMPLEX_SMALL, 2, (255, 255, 255), 2)
|
||||
cv2.imshow('video', frame)
|
||||
cv2.imshow('mask', mask)
|
||||
key=cv2.waitKey(1)&0xFF
|
||||
if key==ord('q'):
|
||||
break
|
||||
if key==ord('p'):
|
||||
seuil+=1
|
||||
if key==ord('m'):
|
||||
seuil-=1
|
||||
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
87
OpenCV/tutoriel13/compte_v2.py
Normal file
87
OpenCV/tutoriel13/compte_v2.py
Normal file
@@ -0,0 +1,87 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
import common
|
||||
|
||||
color_infos=(0, 0, 255)
|
||||
|
||||
ymin=315
|
||||
ymax=360
|
||||
|
||||
xmin1=110
|
||||
xmax1=190
|
||||
|
||||
xmin2=250
|
||||
xmax2=330
|
||||
|
||||
xmin3=380
|
||||
xmax3=460
|
||||
|
||||
video='autoroute.mp4'
|
||||
|
||||
vehicule1=0
|
||||
vehicule2=0
|
||||
vehicule3=0
|
||||
seuil=10
|
||||
seuil2=100
|
||||
|
||||
fond=common.moyenne_image(video, 500)
|
||||
fond=fond[ymin:ymax, xmin1:xmax3]
|
||||
cv2.imshow('fond', fond.astype(np.uint8))
|
||||
fond=fond.astype(np.int32)
|
||||
cap=cv2.VideoCapture(video)
|
||||
|
||||
def calcul_mean(image):
|
||||
height, width=image.shape
|
||||
s=0
|
||||
for y in range(height):
|
||||
for x in range(width):
|
||||
s+=image[y][x]
|
||||
return s/(height*width)
|
||||
|
||||
old_1=0
|
||||
old_2=0
|
||||
old_3=0
|
||||
while True:
|
||||
ret, frame=cap.read()
|
||||
tickmark=cv2.getTickCount()
|
||||
mask=common.calcul_mask(frame[ymin:ymax, xmin1:xmax3], fond, seuil)
|
||||
|
||||
if calcul_mean(mask[0:ymax-ymin, 0:xmax1-xmin1])> seuil2:
|
||||
if old_1==0:
|
||||
vehicule1+=1
|
||||
old_1=1
|
||||
else:
|
||||
old_1=0
|
||||
|
||||
if calcul_mean(mask[0:ymax-ymin, xmin2-xmin1:xmax2-xmin1])> seuil2:
|
||||
if old_2==0:
|
||||
vehicule2+=1
|
||||
old_2=1
|
||||
else:
|
||||
old_2=0
|
||||
|
||||
if calcul_mean(mask[0:ymax-ymin, xmin3-xmin1:xmax3-xmin1])> seuil2:
|
||||
if old_3==0:
|
||||
vehicule3+=1
|
||||
old_3=1
|
||||
else:
|
||||
old_3=0
|
||||
|
||||
fps=cv2.getTickFrequency()/(cv2.getTickCount()-tickmark)
|
||||
cv2.putText(frame, "FPS: {:05.2f} Seuil: {:d}".format(fps, seuil), (10, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, color_infos, 1)
|
||||
cv2.putText(frame, "{:04d} {:04d} {:04d}".format(vehicule1, vehicule2, vehicule3), (xmin1, ymin-10), cv2.FONT_HERSHEY_COMPLEX_SMALL, 2, (255, 255, 255), 2)
|
||||
cv2.rectangle(frame, (xmin1, ymin), (xmax1, ymax), (0, 0, 255) if old_1 else (255, 0, 0), 3)
|
||||
cv2.rectangle(frame, (xmin2, ymin), (xmax2, ymax), (0, 0, 255) if old_2 else (255, 0, 0), 3)
|
||||
cv2.rectangle(frame, (xmin3, ymin), (xmax3, ymax), (0, 0, 255) if old_3 else (255, 0, 0), 3)
|
||||
cv2.imshow('video', frame)
|
||||
cv2.imshow('mask', mask)
|
||||
key=cv2.waitKey(1)&0xFF
|
||||
if key==ord('q'):
|
||||
break
|
||||
if key==ord('p'):
|
||||
seuil+=1
|
||||
if key==ord('m'):
|
||||
seuil-=1
|
||||
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
9
OpenCV/tutoriel13/moyenne.py
Normal file
9
OpenCV/tutoriel13/moyenne.py
Normal file
@@ -0,0 +1,9 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
import common
|
||||
|
||||
image=common.moyenne_image('autoroute.mp4', 100)
|
||||
cv2.imshow('fond', image.astype(np.uint8))
|
||||
cv2.waitKey()
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
Reference in New Issue
Block a user