From d71ebc42874d041e4e7c5933b728b83c0c096ddb Mon Sep 17 00:00:00 2001
From: Andrews Sobral <andrewssobral@gmail.com>
Date: Mon, 20 Mar 2017 00:03:50 +0100
Subject: [PATCH] Fixes for CodeBook algorithm

---
 Demo.py                                         |  1 +
 VideoCapture.cpp                                | 16 ++++++++++------
 VideoCapture.h                                  |  5 +++--
 wrapper_matlab/backgroundSubtractor_wrapper.cpp |  5 +++++
 wrapper_matlab/compile.m                        |  3 ++-
 wrapper_matlab/run_demo.m                       |  3 ++-
 wrapper_python/bgslibrary_module.cpp            |  5 +++++
 7 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/Demo.py b/Demo.py
index b1f6c02..174857e 100644
--- a/Demo.py
+++ b/Demo.py
@@ -45,6 +45,7 @@ bgs = libbgs.FrameDifference()
 #bgs = libbgs.WeightedMovingVariance()
 #bgs = libbgs.TwoPoints()
 #bgs = libbgs.ViBe()
+#bgs = libbgs.CodeBook()
 
 video_file = "dataset/video.avi"
 
diff --git a/VideoCapture.cpp b/VideoCapture.cpp
index c7e12ea..ed8964d 100644
--- a/VideoCapture.cpp
+++ b/VideoCapture.cpp
@@ -135,14 +135,18 @@ namespace bgslibrary
     if (useVideo)  setUpVideo();
     //if (!capture)  std::cerr << "Capture error..." << std::endl;
 
-    using namespace std::chrono_literals;
+    //using namespace std::chrono_literals;
     do
     {
       capture >> frame;
       if (frame.empty())
       {
         std::cout << "Frame is not ready" << std::endl;
-        std::this_thread::sleep_for(1s);
+        std::string dummy;
+        std::cout << "Enter to continue..." << std::endl;
+        std::getline(std::cin, dummy);
+        //cv::waitKey(1000);
+        //std::this_thread::sleep_for(1s);
       }
       else
         break;
@@ -193,7 +197,7 @@ namespace bgslibrary
             std::cout << "Set ROI (press ESC to skip)" << std::endl;
             VC_ROI::img_input1 = new IplImage(img_input);
             cvSetMouseCallback("Input", VC_ROI::VideoCapture_on_mouse, NULL);
-            key = cvWaitKey(0);
+            key = cv::waitKey(0);
             delete VC_ROI::img_input1;
           }
           else
@@ -239,17 +243,17 @@ namespace bgslibrary
 
       //cvResetImageROI(frame);
 
-      key = cvWaitKey(loopDelay);
+      key = cv::waitKey(loopDelay);
       //std::cout << "key: " << key << std::endl;
 
       if (key == KEY_SPACE)
-        key = cvWaitKey(0);
+        key = cv::waitKey(0);
 
       if (key == KEY_ESC)
         break;
 
       if (stopAt > 0 && stopAt == frameNumber)
-        key = cvWaitKey(0);
+        key = cv::waitKey(0);
 
       firstTime = false;
     } while (1);
diff --git a/VideoCapture.h b/VideoCapture.h
index b787682..8b7cd63 100644
--- a/VideoCapture.h
+++ b/VideoCapture.h
@@ -17,8 +17,9 @@ along with BGSLibrary.  If not, see <http://www.gnu.org/licenses/>.
 #pragma once
 
 #include <iostream>
-#include <chrono>
-#include <thread>
+#include <fstream>
+//#include <chrono>
+//#include <thread>
 #include <opencv2/opencv.hpp>
 #include <opencv2/imgproc/imgproc_c.h>
 #include <opencv2/imgproc/types_c.h>
diff --git a/wrapper_matlab/backgroundSubtractor_wrapper.cpp b/wrapper_matlab/backgroundSubtractor_wrapper.cpp
index 5470120..96fb4a6 100644
--- a/wrapper_matlab/backgroundSubtractor_wrapper.cpp
+++ b/wrapper_matlab/backgroundSubtractor_wrapper.cpp
@@ -66,6 +66,7 @@
 #include "PAWCS.h"
 #include "TwoPoints.h"
 #include "ViBe.h"
+#include "CodeBook.h"
 
 using namespace bgslibrary::algorithms;
 
@@ -165,6 +166,8 @@ namespace bgslibrary
       return (IBGS *)mxCalloc(1, sizeof(TwoPoints));
     if (alg_name.compare("ViBe") == 0)
       return (IBGS *)mxCalloc(1, sizeof(ViBe));
+    if (alg_name.compare("CodeBook") == 0)
+      return (IBGS *)mxCalloc(1, sizeof(CodeBook));
     return NULL;
   }
 
@@ -260,6 +263,8 @@ namespace bgslibrary
       return new (ptrBGS) TwoPoints();
     if (alg_name.compare("ViBe") == 0)
       return new (ptrBGS) ViBe();
+    if (alg_name.compare("CodeBook") == 0)
+      return new (ptrBGS) CodeBook();
     return NULL;
   }
 }
diff --git a/wrapper_matlab/compile.m b/wrapper_matlab/compile.m
index 078834e..36ddf84 100644
--- a/wrapper_matlab/compile.m
+++ b/wrapper_matlab/compile.m
@@ -91,4 +91,5 @@ mexOpenCV -v -DMEX_COMPILE_FLAG -I"../package_bgs" backgroundSubtractor_wrapper.
   "../package_bgs/ViBe.cpp" ...
   "../package_bgs/ViBe/vibe-background-sequential.cpp" ...
   "../package_bgs/TwoPoints.cpp" ...
-  "../package_bgs/TwoPoints/two_points.cpp"
+  "../package_bgs/TwoPoints/two_points.cpp" ...
+  "../package_bgs/CodeBook.cpp"
diff --git a/wrapper_matlab/run_demo.m b/wrapper_matlab/run_demo.m
index d7afed4..72307de 100644
--- a/wrapper_matlab/run_demo.m
+++ b/wrapper_matlab/run_demo.m
@@ -56,4 +56,5 @@ demo;
 % demo('LOBSTER')
 % demo('PAWCS')
 % demo('TwoPoints')
-% demo('ViBe')
\ No newline at end of file
+% demo('ViBe')
+% demo('CodeBook')
\ No newline at end of file
diff --git a/wrapper_python/bgslibrary_module.cpp b/wrapper_python/bgslibrary_module.cpp
index edb75a8..a8aa624 100644
--- a/wrapper_python/bgslibrary_module.cpp
+++ b/wrapper_python/bgslibrary_module.cpp
@@ -260,6 +260,11 @@ namespace fs
 				.def("apply", &ViBe::apply)
 				.def("getBackgroundModel", &ViBe::getBackgroundModel)
 				;
+
+			py::class_<CodeBook>("CodeBook")
+				.def("apply", &CodeBook::apply)
+				.def("getBackgroundModel", &CodeBook::getBackgroundModel)
+				;
 		}
 	} // namespace fs
 } // namespace python
-- 
GitLab