本文介绍了尝试实现 cv2.findContours 进行人物检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 opencv 的新手,我正在尝试通过 cv2.findContours 对视频进行形态变换来检测人物.这是代码片段..

I'm new to opencv and I'm trying to detect person through cv2.findContours with morphological transformation of the video. Here is the code snippet..

import numpy as np
import imutils
import cv2 as cv
import time
cap = cv.VideoCapture(0)

while(cap.isOpened()):
    ret, frame = cap.read()

    #frame = imutils.resize(frame, width=700,height=100)

    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    gray = cv.GaussianBlur(gray, (21, 21), 0)



    cv.accumulateWeighted(gray, avg, 0.5)
    mask2 = cv.absdiff(gray, cv.convertScaleAbs(avg))
    mask = cv.absdiff(gray, cv.convertScaleAbs(avg))

    contours0, hierarchy = cv.findContours(mask2,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
    for cnt in contours0:
       .
       .
       .

其余代码具有轮廓通过一条线并递增计数的逻辑.

The rest of the code has the logic of a contour passing a line and incrementing the count.

我遇到的问题是, cv.findContours 检测框架中的每一个移动/变化(包括人).我想要的是 cv.findContours 只检测人而不是任何其他运动.我知道可以通过 harrcasacade 实现人物检测,但有什么方法可以使用 cv2.findContours 实现检测吗?

The problem I'm encountering is, cv.findContours detects every movement/change in the frame (including the person). What I want is cv.findContours to detect only person and not any other movement. I know that person detection can be achieved through harrcasacade but is there any way I can implement detection using cv2.findContours?

如果没有,那么有没有办法我仍然可以进行形态变换和检测人,因为我正在进行的项目需要过滤噪音和大部分背景来检测人并增加它通过线路的计数.

If not then is there a way I can still do morphological transformation and detect people because the project I'm working on requires filtering of noise and much of the background to detect the person and increment it's count on passing the line.

推荐答案

我将向您展示两个选项.

I will show you two options to do this.

我在评论中提到的方法,你可以用 Yolo 来检测人类:

The method I mentioned in the comments which you can use with Yolo to detect humans:

  1. 使用显着性检测视频的突出部分
  2. 应用 K 均值聚类将对象聚类为单个聚类.
  3. 应用背景减法和腐蚀或膨胀(或两者都取决于视频,但尝试所有这些,看看哪一个做得最好).
  4. 裁剪对象
  5. 将裁剪后的对象发送给 Yolo
  6. 如果班级名称是行人或人类,则在其上绘制边界框.

使用 OpenCV 的内置行人检测更容易:

Using OpenCV's builtin pedestrian detection which is much more easier:

  1. 将帧转换为黑白
  2. 对灰色帧使用行人级联检测多尺度().
  3. 在每个行人上画一个边界框

第二种方法要简单得多,但这取决于您对这个项目的期望.

Second method is much simpler but it depends what is expected of you for this project.

这篇关于尝试实现 cv2.findContours 进行人物检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 16:38