基于OPENCV的车辆检测(Python)

import cv2
import numpy as np

bs = cv2.createBackgroundSubtractorKNN(detectShadows = True)
camera = cv2.VideoCapture("traffic.flv")

while True:
	ret, frame = camera.read()
	fgmask = bs.apply(frame)
	fg2 = fgmask.copy()
	th = cv2.threshold(fg2,244,255,cv2.THRESH_BINARY)[1]
	dilated = cv2.dilate(th,cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)),iterations = 2)
	image, contours, hier = cv2.findContours(dilated,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
	for c in contours:
		if cv2.contourArea(c) > 100:
			(x,y,w,h) = cv2.boundingRect(c)
			cv2.rectangle(frame,(x,y),(x+w,y+h),(255,255,0),2)
			
	cv2.imshow("mog",fgmask)
	cv2.imshow("thresh",th)
	cv2.imshow("detection",frame)
	if cv2.waitKey(24) & 0xff == 27:
		break
camera.release()
cv2.destroyAllWindows()

    原文作者:clauyiye
    原文地址: https://blog.csdn.net/clauyiye/article/details/80087994
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