From 4f5e7f932d049cd2686d513688579925f8eb3192 Mon Sep 17 00:00:00 2001
From: Thomas Boy <thomas-boy@idiv.de>
Date: Tue, 28 Aug 2018 17:36:58 +0200
Subject: [PATCH] use different bk ground method, test, no bk gen needed

---
 Demo.cpp                 | 225 +++++++++++++++++++++------------------
 package_bgs/ttoolbox.cpp |  15 +--
 2 files changed, 128 insertions(+), 112 deletions(-)

diff --git a/Demo.cpp b/Demo.cpp
index e75a74c..6dada02 100644
--- a/Demo.cpp
+++ b/Demo.cpp
@@ -11,11 +11,12 @@ based  on original demo.cpp
 #include <stdlib.h>     /* srand, rand */
 #include <time.h>       /* time */
 #include <ctime>
+#include <signal.h>
 
 
 #define PROCESS_CENTER_VERSION_MAJOR 0
 #define PROCESS_CENTER_VERSION_MINOR 9
-#define PROCESS_CENTER_VERSION_MINOR_FIXES 8
+#define PROCESS_CENTER_VERSION_MINOR_FIXES 9
 
 //opencv
 #include <opencv2/opencv.hpp>
@@ -28,6 +29,17 @@ based  on original demo.cpp
 using namespace cv;
 using namespace std;
 
