gpt4 book ai didi

c++ - 使用 OpenCV/C++ 进行运动检测,阈值始终为零

转载 作者:太空狗 更新时间:2023-10-29 23:05:17 28 4
gpt4 key购买 nike

我使用“OpenCV”编写 C++ 人群检测代码,它需要 2 帧并减去它们。然后将结果与阈值进行比较。

这是我第一次处理 C++ 的“OpenCV”,我没有太多关于它的信息。

这些是代码的步骤:

  1. 拍摄两个视频帧,中间间隔 α 分钟。
  2. 将帧转换为黑白图像。
  3. 减去两个帧。
  4. 将差异与阈值进行比较。
  5. If Difference <= threshold then crowd detected else--> 没有人群。

C++代码:

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main (int argc, const char * argv[])
{
//first frame.
Mat current_frame = imread("image1.jpg",CV_LOAD_IMAGE_GRAYSCALE);

//secunde frame.
Mat previous_frame = imread("image2.jpg",CV_LOAD_IMAGE_GRAYSCALE);

//Minus the current frame from the previous_frame and store the result.
Mat result = current_frame-previous_frame;

//compare the difference with threshold, if the deference <70 -> there is crowd.
//if it is > 70 there is no crowd
int threshold= cv::threshold(result,result,0,70,CV_THRESH_BINARY);

if (threshold==0) {
cout<< "crowd detected \n";
}
else {
cout<<" no crowd detected \n ";
}
}

问题是:阈值永远为零!输出总是:检测到人群即使没有人群

我们不关心输出图像,因为我们不会使用它,我们只想知道阈值的最后一个值。

我的目标是了解 2 帧之间的差异程度。我想比较尊重度和阈值来检测特定地方的人群

我希望你们中的一个能帮助我

谢谢

最佳答案

您对 threshold 的使用存在一些缺陷功能

  • 它只返回阈值(不是你期望的)
  • “我们不关心输出图像”——好吧,你最好!
  • 如果您想针对 70 的值设定阈值,那应该是您的第 3 个参数,而不是第 4 个(第 4 个参数是值,任何 >thresh 都设置为

你可能想要的是:

cv::threshold(result,result,70,1, CV_THRESH_BINARY); // same as : result = result>70;
int on_pixels = countNonZero(result);
// or:
int on_pixels = sum(result)[0];

[旁注:如果你真的想检测人群,你将不得不付出更多的努力。只是差分帧容易随着光照变化而出错,还有鸟类、汽车和交通信号灯]

关于c++ - 使用 OpenCV/C++ 进行运动检测,阈值始终为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20082797/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com