基于高斯混合模型的目标检测(基于混合高斯模型实现车辆检测)
基于高斯混合模型的目标检测(基于混合高斯模型实现车辆检测)Zivkovic Z Heijden F V D . Efficient adaptive density estimation per image pixel for the task of background subtraction[J]. Pattern recognition letters 2006 27(7):p.773-780.Barnich O Droogenbroeck M V . ViBe: A Universal Background Subtraction Algorithm for Video Sequences[J]. IEEE Transactions on Image Processing 2011 20(6):1709-1724.需要目标检测相关文献的可以关注并私信我哦!
运动目标检测(从静止的背景中检测出运动的目标)是许多计算机视觉应用(人流统计、车流统计、车牌识别、黑烟车监测等)中必不可少的一步。目前,运动目标检测方法主要包括两类,一类为背景差分算法,包括混合高斯模型、ViBe算法、codebook算法等;另一类为基于深度学习的算法,进一步可以分为one-stage及two-stage两类,one-stage包括YOLO、YOLOV2、YOLOV3、SSD等,two-stage包括R-CNN、fast R-CNN等。本文主要如何基于opencv框架及混合高斯模型实现运动车辆的检测。
安装opencvopencv是用于图像处理的c 第三方库,支持java、python、c 等多种编程语言,本文使用python进行opencv的开发。可通过以下命令安装opencv-python及opencv-contrib-python(国外速度太慢,可使用国内镜像源下载)。
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-contrib-python
混合高斯模型
混合高斯模型利用若干个高斯分布对每个像素点进行背景建模,通过对比将像素点分为背景像素点或前景像素点。混合高斯模型的优势是处理速度比较快,但其也存在一些不足,例如,背景同时出现高低频率的变化时可能出现误判。基于opencv实现的运动车辆检测代码如下:
# 测试mog(混合高斯模型)方法用于前景目标监测
video_path = 'BKB618_105212_video'
parser = argparse.ArgumentParser(description='This program shows how to use background subtraction methods \
provided by OpenCV. You can process both videos and images.')
parser.add_argument('--input' type=str help='Path to a video or a sequence of image.'
default='{}.mp4'.format(video_path))
parser.add_argument('--algo' type=str help='Background subtraction method (KNN MOG2).' default='MOG2')
args = parser.parse_args()
if args.algo == 'MOG2':
backSub = cv.createBackgroundSubtractorMOG2()
else:
backSub = cv.createBackgroundSubtractorKNN()
capture = cv.VideoCapture(cv.samples.findFileOrKeep(args.input))
# num = capture.get(cv.CAP_PROP_FRAME_COUNT)
width = capture.get(cv.CAP_PROP_FRAME_WIDTH)
height = capture.get(cv.CAP_PROP_FRAME_HEIGHT)
if not capture.isOpened:
print('Unable to open: ' args.input)
exit(0)
vibe = OriginVibe(width=width height=height)
while True:
ret frame = capture.read()
if frame is None:
break
frame_index = capture.get(cv.CAP_PROP_POS_FRAMES
fgMask = backSub.apply(frame)
dep img_bin = cv.threshold(fgMask 128 255 cv.THRESH_BINARY)
contours hierarchy = cv.findContours(img_bin cv.RETR_EXTERNAL cv.CHAIN_APPROX_SIMPLE)
for i in range(0 len(contours)):
x y w h = cv.boundingRect(contours[i])
# 去掉面积较小的区域
if w * h > 6400:
# 将车辆轮廓拉伸10%防止漏掉比较细的黑烟
stretch_height = int(h * 1.1)
# 车辆与黑烟区域分别用蓝色矩形框及红色矩形框标出
cv.rectangle(frame (x y) (x w y int(h * 1.1)) (255 0 0) 1)
# cv.rectangle(frame (x y int(stretch_height / 2)) (x w y stretch_height) (0 0 255) 1)
# 截取可能出现黑烟区域并利用双线性插值转换成指定尺寸的图片
smoke_img = frame[y int(stretch_height / 2):y stretch_height x:x w]
# 双线性插值转换成224*224的图片
resize_smoke_img = cv.resize(smoke_img (224 224) interpolation=cv.INTER_LINEAR)
# cv.imshow('smoke' frame)
# cv.imwrite("./result/MOGresult/{}/smoke{}_{}.jpg".format(video_path int(frame_index) i) resize_smoke_img)
# # draw_img3 = cv.drawContours(frame contours -1 (0 0 255) 3)
# # cv.rectangle(frame (10 2) (100 20) (255 255 255) -1)
# # cv.putText(frame str(capture.get(cv.CAP_PROP_POS_FRAMES)) (15 15)
# # cv.FONT_HERSHEY_SIMPLEX 0.5 (0 0 0))
# cv.rectangle(frame (10 2) (100 20) (255 255 255) -1)
# cv.putText(frame str(capture.get(cv.CAP_PROP_POS_FRAMES)) (15 15)
# cv.FONT_HERSHEY_SIMPLEX 0.5 (0 0 0))
cv.imshow('Frame' frame)
cv.imshow('FG Mask' img_bin)
if frame_index % 10 == 0:
cv.imwrite("./result/MOGresult/{}/originstretch{}.jpg".format(video_path frame_index) frame)
cv.imwrite("./result/MOGresult/{}/fg{}.jpg".format(video_path frame_index) img_bin)
keyboard = cv.waitKey(30)
if keyboard == 'q' or keyboard == 27:
break
参考资料
Zivkovic Z Heijden F V D . Efficient adaptive density estimation per image pixel for the task of background subtraction[J]. Pattern recognition letters 2006 27(7):p.773-780.
Barnich O Droogenbroeck M V . ViBe: A Universal Background Subtraction Algorithm for Video Sequences[J]. IEEE Transactions on Image Processing 2011 20(6):1709-1724.
需要目标检测相关文献的可以关注并私信我哦!