Skip to content
Snippets Groups Projects
Commit 4888e575 authored by Aylen Ricca's avatar Aylen Ricca
Browse files

changes to detect hand on c++ on pc

parent 265e9e87
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,10 @@
//opencv
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv/cv.h>
#include <opencv2/highgui/highgui.hpp>
//#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/video/background_segm.hpp>
//C
#include <stdio.h>
......@@ -21,8 +24,9 @@ using namespace std;
Mat frame; //current frame
Mat fgMaskMOG; //fg mask generated by MOG method
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
Ptr<BackgroundSubtractor> pMOG; //MOG Background subtractor
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
Ptr<BackgroundSubtractorMOG> pMOG; //MOG Background subtractor
Ptr<BackgroundSubtractorMOG2> pMOG2; //MOG2 Background subtractor
Mat morphKernel;
int keyboard; //input from keyboard
/** Function Headers */
......@@ -66,19 +70,18 @@ int main(int argc, char* argv[])
namedWindow("FG Mask MOG 2");
//create Background Subtractor objects
// pMOG= new BackgroundSubtractorMOG(200,5,0.7,0); //MOG approach
pMOG= new BackgroundSubtractorMOG(); //MOG approach
pMOG2 = new BackgroundSubtractorMOG2(); //MOG2 approach
// pMOG = createBackgroundSubtractorMOG(); //MOG approach
// pMOG2 = createBackgroundSubtractorMOG2(); //MOG2 approach
morphKernel = getStructuringElement(CV_SHAPE_RECT, Size(3, 3), Point(1, 1));
if(strcmp(argv[1], "-vid") == 0) {
//input data coming from a video
processVideo(argv[2]);
}
else if(strcmp(argv[1], "-img") == 0) {
//input data coming from a sequence of images
processImages(argv[2]);
}
else {
//error in reading input parameters
cerr <<"Please, check the input parameters." << endl;
......@@ -94,6 +97,8 @@ int main(int argc, char* argv[])
* @function processVideo
*/
void processVideo(char* videoFilename) {
int estadoANTERIOR = 0;
int estadoACTUAL = 0;
//create the capture object
VideoCapture capture(videoFilename);
if(!capture.isOpened()){
......@@ -101,8 +106,25 @@ void processVideo(char* videoFilename) {
cerr << "Unable to open video file: " << videoFilename << endl;
exit(EXIT_FAILURE);
}
while( (char)keyboard != 'p' ){
//read the current frame
if(!capture.read(frame)) {
cerr << "Unable to read next frame." << endl;
cerr << "Exiting..." << endl;
exit(EXIT_FAILURE);
}
imshow("Frame", frame);
keyboard = waitKey( 30 );
}
//read input data. ESC or 'q' for quitting
while( (char)keyboard != 'q' && (char)keyboard != 27 ){
if ((char)keyboard == 's'){
pMOG= new BackgroundSubtractorMOG(); //MOG approach
pMOG2 = new BackgroundSubtractorMOG2(); //MOG2 approach
}
//read the current frame
if(!capture.read(frame)) {
cerr << "Unable to read next frame." << endl;
......@@ -110,12 +132,46 @@ void processVideo(char* videoFilename) {
exit(EXIT_FAILURE);
}
//update the background model
pMOG->operator()(frame, fgMaskMOG);
pMOG2->operator()(frame, fgMaskMOG2);
pMOG->operator()(frame, fgMaskMOG,0);
pMOG2->operator()(frame, fgMaskMOG2,0);
erode(fgMaskMOG2, fgMaskMOG2, morphKernel);
cv::threshold(fgMaskMOG2, fgMaskMOG2, 200, 255, THRESH_BINARY);
Scalar suma = cv::sum(fgMaskMOG2);
// cerr << suma.val[0] << endl;
if (suma.val[0] > 1000000){
// estado oclusion
estadoANTERIOR = estadoACTUAL;
estadoACTUAL = 1;
cerr << "MANO !!!\n";
rectangle(frame, cv::Point(10, 2), cv::Point(100,20),cv::Scalar(255,255,255), -1);
putText(frame, "MANO !!!", cv::Point(15, 15),FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
} else {
estadoANTERIOR = estadoACTUAL;
if (estadoANTERIOR == 1){
// estado transicion
estadoACTUAL = 3;
}
if (estadoANTERIOR == 3){
// estado estable
estadoACTUAL = 2;
pMOG= new BackgroundSubtractorMOG(); //MOG approach
pMOG2 = new BackgroundSubtractorMOG2(); //MOG2 approach
}
cerr << "ESTABLE !!!\n";
rectangle(frame, cv::Point(10, 2), cv::Point(100,20),cv::Scalar(255,255,255), -1);
putText(frame, "ESTABLE !!!", cv::Point(15, 15),FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
}
// dilate(fgMaskMOG, fgMaskMOG, morphKernel, 1);
// pMOG->apply(frame, fgMaskMOG);
// pMOG2->apply(frame, fgMaskMOG2);
//get the frame number and write it on the current frame
//get the frame number and write it on the current frame
/*
stringstream ss;
rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
......@@ -136,87 +192,3 @@ void processVideo(char* videoFilename) {
//delete capture object
capture.release();
}
/**
* @function processImages
*/
void processImages(char* fistFrameFilename) {
//read the first file of the sequence
frame = imread(fistFrameFilename);
if(!frame.data){
//error in opening the first image
cerr << "Unable to open first image frame: " << fistFrameFilename << endl;
exit(EXIT_FAILURE);
}
//current image filename
string fn(fistFrameFilename);
//read input data. ESC or 'q' for quitting
while( (char)keyboard != 'q' && (char)keyboard != 27 ){
//update the background model
pMOG->operator()(frame, fgMaskMOG);
pMOG2->operator()(frame, fgMaskMOG2);
// pMOG->apply(frame, fgMaskMOG);
// pMOG2->apply(frame, fgMaskMOG2);
//get the frame number and write it on the current frame
size_t index = fn.find_last_of("/");
if(index == string::npos) {
index = fn.find_last_of("\\");
}
size_t index2 = fn.find_last_of(".");
string prefix = fn.substr(0,index+1);
string suffix = fn.substr(index2);
string frameNumberString = fn.substr(index+1, index2-index-1);
istringstream iss(frameNumberString);
int frameNumber = 0;
iss >> frameNumber;
rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
cv::Scalar(255,255,255), -1);
putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
//show the current frame and the fg masks
imshow("Frame", frame);
imshow("FG Mask MOG", fgMaskMOG);
imshow("FG Mask MOG 2", fgMaskMOG2);
//get the input from the keyboard
keyboard = waitKey( 30 );
//search for the next image in the sequence
ostringstream oss;
oss << (frameNumber + 1);
string nextFrameNumberString = oss.str();
string nextFrameFilename = prefix + nextFrameNumberString + suffix;
//read the next frame
frame = imread(nextFrameFilename);
if(!frame.data){
//error in opening the next image in the sequence
cerr << "Unable to open image frame: " << nextFrameFilename << endl;
exit(EXIT_FAILURE);
}
//update the path of the current frame
fn.assign(nextFrameFilename);
}
}
/*#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv ) {
cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateFileCapture(argv[1]);
IplImage* frame;
while(1) {
frame = cvQueryFrame(capture);
if(!frame) break;
cvShowImage("Example2", frame );
char c = cvWaitKey(33);
if(c == 27) break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("Example2");
return 0;
}
*/
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment