使用OpenCV/C+进行运动检测+;,阈值总是变为零 我使用C++的人群检测代码,使用“OpenCV”,它取2帧并减去它们。然后将结果与阈值进行比较 这是我第一次处理C++的OpenCV,我没有太多的信息。

使用OpenCV/C+进行运动检测+;,阈值总是变为零 我使用C++的人群检测代码,使用“OpenCV”,它取2帧并减去它们。然后将结果与阈值进行比较 这是我第一次处理C++的OpenCV,我没有太多的信息。,c++,opencv,image-processing,motion-detection,C++,Opencv,Image Processing,Motion Detection,以下是代码的步骤: 拍摄两个视频帧,中间间隔α分钟 将帧转换为黑白图像 减去两帧 将差异与阈值进行比较 如果没有差异,就没有人群 c++代码: #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main (int argc, const char * argv[]) { //first frame. Mat current_fra

以下是代码的步骤:

  • 拍摄两个视频帧,中间间隔α分钟
  • 将帧转换为黑白图像
  • 减去两帧
  • 将差异与阈值进行比较
  • 如果没有差异,就没有人群
  • 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 ";
    }   
    }
    
    #包括
    #包括
    使用名称空间std;
    使用名称空间cv;
    int main(int argc,const char*argv[]
    {
    //第一帧。
    Mat current_frame=imread(“image1.jpg”,CV_LOAD_IMAGE_GRAYSCALE);
    //第二个框架。
    Mat previous_frame=imread(“image2.jpg”,CV_LOAD_IMAGE_GRAYSCALE);
    //从上一个_帧减去当前帧并存储结果。
    Mat结果=当前_帧-上一个_帧;
    //如果存在差异,则将差异与阈值进行比较。
    //如果大于70,则没有人群
    int threshold=cv::threshold(结果,结果,0,70,cv_THRESH_二进制);
    如果(阈值==0){
    
    cout在使用该函数时有几个缺陷

    • 它只返回阈值(不是您期望的值)
    • “我们不关心输出图像”-好吧,你最好
    • 如果您想根据70的值设置阈值,则应为第3个参数,而不是第4个参数(第4个参数是值,anything>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];
    

    [旁注:如果你真的想探测人群,你就得投入更多的精力。光是不同的帧很容易在光照变化时出错,还有鸟类、汽车和交通灯]

    我尝试了这段代码,阈值变为70,我的目的是想知道两帧之间的差异有多大。我想将差异与阈值进行比较,以检测特定位置的人群。你没有得到它。返回值是无关的。你不能用它做任何事情。从阈值返回值还是什么?我不理解你的评论*我不明白你的旁注,你能给我解释一下吗?而且你可能想做
    absdiff
    而不仅仅是
    diff
    当你考虑到框架的差异时,诚然,主要的问题仍然是,所有这些都太幼稚了,没有用处