2020-02-13 12:54:14 +01:00
|
|
|
import cv2
|
|
|
|
|
2020-02-13 17:15:43 +01:00
|
|
|
input_video = 'data/video_1.avi'
|
|
|
|
size = 1.0 #wielkosc obrazu
|
2020-02-13 12:54:14 +01:00
|
|
|
|
2020-02-13 17:15:43 +01:00
|
|
|
cap = cv2.VideoCapture(input_video)
|
|
|
|
height, width, frames_count, fps = cap.get(cv2.CAP_PROP_FRAME_HEIGHT), cap.get(
|
|
|
|
cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_COUNT), cap.get(cv2.CAP_PROP_FPS),
|
2020-02-13 12:54:14 +01:00
|
|
|
|
2020-02-13 17:15:43 +01:00
|
|
|
height = int(height)
|
|
|
|
width = int(width)
|
|
|
|
print(height, width, frames_count, fps)
|
|
|
|
|
|
|
|
sub = cv2.createBackgroundSubtractorMOG2() # utworz background subtractor
|
|
|
|
|
|
|
|
while True:
|
2020-02-13 12:54:14 +01:00
|
|
|
ret, frame = cap.read()
|
2020-02-13 17:15:43 +01:00
|
|
|
if not ret: # jezeli film sie zakonczyl
|
|
|
|
#frame = cv2.VideoCapture(input_video)
|
|
|
|
#continue
|
|
|
|
break
|
|
|
|
if ret: #przetwarzanie kadru
|
|
|
|
input_vid = cv2.resize(frame, (0, 0), None, size, size) # rozmiar obrazu
|
|
|
|
cv2.imshow("bez zmian", input_vid) # wyswietlanie filmu wejsciowego bez zmian
|
|
|
|
gray = cv2.cvtColor(input_vid, cv2.COLOR_BGR2GRAY)
|
|
|
|
cv2.imshow("czarnobialy", gray) # wyswietlanie filmu w skali szarosci
|
|
|
|
fgMask = sub.apply(gray) # uzycie background subtraction
|
|
|
|
cv2.imshow("fgMask", fgMask)
|
2020-02-13 22:47:50 +01:00
|
|
|
# operacje morfologiczne. Wg tutoriala na docs.opencv.org. Opening, closing - usuwanie szumów
|
|
|
|
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) # kernel to apply to the morphology
|
|
|
|
closing = cv2.morphologyEx(fgMask, cv2.MORPH_CLOSE, kernel)
|
|
|
|
#cv2.imshow("closing", closing)
|
|
|
|
opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, kernel)
|
|
|
|
#cv2.imshow("opening", opening)
|
|
|
|
dilation = cv2.dilate(opening, kernel)
|
|
|
|
#cv2.imshow("dilation", dilation)
|
|
|
|
retvalbin, bins = cv2.threshold(dilation, 220, 255, cv2.THRESH_BINARY) # Thresholding/binaryzacja obrazu usuwanie cieni
|
|
|
|
#cv2.imshow('bins',bins)
|
|
|
|
# obrysowywanie
|
|
|
|
contours, hierarchy = cv2.findContours(bins, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # RETR_EXTERNAL - bierz pod uwage najbardziej 'zewnetrzne' kontury
|
|
|
|
|
|
|
|
minContourSize = 400
|
|
|
|
maxContourSize = 50000
|
|
|
|
|
|
|
|
for i in range(len(contours)): # przejdz wszystkie kontury w kadrze
|
|
|
|
if hierarchy[0, i, 3] == -1: # uzycie hierarchii zeby uwzgledniac tylko "rodzicow" (najbardziej zewnetrzne kontury)
|
|
|
|
area = cv2.contourArea(contours[i])
|
|
|
|
if minContourSize < area < maxContourSize: # nie obrysowuj zbyt duzych i malych obiektow
|
|
|
|
# obliczanie centoidu konturu (czyli srodka) za pomoca tzw "momentow"
|
|
|
|
centroid = contours[i]
|
|
|
|
moments = cv2.moments(centroid)
|
|
|
|
cx = int(moments['m10'] / moments['m00'])
|
|
|
|
cy = int(moments['m01'] / moments['m00'])
|
|
|
|
|
|
|
|
# punkty graniczne (bounding points) konturu. x,y to wspolrzedne lewego gornego rogu, w,h to szerokosc i wysokosc prostokata
|
|
|
|
x, y, w, h = cv2.boundingRect(centroid)
|
|
|
|
# utworz prostokat wokol konturu
|
|
|
|
cv2.rectangle(input_vid, (x, y), (x + w, y + h), (255, 0, 0), 2)
|
|
|
|
|
|
|
|
cv2.imshow("wykryte pojazdy", input_vid)
|
|
|
|
|
2020-02-13 12:54:14 +01:00
|
|
|
|
2020-02-13 17:15:43 +01:00
|
|
|
key = cv2.waitKey(60)
|
|
|
|
if key == 27: #Wyjdz po nacisnieciu escape
|
2020-02-13 12:54:14 +01:00
|
|
|
break
|
|
|
|
|
2020-02-13 17:15:43 +01:00
|
|
|
cap.release()
|
2020-02-13 12:54:14 +01:00
|
|
|
cv2.destroyAllWindows()
|