Commit 9d1c2012 authored by Benjamin Gallois's avatar Benjamin Gallois

Contours based detection

parent 071914fe
......@@ -6,7 +6,7 @@ SRC= $(IDIR)motionSensor.cpp
# compile
all: $(SRC)
$(CC) $(CFLAGS) $(SRC) -o motionSensor -I/usr/local/include/ -lraspicam -lraspicam_cv -lopencv_core -lopencv_highgui -pthread -lopencv_video -lopencv_imgcodecs -lopencv_imgproc -lstdc++fs
$(CC) $(CFLAGS) $(SRC) -o motionSensor -I/usr/local/include/ -lraspicam -lraspicam_cv -pthread -lstdc++fs -lopencv_core -lopencv_video -lopencv_imgcodecs -lopencv_imgproc
$(CC) $(CFLAGS) display.cpp -o display -I/usr/local/include/ -lraspicam -lraspicam_cv -lopencv_core -lopencv_highgui -pthread -lopencv_video -lopencv_imgcodecs -lopencv_imgproc
# remove compilation products
......
......@@ -6,8 +6,10 @@
#include <unistd.h>
#include <experimental/filesystem>
#include <raspicam/raspicam_cv.h>
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <opencv2/video/background_segm.hpp>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
......@@ -63,7 +65,7 @@ void grabFrame(queue<frame>& queue){
// Statistic variables
time_t timer_begin,timer_end;
int imgCount = 0;
double FPS = 20;
double FPS = 25; // Max 25 FPS
// Object initialization
frame frame;
......@@ -112,15 +114,18 @@ void grabFrame(queue<frame>& queue){
* Grab frame from the camera and insert it in a queue
* @param queue FIBO queue
* @type std::queue
* @param threshold
* @type double
*/
void worker(queue<frame>& queue){
void worker(queue<frame>& queue, double threshold){
Mat tmp;
frame frame;
bool notEmpty = false;
vector<vector<Point>> contours;
for(;;){ // Infinite loop
mtx.lock(); // Lock shared queue
mtx.lock(); // Lock shared queue a minimum of time
if ( !queue.empty() ) {
frame = queue.front();
queue.pop();
......@@ -128,19 +133,19 @@ void worker(queue<frame>& queue){
}
mtx.unlock();
if ( notEmpty ) {
//cv::imshow("raspicam_cv_image.jpg", frame.image);
//if(waitKey(1) >= 0) break;
if ( notEmpty ) { // Avoid deadlock if several worker threads wait for a frame and the queue is empty
analysisMtx.lock();
resize(frame.image, tmp, Size(640, 480)); // Resize analysis image to be faster
pMOG2->apply(tmp , fgMaskMOG2);
analysisMtx.unlock();
double maskSum = sum(fgMaskMOG2)[0];
if (maskSum > 5000000.){ // Condition to detect movement
cout << "Detect" << endl;
string name = "images/" + frame.timestamp + ".pgm";
cout<<name<<endl;
imwrite(name, frame.image);
findContours( fgMaskMOG2, contours, RETR_TREE, CHAIN_APPROX_SIMPLE ); // Find contours in the image
for (unsigned int i = 0; i < contours.size(); i++){
if (contourArea(contours[i]) > threshold) {
cout << "Detect" << endl;
string name = "images/" + frame.timestamp + ".pgm";
imwrite(name, frame.image);
}
}
}
......@@ -152,13 +157,15 @@ void worker(queue<frame>& queue){
}
int main(){
experimental::filesystem::create_directories("images/");
int nWorker = 3;
queue<frame> frameQueue;
queue<frame> frameQueue;
double threshold = 50000;
thread t1(grabFrame, ref(frameQueue));
thread workerPool[nWorker];
for (int i = 0; i < nWorker; i++ ){
workerPool[i] = thread(worker, ref(frameQueue));
workerPool[i] = thread(worker, ref(frameQueue), threshold);
}
t1.join();
for (int i = 0; i < nWorker; i++ ){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment