I work on C++ crowd detection code with “OpenCV”, that takes 2 frames and subtracts them. Then compare the result with threshold.
This is the first time I deal with “OpenCV” for C++ and I don't have much information about it.
These are the steps of the code:
c++ code:
#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 ";
}
}
The problem is : The threshold always be zero! and the output always: crowd detected even if there is no crowd
We don't care about the output image because we won't use it, and we just want to know the last value of threshold.
My aim is to know how much deference between 2 frames. I want to compare the deference with threshold to detect the human crowd in specific place
I hope that one of you can help me
Thank you
there's a couple of flaws in your usage of the threshold function
what you probably wanted, is :
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];
[sidenote: if you really want to detect human crowds, you'll have to put much more sweat into this. just diff'ing frames is prone to err with illumination changes, also, there's birds, cars and traffic lights]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With