+int g_badSignalFlagAbort = 0;
+
+void my_handler(int signum)
+{
+    if (signum == SIGUSR1)
+    {
+        g_badSignalFlagAbort = 1;
+        cerr << "receive signal to abort"<<endl;
+    }
+}
+
 char* getCmdOption(char ** begin, char ** end, const std::string & option)
 {
     char ** itr = std::find(begin, end, option);
@@ -46,6 +58,7 @@ bool cmdOptionExists(char** begin, char** end, const std::string& option)
 
 int main(int argc, char * argv[])
 {
+    signal(SIGUSR1, my_handler);
 
     std::cout << "using processcenter  " <<PROCESS_CENTER_VERSION_MAJOR <<"."<< PROCESS_CENTER_VERSION_MINOR<<"."<<PROCESS_CENTER_VERSION_MINOR_FIXES<< endl;
     std::cout << "Using OpenCV " << CV_MAJOR_VERSION << "." << CV_MINOR_VERSION << "." << CV_SUBMINOR_VERSION << std::endl;
@@ -127,7 +140,7 @@ int main(int argc, char * argv[])
     //    }
 
     /* Background Subtraction Methods */
-    //IBGS *bgs;
+    IBGS *bgs;
 
     //bgs = new FrameDifference;
     //bgs = new StaticFrameDifference;
@@ -154,7 +167,7 @@ int main(int argc, char * argv[])
     //bgs = new FuzzySugenoIntegral;
     //bgs = new FuzzyChoquetIntegral;
     //bgs = new MultiLayer;
-    //bgs = new PixelBasedAdaptiveSegmenter;
+    bgs = new PixelBasedAdaptiveSegmenter;
     //bgs = new LBSimpleGaussian;
     //bgs = new LBFuzzyGaussian;
     //bgs = new LBMixtureOfGaussians;
@@ -173,7 +186,7 @@ int main(int argc, char * argv[])
     //bgs = new ViBe;
     //bgs = new Tapter;
 
-    Tapter *bgs = new Tapter;
+    //Tapter *bgs = new Tapter;
 
     //see paper https://dl.acm.org/citation.cfm?id=2321600
     //https://ieeexplore.ieee.org/document/4527178/
@@ -215,106 +228,106 @@ int main(int argc, char * argv[])
     cout  <<"circleRadius: "<< circleRadius<<endl;
     fsCen.release();
 
-    //first the static pic**********
-    //std::string fileName = getFileName(begin);
-    std::string staticFile = inputDir+"/bk.jpg";
-    cout <<"a) load first static background pic :"<< staticFile<<endl;
-
-    //we check for corrupt image
-    if(TToolBox::checkFileCorrupted(staticFile))
-    {
-        cout<<"error file: "<< staticFile<<" is corrupted, will abort"<<endl;
-        return  EXIT_FAILURE;
-    }
-
-    img_input = imread(staticFile.c_str(), CV_LOAD_IMAGE_COLOR);
-    if(img_input.data )
-    {
-        //we cut out a smaller ROI
-        img_input = TToolBox::cropImageCircle(img_input,circleCenterX,circleCenterY,circleRadius);
-
-        cv::Mat img_mask;
-        cv::Mat img_bkgmodel;
-        bgs->process(img_input, img_mask, img_bkgmodel);
-    }
-    else
-    {
-        cout<<"error loading file: "<< staticFile<<", will abort"<<endl;
-        return  EXIT_FAILURE;
-    }
-
+//    //first the static pic**********
+//    //std::string fileName = getFileName(begin);
+//    std::string staticFile = inputDir+"/bk.jpg";
+//    cout <<"a) load first static background pic :"<< staticFile<<endl;
+
+//    //we check for corrupt image
+//    if(TToolBox::checkFileCorrupted(staticFile))
+//    {
+//        cout<<"error file: "<< staticFile<<" is corrupted, will abort"<<endl;
+//        return  EXIT_FAILURE;
+//    }
+
+//    img_input = imread(staticFile.c_str(), CV_LOAD_IMAGE_COLOR);
+//    if(img_input.data )
+//    {
+//        //we cut out a smaller ROI
+//        img_input = TToolBox::cropImageCircle(img_input,circleCenterX,circleCenterY,circleRadius);
+
+//        cv::Mat img_mask;
+//        cv::Mat img_bkgmodel;
+//        bgs->process(img_input, img_mask, img_bkgmodel);
+//    }
+//    else
+//    {
+//        cout<<"error loading file: "<< staticFile<<", will abort"<<endl;
+//        return  EXIT_FAILURE;
+//    }
+
+//    std::string fileName;
+
+//    //! we train with first x on random draws
+//    int amountTrainingSteps = 10;
+////    //we open the config file and readin
+////    cv::String  configFileName("./config/Tapter.xml");
+////    {//read the config
+////        FileStorage fs;
+////        fs.open(configFileName, FileStorage::READ);
+////        if (!fs.isOpened())
+////        {
+////            cout << "error during open " << configFileName <<  " will abort " <<endl;
+////            return EXIT_FAILURE;
+////        }
+////        //param
+////        amountTrainingSteps = (int) fs["trainingSteps"];
+////        //cout <<"amountTrainingSteps: "<< amountTrainingSteps<< endl;
+////        fs.release();
+////    }
+
+//    cout <<"b) we train the background with random choosen files"<< fileName<<endl;
+//    //int j=255;
+//    vector<string> myRandomTrainList; //we save all draws in a list which will save to the results
+//    for(i=0;i<amountTrainingSteps;i++)
+//    {
+
+//        //random index
+//        int index  = rand() % amountFiles; //TODO: double check no double draw ??
+//        fileName = inputDir +  TToolBox::getFileName(index);
+//        myRandomTrainList.push_back(fileName);
+
+//        //we check for corrupt image
+//        if(TToolBox::checkFileCorrupted(fileName))
+//        {
+//            cout<<"error file: "<< fileName<<" is corrupted, will ignore it and draw new"<<endl;
+//            i--;
+//            continue; //we jump over the rest //TODO bad style
+//        }
+
+//        cout <<"\t"<<i <<"\t of \t"<<amountTrainingSteps<<" rnd file :"<< fileName<<endl;
+//        img_input = imread(fileName.c_str(), CV_LOAD_IMAGE_COLOR);
+
+//        if(img_input.data )
+//        {
+//            //we cut out a smaller ROI
+//            img_input = TToolBox::cropImageCircle(img_input,circleCenterX,circleCenterY,circleRadius);
+
+//            cv::Mat img_mask;
+//            cv::Mat img_bkgmodel;
+
+//            bgs->process(img_input, img_mask, img_bkgmodel); // by default, it shows automatically the foreground mask image
+
+//        }
+//        else
+//        {
+//            cout<<"error loading file: "<< fileName<<", will draw again"<<endl;
+//            i--;
+//            //return  EXIT_FAILURE;
+//        }
+//    }
+
+    cout <<"a) we process all frames "<<endl;
+
+    int everyPic= 60*5;
     std::string fileName;
-
-    //! we train with first x on random draws
-    int amountTrainingSteps = 200;
-    //we open the config file and readin
-    cv::String  configFileName("./config/Tapter.xml");
-    {//read the config
-        FileStorage fs;
-        fs.open(configFileName, FileStorage::READ);
-        if (!fs.isOpened())
-        {
-            cout << "error during open " << configFileName <<  " will abort " <<endl;
-            return EXIT_FAILURE;
-        }
-        //param
-        amountTrainingSteps = (int) fs["trainingSteps"];
-        //cout <<"amountTrainingSteps: "<< amountTrainingSteps<< endl;
-        fs.release();
-    }
-
-    cout <<"b) we train the background with random choosen files"<< fileName<<endl;
-    //int j=255;
-    vector<string> myRandomTrainList; //we save all draws in a list which will save to the results
-    for(i=0;i<amountTrainingSteps;i++)
-    {
-
-        //random index
-        int index  = rand() % amountFiles; //TODO: double check no double draw ??
-        fileName = inputDir +  TToolBox::getFileName(index);
-        myRandomTrainList.push_back(fileName);
-
-        //we check for corrupt image
-        if(TToolBox::checkFileCorrupted(fileName))
-        {
-            cout<<"error file: "<< fileName<<" is corrupted, will ignore it and draw new"<<endl;
-            i--;
-            continue; //we jump over the rest //TODO bad style
-        }
-
-        cout <<"\t"<<i <<"\t of \t"<<amountTrainingSteps<<" rnd file :"<< fileName<<endl;
-        img_input = imread(fileName.c_str(), CV_LOAD_IMAGE_COLOR);
-
-        if(img_input.data )
-        {
-            //we cut out a smaller ROI
-            img_input = TToolBox::cropImageCircle(img_input,circleCenterX,circleCenterY,circleRadius);
-
-            cv::Mat img_mask;
-            cv::Mat img_bkgmodel;
-
-            bgs->process(img_input, img_mask, img_bkgmodel); // by default, it shows automatically the foreground mask image
-
-        }
-        else
-        {
-            cout<<"error loading file: "<< fileName<<", will draw again"<<endl;
-            i--;
-            //return  EXIT_FAILURE;
-        }
-    }
-
-    cout <<"c) we produce train the background with random choosen files"<< fileName<<endl;
-
-    int everyPic=60*5;
-
     int frameCounter=0;
     vector<string> myFileErrorList;//list for pure io error
     vector<string> myFileListNoContour; //list for no contours found;
     vector<string> myFileListAfterContourSelection; //list for no contours found after selection;
     vector<string> myFileListCorrupted; //list all corrupted jpg files
 
-    for(frameCounter=0;frameCounter<amountFiles;frameCounter++)
+    for(frameCounter=0;frameCounter<amountFiles||g_badSignalFlagAbort;frameCounter++)
     {
         //measure time consumption
         clock_t begin = clock();
@@ -494,8 +507,8 @@ int main(int argc, char * argv[])
                             muConvexHull = moments(conHull, true );
                             muConvexHullMassCenter= Point2f( muConvexHull.m10/muConvexHull.m00 , muConvexHull.m01/muConvexHull.m00 );
                         }
-
-                    }//end after selection is empty
+                    }
+                    else //end after selection is empty
                     {
                         cerr<<"error, no contour found after selection in file: "<< fileName<<endl;
                         myFileListAfterContourSelection.push_back(fileName);
@@ -531,9 +544,12 @@ int main(int argc, char * argv[])
 
 #endif
 
+                //cout <<" found center at : "<< massCenters.at(0).x<< ";"<<massCenters.at(0).y<<endl;
                 //! step 8: we write down all our results in yml file
                 std::string nameOutPutFileData =  outputDir + TToolBox::mNzero(frameCounter) + ".yml";
 
+                //cout <<"output file:" << nameOutPutFileData;
+
                 FileStorage fs(nameOutPutFileData.c_str(), FileStorage::WRITE);
                 fs << "masscenters" <<  massCenters;
                 fs << "polygonselection"<< contourSelection;
@@ -614,11 +630,11 @@ int main(int argc, char * argv[])
     double elapsed = double(endAll - beginAll) / CLOCKS_PER_SEC;
     cout <<"process : "<<amountFiles<<" files took:\t"<<(int)(elapsed/60)<<" min -\t"<<(int)(elapsed/60/60)<<" h \n in total"<<endl;
 
-    //we write the random file list to a file
-    std::string nameOutRandomFile =  outputDir + "randlist.yml";
-    FileStorage fs(nameOutRandomFile.c_str(), FileStorage::WRITE);
-    fs << "randomlist" <<  myRandomTrainList;
-    fs.release();
+//    //we write the random file list to a file
+//    std::string nameOutRandomFile =  outputDir + "randlist.yml";
+//    FileStorage fs(nameOutRandomFile.c_str(), FileStorage::WRITE);
+//    fs << "randomlist" <<  myRandomTrainList;
+//    fs.release();
 
     //TODO we should merge the file
 
@@ -641,7 +657,6 @@ int main(int argc, char * argv[])
     cout <<"amount of corrupted jpg files: "<< myFileListCorrupted.size()<<endl;
     fs2 << "fileErrorIOcorruptFiles" <<  myFileListCorrupted;
 
-
     fs2.release();
 
     delete bgs;
diff --git a/package_bgs/ttoolbox.cpp b/package_bgs/ttoolbox.cpp
index 8de7870..d173422 100644
--- a/package_bgs/ttoolbox.cpp
+++ b/package_bgs/ttoolbox.cpp
@@ -43,22 +43,23 @@ std::vector<std::vector<cv::Point>> TToolBox::applyCannyEdgeAndCalcCountours(cv:
     // Find contours, use RETR_EXTERNAL ignore inner child structures, TREE more usefull ?
     cv::findContours( imgCannyEdge, contours, hierarchy, cv::RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );
 
+#define MC_SHOW_STEP_ANALYSE
 #ifdef  MC_SHOW_STEP_ANALYSE
     // Draw contours on extra mat
-    Mat imgContour = Mat::zeros( imgCannyEdge.size(), CV_8UC3 );
-    imgContour =  Scalar(255,255,255); //fille the picture
-    RNG rng(232323);
+    cv::Mat imgContour = cv::Mat::zeros( imgCannyEdge.size(), CV_8UC3 );
+    imgContour =  cv::Scalar(255,255,255); //fille the picture
+    cv::RNG rng(232323);
     for( size_t i = 0; i< contours.size(); i++ )
     {
         //random color
-        Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
+        cv::Scalar color = cv::Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
         //contour
-        drawContours( imgContour, contours, i, color, 1, LINE_AA, hierarchy, 0, Point() );
+        cv::drawContours( imgContour, contours, i, color, 1, cv::LINE_AA, hierarchy, 0, cv::Point() );
     }
 
-    cv::String outpath=  "/homes/tb55xemi/work/bugTrainingSet/testRec/rec04379437pp/result/";
+    cv::String outpath=  "/homes/tb55xemi/work/dev/bgslibrary-bgslib_qtgui_2.0.0/build/test0815/results";
     std::ostringstream convert;
-    convert << outpath << frameCounter <<"_countour_canny_edge.jpg";
+    convert << outpath << "test_countour_canny_edge.jpg";
     cv::imwrite(convert.str().c_str(), imgContour);
 
 #endif
-- 
GitLab