KNN、MOG2和GMG

举报
风吹稻花香 发表于 2021/06/05 22:34:41 2021/06/05
【摘要】   KNN、MOG2和GMG 好像能判断物体移开,花屏效果不好,mouse也不能检测。 mog2: # coding:utf-8 import cv2 # 获取摄像头对象cap = cv2.VideoCapture(0)# 背景分割器对象mog = cv2.createBackgroundSubtractorMOG2() while True: ret, f...

 

KNN、MOG2和GMG

好像能判断物体移开,花屏效果不好,mouse也不能检测。

mog2:


  
  1. # coding:utf-8
  2. import cv2
  3. # 获取摄像头对象
  4. cap = cv2.VideoCapture(0)
  5. # 背景分割器对象
  6. mog = cv2.createBackgroundSubtractorMOG2()
  7. while True:
  8. ret, frame = cap.read()
  9. fgmask = mog.apply(frame)
  10. cv2.imshow("frame", fgmask)
  11. if cv2.waitKey(5) & 0xff == ord("q"):
  12. break
  13. cap.release()
  14. cv2.destroyAllWindows()

knn:10ms以内,人是完整的,人走消失的挺快


  
  1. import time
  2. import cv2
  3. import numpy as np
  4. camera = cv2.VideoCapture(0) # 参数0表示第一个摄像头
  5. bs = cv2.createBackgroundSubtractorKNN(detectShadows=True)
  6. es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
  7. while True:
  8. grabbed, frame_lwpCV = camera.read()
  9. start=time.time()
  10. frame_lwpCV = cv2.GaussianBlur(frame_lwpCV, (5, 5), 0)
  11. fgmask = bs.apply(frame_lwpCV) # 背景分割器,该函数计算了前景掩码
  12. # 二值化阈值处理,前景掩码含有前景的白色值以及阴影的灰色值,在阈值化图像中,将非纯白色(244~255)的所有像素都设为0,而不是255
  13. th = cv2.threshold(fgmask.copy(), 244, 255, cv2.THRESH_BINARY)[1]
  14. # 下面就跟基本运动检测中方法相同,识别目标,检测轮廓,在原始帧上绘制检测结果
  15. dilated = cv2.dilate(th, es, iterations=2) # 形态学膨胀
  16. image, contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 该函数计算一幅图像中目标的轮廓
  17. for c in contours:
  18. if cv2.contourArea(c) > 1600:
  19. (x, y, w, h) = cv2.boundingRect(c)
  20. cv2.rectangle(frame_lwpCV, (x, y), (x + w, y + h), (255, 255, 0), 2)
  21. print(time.time()-start,frame_lwpCV.shape)
  22. cv2.imshow('mog', fgmask)
  23. cv2.imshow('thresh', th)
  24. cv2.imshow('detection', frame_lwpCV)
  25. key = cv2.waitKey(1) & 0xFF
  26. # 按'q'健退出循环
  27. if key == ord('q'):
  28. break
  29. # When everything done, release the capture
  30. camera.release()
  31. cv2.destroyAllWindows()

花屏部分检测比较具体:不能屏蔽花屏

加了椭圆检测器:


  
  1. import time
  2. import cv2
  3. import numpy as np
  4. '''
  5. retval = cv.createBackgroundSubtractorKNN([, history[, dist2Threshold[, detectShadows]]])
  6. Parameters
  7. history Length of the history.
  8. dist2Threshold Threshold on the squared distance between the pixel and the sample to decide whether a pixel is close to that sample. This parameter does not affect the background update.
  9. detectShadows If true, the algorithm will detect shadows and mark them. It decreases the speed a bit, so if you do not need this feature, set the parameter to false.
  10. '''
  11. # KNN背景分割器
  12. knn = cv2.createBackgroundSubtractorKNN(detectShadows=True)
  13. camera = cv2.VideoCapture(0)
  14. def drawCnt(fn, cnt):
  15. if cv2.contourArea(cnt) > 1600:
  16. (x, y, w, h) = cv2.boundingRect(cnt)
  17. cv2.rectangle(fn, (x, y), (x + w, y + h), (255, 255, 0), 2)
  18. while True:
  19. ret, frame = camera.read()
  20. if not ret:
  21. break
  22. start=time.time()
  23. fgmask = knn.apply(frame)
  24. th = cv2.threshold(np.copy(fgmask), 244, 255, cv2.THRESH_BINARY)[1]
  25. es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
  26. dilated = cv2.dilate(th, es, iterations=2)
  27. image, contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  28. for c in contours:
  29. drawCnt(frame, c)
  30. print("time",time.time()-start,frame.shape)
  31. cv2.imshow("knn", fgmask)
  32. cv2.imshow("thresh", th)
  33. cv2.imshow("detection", frame)
  34. if cv2.waitKey(100) & 0xff == ord("q"):
  35. break
  36. camera.release()
  37. cv2.destroyAllWindows()

gmg,初始化很慢:人进入会引起小的图,


  
  1. import time
  2. import numpy as np
  3. import cv2
  4. '''
  5. retval = cv.bgsegm.createBackgroundSubtractorGMG( [, initializationFrames[, decisionThreshold]] )
  6. Parameters
  7. initializationFrames number of frames used to initialize the background models.
  8. decisionThreshold Threshold value, above which it is marked foreground, else background.
  9. '''
  10. # GMG背景分割器
  11. fgbg = cv2.bgsegm.createBackgroundSubtractorGMG()
  12. camera = cv2.VideoCapture(0)
  13. ret, frame = camera.read()
  14. while ret:
  15. start=time.time()
  16. fgmask = fgbg.apply(frame)
  17. th = cv2.threshold(np.copy(fgmask), 244, 255, cv2.THRESH_BINARY)[1]
  18. th = cv2.erode(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=2)
  19. dilated = cv2.dilate(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (8, 3)), iterations=2)
  20. image, contours, hier = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  21. for c in contours:
  22. if cv2.contourArea(c) > 1000:
  23. (x, y, w, h) = cv2.boundingRect(c)
  24. cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 0), 2)
  25. print("time",time.time()-start)
  26. cv2.imshow("GMG", fgmask)
  27. cv2.imshow("thresh", th)
  28. cv2.imshow("diff", frame & cv2.cvtColor(fgmask, cv2.COLOR_GRAY2BGR))
  29. cv2.imshow("detection", frame)
  30. ret, frame = camera.read() # 读取视频帧数据
  31. if cv2.waitKey(100) & 0xff == ord("q"):
  32. break
  33. camera.release()
  34. cv2.destroyAllWindows()

 

文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/95661082

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。