【问题标题】:OpeCV count people in a roomOpenCV 计算房间里的人数
【发布时间】:2016-05-13 13:52:12
【问题描述】:

我需要指导才能实现代码。目的是计算自习室里的学生人数。我的想法是:

1) 拍照空教室学习 2)在一天中的某个时间与坐在预定位置的学生合影,因为椅子不能移动。 3) 定义图片中与座位教室学习相对应的关键点。 4)两张照片的差异。 5)如果这些职位现在被占用(差异已经给出了可见的结果),那么计算与学生数量相对应的差异数量。

有人知道如何在代码中实现它吗?

Mat differenceFrame(Mat prev_frame, Mat curr_frame); 



int main(void) {

    cv::Mat frame, frame1, framedifference;
    int key = 0;

    frame = imread("2.jpg", CV_LOAD_IMAGE_COLOR);   // Read the file

        frame1 = imread("1.jpg", CV_LOAD_IMAGE_COLOR);   // Read the file

    while (key != 27){
        differenceFrame(frame, frame1);

        cv::absdiff(frame, frame1, framedifference);  

        key = 0;
        cv::imshow("stream", framedifference);
        key = cv::waitKey(10);
    }


                ContPeople(framedifference)  ?????

}

现在: 我试过这个解决方案。我不知道这是否是最有效的。 blob可以帮助我吗? 当我对图像进行差异化时,我将一些反射点标记为好像它们被改变了一样,我认为这是光线过多的问题,您可以细化差异以避免这些问题吗?

cv::Mat imgFrame1Copy = F_RoomFull.clone(); cv::Mat imgFrame2Copy = F_RoomEmpty.clone();

cv::Mat imgDifference;
cv::Mat imgThresh;

cv::cvtColor(imgFrame1Copy, imgFrame1Copy, CV_BGR2GRAY);
cv::cvtColor(imgFrame2Copy, imgFrame2Copy, CV_BGR2GRAY);

cv::GaussianBlur(imgFrame1Copy, imgFrame1Copy, cv::Size(5, 5), 0);
cv::GaussianBlur(imgFrame2Copy, imgFrame2Copy, cv::Size(5, 5), 0);

cv::absdiff(imgFrame1Copy, imgFrame2Copy, imgDifference);

cv::threshold(imgDifference, imgThresh, 180, 255, CV_THRESH_BINARY);

cv::imshow("imgThresh", imgThresh);

cv::Mat structuringElement3x3 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
cv::Mat structuringElement5x5 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5));
cv::Mat structuringElement7x7 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(7, 7));
cv::Mat structuringElement9x9 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(9, 9));

cv::dilate(imgThresh, imgThresh, structuringElement5x5);
cv::dilate(imgThresh, imgThresh, structuringElement5x5);
cv::erode(imgThresh, imgThresh, structuringElement5x5);

cv::Mat imgThreshCopy = imgThresh.clone();


cv::findContours(imgThreshCopy, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
cv::Mat imgContours(imgThresh.size(), CV_8UC3, SCALAR_BLACK);
cv::drawContours(imgContours, contours, -1, SCALAR_WHITE, -1);
cv::imshow("imgContours", imgContours);


printf("%d", contours.size());

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    当你减去这两个图像(矩阵)时,只有学生的位置会有非零值。其他值应为零或非常接近于零。

    设置一个阈值以将所有其他值设置为零(我的意思是如果在上一张图像中没有学生的位置有任何非零小值)。

    然后进行轮廓检测。 请参阅此处的代码: http://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html

    等高线的数量= 没有。学生人数

    如果两个轮廓重叠,使用轮廓面积来计算它们。期望最大轮廓不重叠

    【讨论】:

    • 除非学生拥抱(轮廓重叠)
    • 哈哈。我认为他们坐在不同的椅子上
    • 即使某些轮廓重叠,您也可以使用轮廓区域将它们分开。但是如果3-5组学生(每组2/3/4名学生)互相拥抱,那么情况就会非常复杂。如果 20 个学生中有 2 个或 3 个学生互相拥抱,那应该不是问题
    • 好的,但是由于假设学生坐在椅子上,因此相机的定位是简化成像识别问题的好方法。
    • 是的,我假设椅子以平行线排列,并且相机位置垂直于该线。提问者没有具体说明这些事情。所以我假设了通常的摄影场景。
    【解决方案2】:

    您还可以在 OpenCv 中使用背景减法方法。有很多参数可以进行微调。

    这里是链接Link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-31
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      • 2016-09-21
      • 2021-07-01
      • 2012-08-28
      • 2022-11-11
      相关资源
      最近更新 更多