diff --git a/examples/Demo2.cpp b/examples/Demo2.cpp
index 8b4f69839d141345a01c423d1bd98d3d34b287bb..2c4b8ad9db427a0599f16f31be9bc4af06996da4 100644
--- a/examples/Demo2.cpp
+++ b/examples/Demo2.cpp
@@ -63,6 +63,7 @@ int main(int argc, char **argv)
       {
         std::cout << "Exception occurred" << std::endl;
         std::cout << e.what() << std::endl;
+        break;
       }
 
       key = cv::waitKey(33);
diff --git a/src/algorithms/DPAdaptiveMedian.cpp b/src/algorithms/DPAdaptiveMedian.cpp
index e121bdf3348f9b61cc699158ef83bb6cc02aebe3..e6fd808d823ba3cbbd8dd885558020cf1cd6a241 100644
--- a/src/algorithms/DPAdaptiveMedian.cpp
+++ b/src/algorithms/DPAdaptiveMedian.cpp
@@ -21,11 +21,8 @@ void DPAdaptiveMedian::process(const cv::Mat &img_input, cv::Mat &img_output, cv
 {
   init(img_input, img_output, img_bgmodel);
 
-  frame = new IplImage(img_input);
-
-  if (firstTime)
-    frame_data.ReleaseMemory(false);
-  frame_data = frame;
+  IplImage _frame = cvIplImage(img_input);
+  frame_data = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
     int width = img_input.size().width;
@@ -52,7 +49,7 @@ void DPAdaptiveMedian::process(const cv::Mat &img_input, cv::Mat &img_output, cv
   bgs.Update(frameNumber, frame_data, lowThresholdMask);
 
   img_foreground = cv::cvarrToMat(highThresholdMask.Ptr());
-  //bitwise_not(img_foreground, img_foreground);
+  // bitwise_not(img_foreground, img_foreground);
   img_background = cv::cvarrToMat(bgs.Background()->Ptr());
 
 #ifndef MEX_COMPILE_FLAG
@@ -65,7 +62,6 @@ void DPAdaptiveMedian::process(const cv::Mat &img_input, cv::Mat &img_output, cv
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
   firstTime = false;
   frameNumber++;
 }
diff --git a/src/algorithms/DPAdaptiveMedian.h b/src/algorithms/DPAdaptiveMedian.h
index fbad2da636f3553c842ee5ff5ebe9d0beca80f56..9d00cea0c9998764dcc4702778f33f03fec34d97 100644
--- a/src/algorithms/DPAdaptiveMedian.h
+++ b/src/algorithms/DPAdaptiveMedian.h
@@ -17,7 +17,6 @@ namespace bgslibrary
       int threshold;
       int samplingRate;
       int learningFrames;
-      IplImage* frame;
       dp::RgbImage frame_data;
       dp::AdaptiveMedianParams params;
       dp::AdaptiveMedianBGS bgs;
diff --git a/src/algorithms/DPEigenbackground.cpp b/src/algorithms/DPEigenbackground.cpp
index dfc7d64dba4bb9f78126e2eb3b1c99925beab4ef..2627cdc1deffc6632f58323a557463cd38b4b1e1 100644
--- a/src/algorithms/DPEigenbackground.cpp
+++ b/src/algorithms/DPEigenbackground.cpp
@@ -21,11 +21,8 @@ void DPEigenbackground::process(const cv::Mat &img_input, cv::Mat &img_output, c
 {
   init(img_input, img_output, img_bgmodel);
 
-  frame = new IplImage(img_input);
-
-  if (firstTime)
-    frame_data.ReleaseMemory(false);
-  frame_data = frame;
+  IplImage _frame = cvIplImage(img_input);
+  frame_data = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
     int width = img_input.size().width;
@@ -65,7 +62,6 @@ void DPEigenbackground::process(const cv::Mat &img_input, cv::Mat &img_output, c
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
   firstTime = false;
   frameNumber++;
 }
diff --git a/src/algorithms/DPEigenbackground.h b/src/algorithms/DPEigenbackground.h
index 0d9b18dff20b25c6448e64429cfa5cb9a50fce71..6db3de41846e69bdd973be3ec8daffd4576e7506 100644
--- a/src/algorithms/DPEigenbackground.h
+++ b/src/algorithms/DPEigenbackground.h
@@ -18,7 +18,6 @@ namespace bgslibrary
       int threshold;
       int historySize;
       int embeddedDim;
-      IplImage* frame;
       dp::RgbImage frame_data;
       dp::EigenbackgroundParams params;
       dp::Eigenbackground bgs;
diff --git a/src/algorithms/DPGrimsonGMM.cpp b/src/algorithms/DPGrimsonGMM.cpp
index 743acf19e517ea82de47e6ab10711c10b1b6405b..3febe641c6aaa25c17e69d502ded562e789f856d 100644
--- a/src/algorithms/DPGrimsonGMM.cpp
+++ b/src/algorithms/DPGrimsonGMM.cpp
@@ -21,11 +21,8 @@ void DPGrimsonGMM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Ma
 {
   init(img_input, img_output, img_bgmodel);
 
-  frame = new IplImage(img_input);
-
-  if (firstTime)
-    frame_data.ReleaseMemory(false);
-  frame_data = frame;
+  IplImage _frame = cvIplImage(img_input);
+  frame_data = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
     int width = img_input.size().width;
@@ -64,7 +61,6 @@ void DPGrimsonGMM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Ma
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
   firstTime = false;
   frameNumber++;
 }
diff --git a/src/algorithms/DPMean.cpp b/src/algorithms/DPMean.cpp
index 18320cd5e57b52be02c29323578b6213c21182b6..673e9df39e67032b0f7caccd2890f4d5707829de 100644
--- a/src/algorithms/DPMean.cpp
+++ b/src/algorithms/DPMean.cpp
@@ -21,11 +21,8 @@ void DPMean::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img
 {
   init(img_input, img_output, img_bgmodel);
 
-  frame = new IplImage(img_input);
-
-  if (firstTime)
-    frame_data.ReleaseMemory(false);
-  frame_data = frame;
+  IplImage _frame = cvIplImage(img_input);
+  frame_data = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
     int width = img_input.size().width;
@@ -64,7 +61,6 @@ void DPMean::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
   firstTime = false;
   frameNumber++;
 }
diff --git a/src/algorithms/DPPratiMediod.cpp b/src/algorithms/DPPratiMediod.cpp
index 3cade2078531a25406f2eb5620614c84b8dd54c5..12ea039865ff52d43e5a8de25ee8ab70e72c71cc 100644
--- a/src/algorithms/DPPratiMediod.cpp
+++ b/src/algorithms/DPPratiMediod.cpp
@@ -21,11 +21,8 @@ void DPPratiMediod::process(const cv::Mat &img_input, cv::Mat &img_output, cv::M
 {
   init(img_input, img_output, img_bgmodel);
 
-  frame = new IplImage(img_input);
-
-  if (firstTime)
-    frame_data.ReleaseMemory(false);
-  frame_data = frame;
+  IplImage _frame = cvIplImage(img_input);
+  frame_data = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
     int width = img_input.size().width;
@@ -65,7 +62,6 @@ void DPPratiMediod::process(const cv::Mat &img_input, cv::Mat &img_output, cv::M
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
   firstTime = false;
   frameNumber++;
 }
diff --git a/src/algorithms/DPTexture.cpp b/src/algorithms/DPTexture.cpp
index 7f58230711e7c098f04a245f5f2e623dfcb0d845..0959339bf7df1907683ae18f2d25bb86f3398b2e 100644
--- a/src/algorithms/DPTexture.cpp
+++ b/src/algorithms/DPTexture.cpp
@@ -29,7 +29,8 @@ void DPTexture::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &
 {
   init(img_input, img_output, img_bgmodel);
 
-  frame = new IplImage(img_input);
+  IplImage _frame = cvIplImage(img_input);
+  frame = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
     width = img_input.size().width;
@@ -112,8 +113,6 @@ void DPTexture::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &
 
   // update background subtraction
   bgs.UpdateModel(fgMask, bgModel, curTextureHist, modeArray);
-
-  delete frame;
 }
 
 void DPTexture::save_config(cv::FileStorage &fs) {
diff --git a/src/algorithms/DPWrenGA.cpp b/src/algorithms/DPWrenGA.cpp
index a7f91288855233f028b1ad34c6246b18ab96fa23..971e4d4db931978c04e828187c1748a33a383b4e 100644
--- a/src/algorithms/DPWrenGA.cpp
+++ b/src/algorithms/DPWrenGA.cpp
@@ -21,11 +21,8 @@ void DPWrenGA::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &i
 {
   init(img_input, img_output, img_bgmodel);
 
-  frame = new IplImage(img_input);
-
-  if (firstTime)
-    frame_data.ReleaseMemory(false);
-  frame_data = frame;
+  IplImage _frame = cvIplImage(img_input);
+  frame_data = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
     int width = img_input.size().width;
@@ -63,7 +60,6 @@ void DPWrenGA::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &i
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
   firstTime = false;
   frameNumber++;
 }
diff --git a/src/algorithms/DPZivkovicAGMM.cpp b/src/algorithms/DPZivkovicAGMM.cpp
index 289f5cc137a173769529454817d53b793fa0388d..1d2efbe9105a610b158aec1974ea3f2504b8599b 100644
--- a/src/algorithms/DPZivkovicAGMM.cpp
+++ b/src/algorithms/DPZivkovicAGMM.cpp
@@ -21,11 +21,8 @@ void DPZivkovicAGMM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::
 {
   init(img_input, img_output, img_bgmodel);
 
-  frame = new IplImage(img_input);
-
-  if (firstTime)
-    frame_data.ReleaseMemory(false);
-  frame_data = frame;
+  IplImage _frame = cvIplImage(img_input);
+  frame_data = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
     int width = img_input.size().width;
@@ -63,7 +60,6 @@ void DPZivkovicAGMM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
   firstTime = false;
   frameNumber++;
 }
diff --git a/src/algorithms/FuzzyChoquetIntegral.cpp b/src/algorithms/FuzzyChoquetIntegral.cpp
index 90dfe6eaee5f6cc8f90f44f14de280dc26ee862e..808c87460d54d93e51acf21d44e550018bfcb886 100644
--- a/src/algorithms/FuzzyChoquetIntegral.cpp
+++ b/src/algorithms/FuzzyChoquetIntegral.cpp
@@ -73,10 +73,17 @@ void FuzzyChoquetIntegral::process(const cv::Mat &img_input, cv::Mat &img_output
     cv::Mat img_background_f1;
     cv::cvtColor(img_background_f3, img_background_f1, CV_BGR2GRAY);
 
-    IplImage* input_f3 = new IplImage(img_input_f3);
-    IplImage* input_f1 = new IplImage(img_input_f1);
-    IplImage* background_f3 = new IplImage(img_background_f3);
-    IplImage* background_f1 = new IplImage(img_background_f1);
+    IplImage _input_f3 = cvIplImage(img_input_f3);
+    IplImage* input_f3 = cvCloneImage(&(IplImage)_input_f3);
+
+    IplImage _input_f1 = cvIplImage(img_input_f1);
+    IplImage* input_f1 = cvCloneImage(&(IplImage)_input_f1);
+
+    IplImage _background_f3 = cvIplImage(img_background_f3);
+    IplImage* background_f3 = cvCloneImage(&(IplImage)_background_f3);
+
+    IplImage _background_f1 = cvIplImage(img_background_f1);
+    IplImage* background_f1 = cvCloneImage(&(IplImage)_background_f1);
 
     IplImage* lbp_input_f1 = cvCreateImage(cvSize(input_f1->width, input_f1->height), IPL_DEPTH_32F, 1);
     cvSetZero(lbp_input_f1);
@@ -151,11 +158,6 @@ void FuzzyChoquetIntegral::process(const cv::Mat &img_input, cv::Mat &img_output
     cvReleaseImage(&sim_color_f3);
     cvReleaseImage(&integral_choquet_f1);
     cvReleaseImage(&updated_background_f3);
-
-    delete background_f1;
-    delete background_f3;
-    delete input_f1;
-    delete input_f3;
   }
 
   firstTime = false;
diff --git a/src/algorithms/FuzzySugenoIntegral.cpp b/src/algorithms/FuzzySugenoIntegral.cpp
index 5ac009af330383cec02cedacf156c84708949908..6043d985ddfb4aab7a954746b6c068aec127f195 100644
--- a/src/algorithms/FuzzySugenoIntegral.cpp
+++ b/src/algorithms/FuzzySugenoIntegral.cpp
@@ -72,10 +72,17 @@ void FuzzySugenoIntegral::process(const cv::Mat &img_input, cv::Mat &img_output,
     cv::Mat img_background_f1;
     cv::cvtColor(img_background_f3, img_background_f1, CV_BGR2GRAY);
 
-    IplImage* input_f3 = new IplImage(img_input_f3);
-    IplImage* input_f1 = new IplImage(img_input_f1);
-    IplImage* background_f3 = new IplImage(img_background_f3);
-    IplImage* background_f1 = new IplImage(img_background_f1);
+    IplImage _input_f3 = cvIplImage(img_input_f3);
+    IplImage* input_f3 = cvCloneImage(&(IplImage)_input_f3);
+
+    IplImage _input_f1 = cvIplImage(img_input_f1);
+    IplImage* input_f1 = cvCloneImage(&(IplImage)_input_f1);
+
+    IplImage _background_f3 = cvIplImage(img_background_f3);
+    IplImage* background_f3 = cvCloneImage(&(IplImage)_background_f3);
+
+    IplImage _background_f1 = cvIplImage(img_background_f1);
+    IplImage* background_f1 = cvCloneImage(&(IplImage)_background_f1);
 
     IplImage* lbp_input_f1 = cvCreateImage(cvSize(input_f1->width, input_f1->height), IPL_DEPTH_32F, 1);
     cvSetZero(lbp_input_f1);
@@ -150,11 +157,6 @@ void FuzzySugenoIntegral::process(const cv::Mat &img_input, cv::Mat &img_output,
     cvReleaseImage(&sim_color_f3);
     cvReleaseImage(&integral_sugeno_f1);
     cvReleaseImage(&updated_background_f3);
-
-    delete background_f1;
-    delete background_f3;
-    delete input_f1;
-    delete input_f3;
   }
 
   firstTime = false;
diff --git a/src/algorithms/IMBS/IMBS.cpp b/src/algorithms/IMBS/IMBS.cpp
index bab74da8deb60b83f84b49a64d6869c085ca45c5..e8081760ae0362c29e27be6b9d5fd4e41a98f287 100644
--- a/src/algorithms/IMBS/IMBS.cpp
+++ b/src/algorithms/IMBS/IMBS.cpp
@@ -9,7 +9,7 @@ BackgroundSubtractorIMBS::BackgroundSubtractorIMBS()
   fps = 0.;
   fgThreshold = 15;
   associationThreshold = 5;
-  samplingPeriod = 250;//500.ms
+  samplingPeriod = 50;//500.ms
   minBinHeight = 2;
   numSamples = 10; //30
   alpha = 0.65f;
@@ -410,7 +410,7 @@ void BackgroundSubtractorIMBS::createBg(unsigned int bg_sample_number) {
 
     bg_reset = false;
     if (sudden_change) {
-      numSamples *= 3.;
+      numSamples *= 2.;
       samplingPeriod *= 2.;
       sudden_change = false;
     }
@@ -699,7 +699,7 @@ void BackgroundSubtractorIMBS::changeBg() {
   //cout << "qua" << endl;
 
   if (!bg_reset) {
-    numSamples /= 3.;
+    numSamples /= 2.;
     samplingPeriod /= 2.;
     bg_frame_counter = 0;
     bg_reset = true;
diff --git a/src/algorithms/IMBS/IMBS.hpp b/src/algorithms/IMBS/IMBS.hpp
index d17f05164b88d36148c04cd7a8df77f736d0a8fb..3f86660c73fadf8a22e5948634fd5cc0b01ca86a 100644
--- a/src/algorithms/IMBS/IMBS.hpp
+++ b/src/algorithms/IMBS/IMBS.hpp
@@ -29,9 +29,9 @@ namespace bgslibrary
         BackgroundSubtractorIMBS(double fps,
           unsigned int fgThreshold = 15,
           unsigned int associationThreshold = 5,
-          double samplingPeriod = 500.,
+          double samplingPeriod = 50.,
           unsigned int minBinHeight = 2,
-          unsigned int numSamples = 30,
+          unsigned int numSamples = 10,
           double alpha = 0.65,
           double beta = 1.15,
           double tau_s = 60.,
diff --git a/src/algorithms/KNN.cpp b/src/algorithms/KNN.cpp
index 332709498a65d80b7beba13e0d93999ae8d55459..33f5cd091293c9f544ce1e16f9987b638ace0ab6 100644
--- a/src/algorithms/KNN.cpp
+++ b/src/algorithms/KNN.cpp
@@ -6,9 +6,7 @@ using namespace bgslibrary::algorithms;
 
 KNN::KNN() :
   IBGS(quote(KNN)),
-  history(500), nSamples(7), dist2Threshold(20.0f * 20.0f),
-  knnSamples(0), doShadowDetection(true), shadowValue(127),
-  shadowThreshold(0.5f)
+  history(10), dist2Threshold(20.0f * 20.0f), shadowThreshold(0.5f)
 {
   debug_construction(KNN);
   initLoadSaveConfig(algorithmName);
@@ -35,16 +33,15 @@ void KNN::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img_bg
   //  Fast for small foreground object. Results on the benchmark data is at http://www.changedetection.net.
   //------------------------------------------------------------------
 
-  int prevNSamples = nSamples;
   if (firstTime)
-    knn = cv::createBackgroundSubtractorKNN(history, dist2Threshold, doShadowDetection);
+    knn = cv::createBackgroundSubtractorKNN(); // history, dist2Threshold, doShadowDetection
 
-  knn->setNSamples(nSamples);
-  knn->setkNNSamples(knnSamples);
-  knn->setShadowValue(shadowValue);
+  knn->setHistory(history);
+  knn->setDist2Threshold(dist2Threshold);
   knn->setShadowThreshold(shadowThreshold);
 
-  knn->apply(img_input, img_foreground, prevNSamples != nSamples ? 0.f : 1.f);
+  //knn->apply(img_input, img_foreground, nSamples >= knnSamples ? 0.f : 1.f);
+  knn->apply(img_input, img_foreground);
   knn->getBackgroundImage(img_background);
 
 #ifndef MEX_COMPILE_FLAG
@@ -62,22 +59,14 @@ void KNN::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img_bg
 
 void KNN::save_config(cv::FileStorage &fs) {
   fs << "history" << history;
-  fs << "nSamples" << nSamples;
   fs << "dist2Threshold" << dist2Threshold;
-  fs << "knnSamples" << knnSamples;
-  fs << "doShadowDetection" << doShadowDetection;
-  fs << "shadowValue" << shadowValue;
   fs << "shadowThreshold" << shadowThreshold;
   fs << "showOutput" << showOutput;
 }
 
 void KNN::load_config(cv::FileStorage &fs) {
   fs["history"] >> history;
-  fs["nSamples"] >> nSamples;
   fs["dist2Threshold"] >> dist2Threshold;
-  fs["knnSamples"] >> knnSamples;
-  fs["doShadowDetection"] >> doShadowDetection;
-  fs["shadowValue"] >> shadowValue;
   fs["shadowThreshold"] >> shadowThreshold;
   fs["showOutput"] >> showOutput;
 }
diff --git a/src/algorithms/KNN.h b/src/algorithms/KNN.h
index 6950472604ee43f7b9ec238f8579a26a3340231a..4bc986abc429430cf81c52dda5cf250d27ffdfdc 100644
--- a/src/algorithms/KNN.h
+++ b/src/algorithms/KNN.h
@@ -19,11 +19,7 @@ namespace bgslibrary
     private:
       cv::Ptr<cv::BackgroundSubtractorKNN> knn;
       int history;
-      int nSamples;
       float dist2Threshold;
-      int knnSamples;
-      bool doShadowDetection;
-      int shadowValue;
       float shadowThreshold;
 
     public:
diff --git a/src/algorithms/LBAdaptiveSOM.cpp b/src/algorithms/LBAdaptiveSOM.cpp
index b051ed54e035be32f5ce9d84cc68acb44fb72411..76efbc1343f884d1b5e16cebf2120c29d6950a63 100644
--- a/src/algorithms/LBAdaptiveSOM.cpp
+++ b/src/algorithms/LBAdaptiveSOM.cpp
@@ -23,11 +23,12 @@ void LBAdaptiveSOM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::M
 {
   init(img_input, img_output, img_bgmodel);
 
-  IplImage *frame = new IplImage(img_input);
+  IplImage _frame = cvIplImage(img_input);
+  IplImage* frame = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
-    int w = cvGetSize(frame).width;
-    int h = cvGetSize(frame).height;
+    int w = img_input.size().width;
+    int h = img_input.size().height;
 
     m_pBGModel = new lb::BGModelSom(w, h);
     m_pBGModel->InitModel(frame);
@@ -54,8 +55,6 @@ void LBAdaptiveSOM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::M
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
-
   firstTime = false;
 }
 
diff --git a/src/algorithms/LBFuzzyAdaptiveSOM.cpp b/src/algorithms/LBFuzzyAdaptiveSOM.cpp
index a4feed6d131ed665184b36bc64ca5dc2aeac9544..8fb0ccb81e1bceebc566b4b6ecfd325d669d56ee 100644
--- a/src/algorithms/LBFuzzyAdaptiveSOM.cpp
+++ b/src/algorithms/LBFuzzyAdaptiveSOM.cpp
@@ -22,11 +22,12 @@ void LBFuzzyAdaptiveSOM::process(const cv::Mat &img_input, cv::Mat &img_output,
 {
   init(img_input, img_output, img_bgmodel);
 
-  IplImage *frame = new IplImage(img_input);
+  IplImage _frame = cvIplImage(img_input);
+  IplImage* frame = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
-    int w = cvGetSize(frame).width;
-    int h = cvGetSize(frame).height;
+    int w = img_input.size().width;
+    int h = img_input.size().height;
 
     m_pBGModel = new lb::BGModelFuzzySom(w, h);
     m_pBGModel->InitModel(frame);
@@ -53,8 +54,6 @@ void LBFuzzyAdaptiveSOM::process(const cv::Mat &img_input, cv::Mat &img_output,
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
-
   firstTime = false;
 }
 
diff --git a/src/algorithms/LBFuzzyGaussian.cpp b/src/algorithms/LBFuzzyGaussian.cpp
index c501848815ad4b6ae9a717d2658e37d3785c8dc4..e175d590d0410b12babc18f8f969952c93b93856 100644
--- a/src/algorithms/LBFuzzyGaussian.cpp
+++ b/src/algorithms/LBFuzzyGaussian.cpp
@@ -22,11 +22,12 @@ void LBFuzzyGaussian::process(const cv::Mat &img_input, cv::Mat &img_output, cv:
 {
   init(img_input, img_output, img_bgmodel);
 
-  IplImage *frame = new IplImage(img_input);
+  IplImage _frame = cvIplImage(img_input);
+  IplImage* frame = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
-    int w = cvGetSize(frame).width;
-    int h = cvGetSize(frame).height;
+    int w = img_input.size().width;
+    int h = img_input.size().height;
 
     m_pBGModel = new lb::BGModelFuzzyGauss(w, h);
     m_pBGModel->InitModel(frame);
@@ -52,8 +53,6 @@ void LBFuzzyGaussian::process(const cv::Mat &img_input, cv::Mat &img_output, cv:
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
-
   firstTime = false;
 }
 
diff --git a/src/algorithms/LBMixtureOfGaussians.cpp b/src/algorithms/LBMixtureOfGaussians.cpp
index 219cd1b9f5dabcb370abfa618b3f3dc78430c4e4..d949adb5dd633a668800d8b80a793f0c73e9c87b 100644
--- a/src/algorithms/LBMixtureOfGaussians.cpp
+++ b/src/algorithms/LBMixtureOfGaussians.cpp
@@ -22,11 +22,12 @@ void LBMixtureOfGaussians::process(const cv::Mat &img_input, cv::Mat &img_output
 {
   init(img_input, img_output, img_bgmodel);
 
-  IplImage *frame = new IplImage(img_input);
+  IplImage _frame = cvIplImage(img_input);
+  IplImage* frame = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
-    int w = cvGetSize(frame).width;
-    int h = cvGetSize(frame).height;
+    int w = img_input.size().width;
+    int h = img_input.size().height;
 
     m_pBGModel = new lb::BGModelMog(w, h);
     m_pBGModel->InitModel(frame);
@@ -52,8 +53,6 @@ void LBMixtureOfGaussians::process(const cv::Mat &img_input, cv::Mat &img_output
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
-
   firstTime = false;
 }
 
diff --git a/src/algorithms/LBSimpleGaussian.cpp b/src/algorithms/LBSimpleGaussian.cpp
index 6586833be501a0bb2e6fb557c48dae89dd876eec..67641198de2c341877da30c3dfccecfb5cb98c75 100644
--- a/src/algorithms/LBSimpleGaussian.cpp
+++ b/src/algorithms/LBSimpleGaussian.cpp
@@ -21,11 +21,12 @@ void LBSimpleGaussian::process(const cv::Mat &img_input, cv::Mat &img_output, cv
 {
   init(img_input, img_output, img_bgmodel);
 
-  IplImage *frame = new IplImage(img_input);
+  IplImage _frame = cvIplImage(img_input);
+  IplImage* frame = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
-    int w = cvGetSize(frame).width;
-    int h = cvGetSize(frame).height;
+    int w = img_input.size().width;
+    int h = img_input.size().height;
 
     m_pBGModel = new lb::BGModelGauss(w, h);
     m_pBGModel->InitModel(frame);
@@ -50,8 +51,6 @@ void LBSimpleGaussian::process(const cv::Mat &img_input, cv::Mat &img_output, cv
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
-
   firstTime = false;
 }
 
diff --git a/src/algorithms/MultiCue.cpp b/src/algorithms/MultiCue.cpp
index 72fbab1a8904b5c5f8b1ed9cefab613c1a7fea16..8f7e0acd6bdb9c328213e4430d5a0544ec50b4a6 100644
--- a/src/algorithms/MultiCue.cpp
+++ b/src/algorithms/MultiCue.cpp
@@ -73,26 +73,28 @@ void MultiCue::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &i
 {
   init(img_input, img_output, img_bgmodel);
 
-  //--STep1: Background Modeling--//
-  //IplImage* frame = &IplImage(img_input);
-  IplImage* frame = new IplImage(img_input);
-  IplImage* result_image = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
+  // Step 1: Background Modeling
+  IplImage _frame = cvIplImage(img_input);
+  IplImage* frame = cvCloneImage(&(IplImage)_frame);
+
+  int width = img_input.size().width;
+  int height = img_input.size().height;
+  IplImage* result_image = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
   cvSetZero(result_image);
   if (g_iFrameCount <= g_iTrainingPeriod) {
     BackgroundModeling_Par(frame);
     g_iFrameCount++;
   }
-  //--Step2: Background Subtraction--//
+  // Step 2: Background Subtraction
   else {
     g_bForegroundMapEnable = FALSE;
 
     ForegroundExtraction(frame);
     UpdateModel_Par();
 
-    //Get BGS Results
+    // Get BGS Results
     GetForegroundMap(result_image, NULL);
   }
-  delete frame;
 
   img_background = cv::Mat::zeros(img_input.size(), img_input.type());
   img_foreground = cv::cvarrToMat(result_image, TRUE);
@@ -537,8 +539,9 @@ void MultiCue::GaussianFiltering(IplImage* frame, uchar*** aFilteredFrame) {
     cv::GaussianBlur(temp_img, temp_img, cv::Size(7, 7), dSigma);
 
     //Store results into aFilteredFrame[][][]
-    //IplImage* img = &IplImage(temp_img);
-    IplImage* img = new IplImage(temp_img);
+    IplImage _img = cvIplImage(temp_img);
+    IplImage* img = cvCloneImage(&(IplImage)_img);
+
     //int iWidthStep = img->widthStep;
 
     for (int i = 0; i < g_iRHeight; i++) {
@@ -548,7 +551,6 @@ void MultiCue::GaussianFiltering(IplImage* frame, uchar*** aFilteredFrame) {
         aFilteredFrame[i][j][2] = img->imageData[i*img->widthStep + j * 3 + 2];
       }
     }
-    delete img;
   }
 }
 
diff --git a/src/algorithms/MultiLayer.cpp b/src/algorithms/MultiLayer.cpp
index 1914785815ed7780af4925ab6fcd40f045a8b4c4..49ad0c5ba2208680db6078fce16c59ab7078287b 100644
--- a/src/algorithms/MultiLayer.cpp
+++ b/src/algorithms/MultiLayer.cpp
@@ -57,8 +57,9 @@ void MultiLayer::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat
 
     if (status == MLBGS_DETECT)
       std::cout << algorithmName + " in DETECT mode" << std::endl;
-
-    org_img = new IplImage(img_input);
+    
+    IplImage _frame = cvIplImage(img_input);
+    org_img = cvCloneImage(&(IplImage)_frame);
 
     fg_img = cvCreateImage(img_size, org_img->depth, org_img->nChannels);
     bg_img = cvCreateImage(img_size, org_img->depth, org_img->nChannels);
@@ -156,7 +157,6 @@ void MultiLayer::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat
     }
 
     BGS->SetParameters(max_mode_num, mode_learn_rate_per_second, weight_learn_rate_per_second, init_mode_weight);
-    delete org_img;
   }
 
   //IplImage* inputImage = new IplImage(img_input);
@@ -181,7 +181,8 @@ void MultiLayer::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat
       std::cout << algorithmName + " enabled learning in DETECT mode" << std::endl;
   }
 
-  IplImage* img = new IplImage(img_input);
+  IplImage _frame = cvIplImage(img_input);
+  IplImage* img = cvCloneImage(&(IplImage)_frame);
 
   BGS->SetRGBInputImage(img);
   BGS->Process();
@@ -206,7 +207,6 @@ void MultiLayer::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete img;
   //cvReleaseImage(&img);
 
   firstTime = false;
diff --git a/src/algorithms/MultiLayer/CMultiLayerBGS.cpp b/src/algorithms/MultiLayer/CMultiLayerBGS.cpp
index 481f41662fbbc02a8b63ac82ca57b62a0dadd91e..b2952163223061e3bb74ebec6a8ee0550db43804 100644
--- a/src/algorithms/MultiLayer/CMultiLayerBGS.cpp
+++ b/src/algorithms/MultiLayer/CMultiLayerBGS.cpp
@@ -167,7 +167,7 @@ void CMultiLayerBGS::MergeImages(int num, ...) {
   va_end(arg_ptr);
 
   CvRect imgROIRect;
-  CvSize imgSize = cvGetSize(ppIplImg[0]);
+  CvSize imgSize = cvSize(ppIplImg[0]->width, ppIplImg[0]->height);
   if (ppIplImg[num] == NULL) { // for the output video
     ppIplImg[num] = cvCreateImage(cvSize(imgSize.width*nCols, imgSize.height * nRows), IPL_DEPTH_8U, ppIplImg[0]->nChannels);
   }
@@ -321,7 +321,7 @@ void CMultiLayerBGS::SetNewImage(IplImage *new_img, CvRect *roi) {
 
 void CMultiLayerBGS::SetBkMaskImage(IplImage *mask_img) {
   if (m_pBkMaskImg == NULL) {
-    m_pBkMaskImg = cvCreateImage(cvGetSize(mask_img), mask_img->depth, mask_img->nChannels);
+    m_pBkMaskImg = cvCreateImage(cvSize(mask_img->width, mask_img->height), mask_img->depth, mask_img->nChannels);
   }
   cvCopy(mask_img, m_pBkMaskImg);
 }
@@ -859,7 +859,7 @@ void CMultiLayerBGS::Initialization(IplImage *first_img, int lbp_level_num, floa
   m_ppOrgLBPImgs = NULL;
   m_pFgProbImg = NULL;
 
-  m_cvImgSize = cvGetSize(first_img);
+  m_cvImgSize = cvSize(first_img->width, first_img->height);
 
   m_nChannel = first_img->nChannels;
 
@@ -962,9 +962,8 @@ void CMultiLayerBGS::ComputeGradientImage(IplImage *src, IplImage *dst, bool bIs
   }
 
   int a;
-
-  IplImage* _dX = cvCreateImage(cvGetSize(dst), IPL_DEPTH_16S, 1);
-  IplImage* _dY = cvCreateImage(cvGetSize(dst), IPL_DEPTH_16S, 1);
+  IplImage* _dX = cvCreateImage(cvSize(dst->width, dst->height), IPL_DEPTH_16S, 1);
+  IplImage* _dY = cvCreateImage(cvSize(dst->width, dst->height), IPL_DEPTH_16S, 1);
 
   int aperture_size = 3;
 
@@ -1284,10 +1283,10 @@ void CMultiLayerBGS::GetCurrentLayeredBackgroundImage(int layered_no, IplImage *
   }
 
   COpencvDataConversion<float, float> ODC;
-  IplImage* bg_layer_float_mask_img = cvCreateImage(cvGetSize(m_pOrgImg), IPL_DEPTH_32F, 1);
-  IplImage* bg_layer_low_mask_img = cvCreateImage(cvGetSize(m_pOrgImg), IPL_DEPTH_8U, 1);
-  IplImage* bg_layer_high_mask_img = cvCreateImage(cvGetSize(m_pOrgImg), IPL_DEPTH_8U, 1);
-  IplImage* bg_layer_mask_img = cvCreateImage(cvGetSize(m_pOrgImg), IPL_DEPTH_8U, 1);
+  IplImage* bg_layer_float_mask_img = cvCreateImage(cvSize(m_pOrgImg->width, m_pOrgImg->height), IPL_DEPTH_32F, 1);
+  IplImage* bg_layer_low_mask_img = cvCreateImage(cvSize(m_pOrgImg->width, m_pOrgImg->height), IPL_DEPTH_8U, 1);
+  IplImage* bg_layer_high_mask_img = cvCreateImage(cvSize(m_pOrgImg->width, m_pOrgImg->height), IPL_DEPTH_8U, 1);
+  IplImage* bg_layer_mask_img = cvCreateImage(cvSize(m_pOrgImg->width, m_pOrgImg->height), IPL_DEPTH_8U, 1);
 
   ODC.SetImageData(bg_layer_float_mask_img, bg_layer_mask);
 
diff --git a/src/algorithms/MultiLayer/LocalBinaryPattern.cpp b/src/algorithms/MultiLayer/LocalBinaryPattern.cpp
index 89e53381c45815197e395aa992f37446c1d70258..120e01d49e7bd0d70064350838326f7d3b9a8378 100644
--- a/src/algorithms/MultiLayer/LocalBinaryPattern.cpp
+++ b/src/algorithms/MultiLayer/LocalBinaryPattern.cpp
@@ -27,7 +27,7 @@ void CLocalBinaryPattern::Initialization(IplImage **first_imgs, int imgs_num, in
   m_ppOrgImgs = first_imgs;
   int a, b;
   for (a = 0; a < m_nImgsNum; a++) {
-    m_cvImgSize = cvGetSize(first_imgs[a]);
+    m_cvImgSize = cvSize(first_imgs[a]->width, first_imgs[a]->height);
     if (first_imgs[a]->nChannels > 1) {
       printf("Input image channel must be 1!");
       exit(1);
diff --git a/src/algorithms/T2FGMM_UM.cpp b/src/algorithms/T2FGMM_UM.cpp
index 7c95dfe58cab47bd1f9f2c36c50bc6776bb62fa2..a3bff750f7902dc7207031c1a38bf4ec335f9891 100644
--- a/src/algorithms/T2FGMM_UM.cpp
+++ b/src/algorithms/T2FGMM_UM.cpp
@@ -20,11 +20,9 @@ T2FGMM_UM::~T2FGMM_UM() {
 void T2FGMM_UM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img_bgmodel)
 {
   init(img_input, img_output, img_bgmodel);
-  frame = new IplImage(img_input);
 
-  if (firstTime)
-    frame_data.ReleaseMemory(false);
-  frame_data = frame;
+  IplImage _frame = cvIplImage(img_input);
+  frame_data = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
     int width = img_input.size().width;
@@ -65,7 +63,6 @@ void T2FGMM_UM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
   firstTime = false;
   frameNumber++;
 }
diff --git a/src/algorithms/T2FGMM_UV.cpp b/src/algorithms/T2FGMM_UV.cpp
index cff817bd82f0266bedea0cb809edbcbdc1a21084..c366f42cb4b3011e34a37af57ea6bd47d1b94c7f 100644
--- a/src/algorithms/T2FGMM_UV.cpp
+++ b/src/algorithms/T2FGMM_UV.cpp
@@ -20,11 +20,9 @@ T2FGMM_UV::~T2FGMM_UV() {
 void T2FGMM_UV::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img_bgmodel)
 {
   init(img_input, img_output, img_bgmodel);
-  frame = new IplImage(img_input);
 
-  if (firstTime)
-    frame_data.ReleaseMemory(false);
-  frame_data = frame;
+  IplImage _frame = cvIplImage(img_input);
+  frame_data = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
     int width = img_input.size().width;
@@ -65,7 +63,6 @@ void T2FGMM_UV::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
   firstTime = false;
   frameNumber++;
 }
diff --git a/src/algorithms/T2FMRF_UM.cpp b/src/algorithms/T2FMRF_UM.cpp
index 208be5d63d5fa7199e79885fd76fd2a44d6fac40..207ceab46491b7f6c5a577d83cb0df40d8f5fa8e 100644
--- a/src/algorithms/T2FMRF_UM.cpp
+++ b/src/algorithms/T2FMRF_UM.cpp
@@ -20,11 +20,9 @@ T2FMRF_UM::~T2FMRF_UM() {
 void T2FMRF_UM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img_bgmodel)
 {
   init(img_input, img_output, img_bgmodel);
-  frame = new IplImage(img_input);
 
-  if (firstTime)
-    frame_data.ReleaseMemory(false);
-  frame_data = frame;
+  IplImage _frame = cvIplImage(img_input);
+  frame_data = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
     int width = img_input.size().width;
@@ -94,7 +92,6 @@ void T2FMRF_UM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
   frameNumber++;
 }
 
diff --git a/src/algorithms/T2FMRF_UV.cpp b/src/algorithms/T2FMRF_UV.cpp
index c0a1c5ff65c2f13f3427f44178c43bfdb807934d..ddfb9d75d4e38c86a4e8a4c4fd2d5eb667c4d2a1 100644
--- a/src/algorithms/T2FMRF_UV.cpp
+++ b/src/algorithms/T2FMRF_UV.cpp
@@ -20,11 +20,9 @@ T2FMRF_UV::~T2FMRF_UV() {
 void T2FMRF_UV::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img_bgmodel)
 {
   init(img_input, img_output, img_bgmodel);
-  frame = new IplImage(img_input);
 
-  if (firstTime)
-    frame_data.ReleaseMemory(false);
-  frame_data = frame;
+  IplImage _frame = cvIplImage(img_input);
+  frame_data = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
     int width = img_input.size().width;
@@ -94,7 +92,6 @@ void T2FMRF_UV::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
   frameNumber++;
 }
 
diff --git a/src/algorithms/VuMeter.cpp b/src/algorithms/VuMeter.cpp
index c83d6b8f1f0046287f3403a8efacfb4459cb8f4a..bd84edf501f7c9e95267c1343f860034c8843e56 100644
--- a/src/algorithms/VuMeter.cpp
+++ b/src/algorithms/VuMeter.cpp
@@ -23,20 +23,25 @@ VuMeter::~VuMeter() {
 void VuMeter::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img_bgmodel)
 {
   init(img_input, img_output, img_bgmodel);
-  frame = new IplImage(img_input);
+
+  IplImage _frame = cvIplImage(img_input);
+  frame = cvCloneImage(&(IplImage)_frame);
 
   if (firstTime) {
+    int w = img_input.size().width;
+    int h = img_input.size().height;
+
     bgs.SetAlpha(alpha);
     bgs.SetBinSize(binSize);
     bgs.SetThreshold(threshold);
 
-    gray = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
+    gray = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);
     cvCvtColor(frame, gray, CV_RGB2GRAY);
 
-    background = cvCreateImage(cvGetSize(gray), IPL_DEPTH_8U, 1);
+    background = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);
     cvCopy(gray, background);
 
-    mask = cvCreateImage(cvGetSize(gray), IPL_DEPTH_8U, 1);
+    mask = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);
     cvZero(mask);
   }
   else
@@ -61,7 +66,6 @@ void VuMeter::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &im
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
 
-  delete frame;
   firstTime = false;
 }