diff --git a/src/algorithms/T2F/MRF.cpp b/src/algorithms/T2F/MRF.cpp
index 2d6d18b4f5c306de93922582c39bbf9a6daf6d69..85f3529c4999528424626cae4fad02c91ca5367f 100644
--- a/src/algorithms/T2F/MRF.cpp
+++ b/src/algorithms/T2F/MRF.cpp
@@ -253,6 +253,7 @@ void MRF_TC::InitEvidence2(GMM *gmm, HMM *hmm, IplImage *labeling)
       }
     }
   }
+  background.ReleaseImage();
 }
 
 void MRF_TC::CreateOutput2()
diff --git a/src/algorithms/T2FGMM_UM.cpp b/src/algorithms/T2FGMM_UM.cpp
index c04282fe6e1e6209eb6a6fab940cc3d84eac5297..ab253144a2ac067ad380804d42e794000c486c4e 100644
--- a/src/algorithms/T2FGMM_UM.cpp
+++ b/src/algorithms/T2FGMM_UM.cpp
@@ -62,6 +62,7 @@ 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);
+  frame_data.ReleaseImage();
 
   firstTime = false;
   frameNumber++;
diff --git a/src/algorithms/T2FGMM_UV.cpp b/src/algorithms/T2FGMM_UV.cpp
index c27baccf67d92a16a0d44344d5eb2b724aba7973..8552c5dad23687916c2f63022f1be206cb2ac376 100644
--- a/src/algorithms/T2FGMM_UV.cpp
+++ b/src/algorithms/T2FGMM_UV.cpp
@@ -62,6 +62,7 @@ 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);
+  frame_data.ReleaseImage();
 
   firstTime = false;
   frameNumber++;
diff --git a/src/algorithms/T2FMRF_UM.cpp b/src/algorithms/T2FMRF_UM.cpp
index 062e312028c750b671c34a3147105013e12eebd1..d78758233c87aaf930b237f0f502ed96816a2350 100644
--- a/src/algorithms/T2FMRF_UM.cpp
+++ b/src/algorithms/T2FMRF_UM.cpp
@@ -25,14 +25,8 @@ void T2FMRF_UM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &
   frame_data = cvCloneImage(&_frame);
 
   if (firstTime) {
-    int width = img_input.size().width;
-    int height = img_input.size().height;
-
-    lowThresholdMask = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
-    lowThresholdMask.Ptr()->origin = IPL_ORIGIN_BL;
-
-    highThresholdMask = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
-    highThresholdMask.Ptr()->origin = IPL_ORIGIN_BL;
+    width = img_input.size().width;
+    height = img_input.size().height;
 
     params.SetFrameSize(width, height);
     params.LowThreshold() = threshold;
@@ -46,16 +40,25 @@ void T2FMRF_UM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &
     bgs.Initalize(params);
     bgs.InitModel(frame_data);
 
-    old_labeling = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
-    old = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
-
     mrf.height = height;
     mrf.width = width;
     mrf.Build_Classes_OldLabeling_InImage_LocalEnergy();
 
+    old_labeling = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
+    old = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
+
+    gmm = bgs.gmm();
+    hmm = bgs.hmm();
+
     firstTime = false;
   }
 
+  lowThresholdMask = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
+  lowThresholdMask.Ptr()->origin = IPL_ORIGIN_BL;
+
+  highThresholdMask = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
+  highThresholdMask.Ptr()->origin = IPL_ORIGIN_BL;
+
   bgs.Subtract(frameNumber, frame_data, lowThresholdMask, highThresholdMask);
   cvCopy(lowThresholdMask.Ptr(), old);
 
@@ -65,19 +68,18 @@ void T2FMRF_UM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &
   //the optimization process is done when the foreground detection is stable,
   if (frameNumber >= 10)
   {
-    gmm = bgs.gmm();
-    hmm = bgs.hmm();
     mrf.background2 = frame_data.Ptr();
     mrf.in_image = lowThresholdMask.Ptr();
     mrf.out_image = lowThresholdMask.Ptr();
     mrf.InitEvidence2(gmm, hmm, old_labeling);
+    // mrf.InitEvidence2(gmm, hmm, lowThresholdMask.Ptr());
     mrf.ICM2();
     cvCopy(mrf.out_image, lowThresholdMask.Ptr());
   }
 
   cvCopy(old, old_labeling);
 
-  lowThresholdMask.Clear();
+  // lowThresholdMask.Clear();
   bgs.Update(frameNumber, frame_data, lowThresholdMask);
 
   img_foreground = cv::cvarrToMat(highThresholdMask.Ptr());
@@ -91,6 +93,9 @@ 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);
+  frame_data.ReleaseImage();
+  lowThresholdMask.ReleaseImage();
+  highThresholdMask.ReleaseImage();
 
   frameNumber++;
 }
diff --git a/src/algorithms/T2FMRF_UM.h b/src/algorithms/T2FMRF_UM.h
index e290dea6047a1720d91ea7cc2f899a019104e30d..b2ae476432696fc9d239a34f84ef4777dd11fa7a 100644
--- a/src/algorithms/T2FMRF_UM.h
+++ b/src/algorithms/T2FMRF_UM.h
@@ -19,6 +19,8 @@ namespace bgslibrary
       float km;
       float kv;
       int gaussians;
+      int width;
+      int height;
       IplImage *old_labeling;
       IplImage *old;
       dp::RgbImage frame_data;
diff --git a/src/algorithms/T2FMRF_UV.cpp b/src/algorithms/T2FMRF_UV.cpp
index c5f90f8ff5c3b8d72f15cdab09f1abdb5b32ea98..2911dbefac5f37dd7deaf508df07d0561d7bb6f4 100644
--- a/src/algorithms/T2FMRF_UV.cpp
+++ b/src/algorithms/T2FMRF_UV.cpp
@@ -53,6 +53,9 @@ void T2FMRF_UV::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &
     mrf.width = width;
     mrf.Build_Classes_OldLabeling_InImage_LocalEnergy();
 
+    gmm = bgs.gmm();
+    hmm = bgs.hmm();
+
     firstTime = false;
   }
 
@@ -65,8 +68,6 @@ void T2FMRF_UV::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &
   //the optimization process is done when the foreground detection is stable,
   if (frameNumber >= 10)
   {
-    gmm = bgs.gmm();
-    hmm = bgs.hmm();
     mrf.background2 = frame_data.Ptr();
     mrf.in_image = lowThresholdMask.Ptr();
     mrf.out_image = lowThresholdMask.Ptr();
@@ -91,6 +92,7 @@ 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);
+  frame_data.ReleaseImage();
 
   frameNumber++;
 }