diff --git a/src/algorithms/DPAdaptiveMedian.cpp b/src/algorithms/DPAdaptiveMedian.cpp
index e121bdf3348f9b61cc699158ef83bb6cc02aebe3..d8c0bc84d993fbeda1e2cdd7f18be6beb7926941 100644
--- a/src/algorithms/DPAdaptiveMedian.cpp
+++ b/src/algorithms/DPAdaptiveMedian.cpp
@@ -21,7 +21,12 @@ void DPAdaptiveMedian::process(const cv::Mat &img_input, cv::Mat &img_output, cv
 {
   init(img_input, img_output, img_bgmodel);
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  frame = &_frame;
+#else
   frame = new IplImage(img_input);
+#endif
 
   if (firstTime)
     frame_data.ReleaseMemory(false);
diff --git a/src/algorithms/DPEigenbackground.cpp b/src/algorithms/DPEigenbackground.cpp
index dfc7d64dba4bb9f78126e2eb3b1c99925beab4ef..770cc6102e2da8324b7ccf92e7e2ad411e3cba87 100644
--- a/src/algorithms/DPEigenbackground.cpp
+++ b/src/algorithms/DPEigenbackground.cpp
@@ -21,7 +21,12 @@ void DPEigenbackground::process(const cv::Mat &img_input, cv::Mat &img_output, c
 {
   init(img_input, img_output, img_bgmodel);
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  frame = &_frame;
+#else
   frame = new IplImage(img_input);
+#endif
 
   if (firstTime)
     frame_data.ReleaseMemory(false);
diff --git a/src/algorithms/DPGrimsonGMM.cpp b/src/algorithms/DPGrimsonGMM.cpp
index 743acf19e517ea82de47e6ab10711c10b1b6405b..77e26931c1acae0e7ad93e6bbacc64b05d6bf9af 100644
--- a/src/algorithms/DPGrimsonGMM.cpp
+++ b/src/algorithms/DPGrimsonGMM.cpp
@@ -21,7 +21,12 @@ void DPGrimsonGMM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Ma
 {
   init(img_input, img_output, img_bgmodel);
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  frame = &_frame;
+#else
   frame = new IplImage(img_input);
+#endif
 
   if (firstTime)
     frame_data.ReleaseMemory(false);
diff --git a/src/algorithms/DPMean.cpp b/src/algorithms/DPMean.cpp
index 18320cd5e57b52be02c29323578b6213c21182b6..766e37914004029fcf607acf2a51c136850e72eb 100644
--- a/src/algorithms/DPMean.cpp
+++ b/src/algorithms/DPMean.cpp
@@ -21,7 +21,12 @@ void DPMean::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img
 {
   init(img_input, img_output, img_bgmodel);
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  frame = &_frame;
+#else
   frame = new IplImage(img_input);
+#endif
 
   if (firstTime)
     frame_data.ReleaseMemory(false);
diff --git a/src/algorithms/DPPratiMediod.cpp b/src/algorithms/DPPratiMediod.cpp
index 3cade2078531a25406f2eb5620614c84b8dd54c5..b53552b5e8b0d335ef7669ba534fe837b1996caf 100644
--- a/src/algorithms/DPPratiMediod.cpp
+++ b/src/algorithms/DPPratiMediod.cpp
@@ -21,7 +21,12 @@ void DPPratiMediod::process(const cv::Mat &img_input, cv::Mat &img_output, cv::M
 {
   init(img_input, img_output, img_bgmodel);
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  frame = &_frame;
+#else
   frame = new IplImage(img_input);
+#endif
 
   if (firstTime)
     frame_data.ReleaseMemory(false);
diff --git a/src/algorithms/DPTexture.cpp b/src/algorithms/DPTexture.cpp
index 7f58230711e7c098f04a245f5f2e623dfcb0d845..ba33e9f0053278120a5d9a1ce31f2db2e84cb183 100644
--- a/src/algorithms/DPTexture.cpp
+++ b/src/algorithms/DPTexture.cpp
@@ -29,7 +29,12 @@ void DPTexture::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &
 {
   init(img_input, img_output, img_bgmodel);
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  frame = &_frame;
+#else
   frame = new IplImage(img_input);
+#endif
 
   if (firstTime) {
     width = img_input.size().width;
diff --git a/src/algorithms/DPWrenGA.cpp b/src/algorithms/DPWrenGA.cpp
index a7f91288855233f028b1ad34c6246b18ab96fa23..e3f5df22e33260126363a4b5aa5c315f268fc016 100644
--- a/src/algorithms/DPWrenGA.cpp
+++ b/src/algorithms/DPWrenGA.cpp
@@ -21,7 +21,12 @@ void DPWrenGA::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &i
 {
   init(img_input, img_output, img_bgmodel);
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  frame = &_frame;
+#else
   frame = new IplImage(img_input);
+#endif
 
   if (firstTime)
     frame_data.ReleaseMemory(false);
diff --git a/src/algorithms/DPZivkovicAGMM.cpp b/src/algorithms/DPZivkovicAGMM.cpp
index 289f5cc137a173769529454817d53b793fa0388d..6fd403ca0959513b0a072e480ceccb38ad051896 100644
--- a/src/algorithms/DPZivkovicAGMM.cpp
+++ b/src/algorithms/DPZivkovicAGMM.cpp
@@ -21,7 +21,12 @@ void DPZivkovicAGMM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::
 {
   init(img_input, img_output, img_bgmodel);
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  frame = &_frame;
+#else
   frame = new IplImage(img_input);
+#endif
 
   if (firstTime)
     frame_data.ReleaseMemory(false);
diff --git a/src/algorithms/FuzzyChoquetIntegral.cpp b/src/algorithms/FuzzyChoquetIntegral.cpp
index 90dfe6eaee5f6cc8f90f44f14de280dc26ee862e..2145eaea6e428bb556289a7e1f16800a97ce09f8 100644
--- a/src/algorithms/FuzzyChoquetIntegral.cpp
+++ b/src/algorithms/FuzzyChoquetIntegral.cpp
@@ -73,10 +73,24 @@ 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);
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+    IplImage _input_f3 = cvIplImage(img_input_f3);
+    IplImage* input_f3 = &_input_f3;
+
+    IplImage _input_f1 = cvIplImage(img_input_f1);
+    IplImage* input_f1 = &_input_f1;
+
+    IplImage _background_f3 = cvIplImage(img_background_f3);
+    IplImage* background_f3 = &_background_f3;
+
+    IplImage _background_f1 = cvIplImage(img_background_f1);
+    IplImage* background_f1 = &_background_f1;
+#else
     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);
+#endif
 
     IplImage* lbp_input_f1 = cvCreateImage(cvSize(input_f1->width, input_f1->height), IPL_DEPTH_32F, 1);
     cvSetZero(lbp_input_f1);
diff --git a/src/algorithms/FuzzySugenoIntegral.cpp b/src/algorithms/FuzzySugenoIntegral.cpp
index 5ac009af330383cec02cedacf156c84708949908..1c70dc4cda71c2ee0a08bef880e500b0643f893f 100644
--- a/src/algorithms/FuzzySugenoIntegral.cpp
+++ b/src/algorithms/FuzzySugenoIntegral.cpp
@@ -72,10 +72,24 @@ 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);
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+    IplImage _input_f3 = cvIplImage(img_input_f3);
+    IplImage* input_f3 = &_input_f3;
+
+    IplImage _input_f1 = cvIplImage(img_input_f1);
+    IplImage* input_f1 = &_input_f1;
+
+    IplImage _background_f3 = cvIplImage(img_background_f3);
+    IplImage* background_f3 = &_background_f3;
+
+    IplImage _background_f1 = cvIplImage(img_background_f1);
+    IplImage* background_f1 = &_background_f1;
+#else
     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);
+#endif
 
     IplImage* lbp_input_f1 = cvCreateImage(cvSize(input_f1->width, input_f1->height), IPL_DEPTH_32F, 1);
     cvSetZero(lbp_input_f1);
diff --git a/src/algorithms/IBGS.h b/src/algorithms/IBGS.h
index bbf835fa24b9289738c76ff6b957cfbf20cedd75..d92b5df2b61b5eb6b7a953cfd3e861efac31887d 100644
--- a/src/algorithms/IBGS.h
+++ b/src/algorithms/IBGS.h
@@ -19,7 +19,7 @@
 
 #include "../utils/ILoadSaveConfig.h"
 
-#ifndef CV_RGB
+#ifndef CV_RGB && CV_MAJOR_VERSION > 3
 #define CV_RGB(r, g, b) cv::Scalar((b), (g), (r), 0)
 #endif
 
diff --git a/src/algorithms/LBAdaptiveSOM.cpp b/src/algorithms/LBAdaptiveSOM.cpp
index b051ed54e035be32f5ce9d84cc68acb44fb72411..4c0303571468a742b88f4c1164502c0e16fab64e 100644
--- a/src/algorithms/LBAdaptiveSOM.cpp
+++ b/src/algorithms/LBAdaptiveSOM.cpp
@@ -23,8 +23,12 @@ void LBAdaptiveSOM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::M
 {
   init(img_input, img_output, img_bgmodel);
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  IplImage *frame = &_frame;
+#else
   IplImage *frame = new IplImage(img_input);
-
+#endif
   if (firstTime) {
     int w = cvGetSize(frame).width;
     int h = cvGetSize(frame).height;
diff --git a/src/algorithms/LBFuzzyAdaptiveSOM.cpp b/src/algorithms/LBFuzzyAdaptiveSOM.cpp
index a4feed6d131ed665184b36bc64ca5dc2aeac9544..a88b5780e4d2c03cbea7d52be5ec384480ac8ff6 100644
--- a/src/algorithms/LBFuzzyAdaptiveSOM.cpp
+++ b/src/algorithms/LBFuzzyAdaptiveSOM.cpp
@@ -22,7 +22,12 @@ void LBFuzzyAdaptiveSOM::process(const cv::Mat &img_input, cv::Mat &img_output,
 {
   init(img_input, img_output, img_bgmodel);
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  IplImage *frame = &_frame;
+#else
   IplImage *frame = new IplImage(img_input);
+#endif
 
   if (firstTime) {
     int w = cvGetSize(frame).width;
diff --git a/src/algorithms/LBFuzzyGaussian.cpp b/src/algorithms/LBFuzzyGaussian.cpp
index c501848815ad4b6ae9a717d2658e37d3785c8dc4..31e3232d795b14573c73a2259446bea894ef31f4 100644
--- a/src/algorithms/LBFuzzyGaussian.cpp
+++ b/src/algorithms/LBFuzzyGaussian.cpp
@@ -22,8 +22,12 @@ void LBFuzzyGaussian::process(const cv::Mat &img_input, cv::Mat &img_output, cv:
 {
   init(img_input, img_output, img_bgmodel);
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  IplImage *frame = &_frame;
+#else
   IplImage *frame = new IplImage(img_input);
-
+#endif
   if (firstTime) {
     int w = cvGetSize(frame).width;
     int h = cvGetSize(frame).height;
diff --git a/src/algorithms/LBMixtureOfGaussians.cpp b/src/algorithms/LBMixtureOfGaussians.cpp
index 219cd1b9f5dabcb370abfa618b3f3dc78430c4e4..449b99c71fdb00fc45a5786dbe2a1cac2c474242 100644
--- a/src/algorithms/LBMixtureOfGaussians.cpp
+++ b/src/algorithms/LBMixtureOfGaussians.cpp
@@ -22,8 +22,12 @@ void LBMixtureOfGaussians::process(const cv::Mat &img_input, cv::Mat &img_output
 {
   init(img_input, img_output, img_bgmodel);
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  IplImage *frame = &_frame;
+#else
   IplImage *frame = new IplImage(img_input);
-
+#endif
   if (firstTime) {
     int w = cvGetSize(frame).width;
     int h = cvGetSize(frame).height;
diff --git a/src/algorithms/LBP_MRF.cpp b/src/algorithms/LBP_MRF.cpp
index e0d4c64977688cccf415075cb1f0c2ca27682b95..c0f31de1bdc87791a299d9dc69e44e8394653480 100644
--- a/src/algorithms/LBP_MRF.cpp
+++ b/src/algorithms/LBP_MRF.cpp
@@ -24,7 +24,12 @@ void LBP_MRF::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &im
 {
   init(img_input, img_output, img_bgmodel);
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage TempImage = cvIplImage(img_input);
+#else
   IplImage TempImage(img_input);
+#endif
+
   lbp_mrf::MEImage InputImage(img_input.cols, img_input.rows, img_input.channels());
   lbp_mrf::MEImage OutputImage(img_input.cols, img_input.rows, img_input.channels());
 
diff --git a/src/algorithms/LBP_MRF/MEImage.cpp b/src/algorithms/LBP_MRF/MEImage.cpp
index 7899b5220cff399ea07dc345c11d560a613f6142..3103e73f594e48d2c0f99153f7af2f0e80526ae9 100644
--- a/src/algorithms/LBP_MRF/MEImage.cpp
+++ b/src/algorithms/LBP_MRF/MEImage.cpp
@@ -1,12 +1,13 @@
-#include <opencv2/opencv.hpp>
-// opencv legacy includes
-#include <opencv2/imgproc/types_c.h>
-#include <opencv2/imgproc/imgproc_c.h>
+#include "opencv2/core/version.hpp"
+#if CV_MAJOR_VERSION >= 2 && CV_MAJOR_VERSION <= 3
 
 #include "MEImage.hpp"
 #include "MEDefs.hpp"
 
-#if CV_MAJOR_VERSION >= 2 && CV_MAJOR_VERSION <= 3
+//#if CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9
+//#define CV_RGB(r, g, b) cvScalar((b), (g), (r), 0)
+//#endif
+#define CV_RGB_LEGACY(r, g, b) cvScalar((b), (g), (r), 0)
 
 //using namespace bgslibrary::algorithms::lbp_mrf;
 
@@ -1286,7 +1287,7 @@ namespace bgslibrary
             Point1.y = y - Resulty / 2;
             Point2.x = x + Resultx / 2;
             Point2.y = y + Resulty / 2;
-            cvLine(ME_CAST_TO_IPLIMAGE(cvImg), Point1, Point2, CV_RGB(255, 255, 255), 1, 8);
+            cvLine(ME_CAST_TO_IPLIMAGE(cvImg), Point1, Point2, CV_RGB_LEGACY(255, 255, 255), 1, 8);
           }
       }
 
diff --git a/src/algorithms/LBP_MRF/MEImage.hpp b/src/algorithms/LBP_MRF/MEImage.hpp
index 9bc9e72729127a0413ad4c3fae13a530d8527ff5..9d9e079cb7b468e602c31689af4e799d1a85fd90 100644
--- a/src/algorithms/LBP_MRF/MEImage.hpp
+++ b/src/algorithms/LBP_MRF/MEImage.hpp
@@ -3,6 +3,12 @@
 #include "opencv2/core/version.hpp"
 #if CV_MAJOR_VERSION >= 2 && CV_MAJOR_VERSION <= 3
 
+//#include <opencv2/opencv.hpp>
+//#include <opencv2/imgproc.hpp>
+
+// opencv legacy includes
+#include <opencv2/imgproc/imgproc_c.h>
+
 namespace bgslibrary
 {
   namespace algorithms
diff --git a/src/algorithms/LBP_MRF/MotionDetection.cpp b/src/algorithms/LBP_MRF/MotionDetection.cpp
index b434806ad248ddfce5a28d5abe809a00c337ab24..80ff513f40e031c9e554120bf4d0296f2a1f1127 100644
--- a/src/algorithms/LBP_MRF/MotionDetection.cpp
+++ b/src/algorithms/LBP_MRF/MotionDetection.cpp
@@ -1,14 +1,17 @@
-#include <opencv2/opencv.hpp>
-#include <opencv2/imgproc.hpp>
+#include "opencv2/core/version.hpp"
+#if CV_MAJOR_VERSION >= 2 && CV_MAJOR_VERSION <= 3
 
 #include "MotionDetection.hpp"
 
-#if CV_MAJOR_VERSION >= 2 && CV_MAJOR_VERSION <= 3
-
 #include "graph.h"
 #include "MEHistogram.hpp"
 #include "MEImage.hpp"
 
+//#if CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9
+//#define CV_RGB(r, g, b) cvScalar((b), (g), (r), 0)
+//#endif
+#define CV_RGB_LEGACY(r, g, b) cvScalar((b), (g), (r), 0)
+
 namespace bgslibrary
 {
   namespace algorithms
@@ -1354,20 +1357,20 @@ namespace bgslibrary
         {
         case sm_Circle:
           cvCircle(MaskImage, cvPoint(HUHistogramArea / 2, HUHistogramArea / 2),
-            CircleRadius, CV_RGB(1, 1, 1), -1);
+            CircleRadius, CV_RGB_LEGACY(1, 1, 1), -1);
           break;
 
         case sm_Square:
           cvRectangle(MaskImage,
             cvPoint(HUHistogramArea / 2 - SquareSide / 2, HUHistogramArea / 2 - SquareSide / 2),
             cvPoint(HUHistogramArea / 2 + SquareSide / 2, HUHistogramArea / 2 + SquareSide / 2),
-            CV_RGB(1, 1, 1), -1);
+            CV_RGB_LEGACY(1, 1, 1), -1);
           break;
 
         case sm_Ellipse:
           cvEllipse(MaskImage, cvPoint(HUHistogramArea / 2, HUHistogramArea / 2),
             cvSize(EllipseA, EllipseB), 45, 0, 360,
-            CV_RGB(1, 1, 1), -1);
+            CV_RGB_LEGACY(1, 1, 1), -1);
           break;
 
         case sm_RandomPixels:
@@ -1387,7 +1390,7 @@ namespace bgslibrary
 
         default:
           cvCircle(MaskImage, cvPoint(HUHistogramArea / 2, HUHistogramArea / 2),
-            (int)MERound(sqrt((float)DesiredArea / ME_PI_VALUE)), CV_RGB(1, 1, 1), -1);
+            (int)MERound(sqrt((float)DesiredArea / ME_PI_VALUE)), CV_RGB_LEGACY(1, 1, 1), -1);
           break;
         }
 
diff --git a/src/algorithms/LBP_MRF/MotionDetection.hpp b/src/algorithms/LBP_MRF/MotionDetection.hpp
index f7720e5d2198275130c20c7762760fa91b129d81..a944ef27f301ded232f572251f33296dbad9ea30 100644
--- a/src/algorithms/LBP_MRF/MotionDetection.hpp
+++ b/src/algorithms/LBP_MRF/MotionDetection.hpp
@@ -3,7 +3,12 @@
 #include "opencv2/core/version.hpp"
 #if CV_MAJOR_VERSION >= 2 && CV_MAJOR_VERSION <= 3
 
-#include <opencv2/imgproc/types_c.h>
+//#include <opencv2/opencv.hpp>
+//#include <opencv2/imgproc.hpp>
+
+// opencv legacy includes
+#include <opencv2/imgproc/imgproc_c.h>
+#include <opencv2/video/tracking_c.h>
 
 #include "MEDefs.hpp"
 #include "MEImage.hpp"
diff --git a/src/algorithms/LBSimpleGaussian.cpp b/src/algorithms/LBSimpleGaussian.cpp
index 6586833be501a0bb2e6fb557c48dae89dd876eec..ab7e776acd98264aa29f375413bf6298ca490ef9 100644
--- a/src/algorithms/LBSimpleGaussian.cpp
+++ b/src/algorithms/LBSimpleGaussian.cpp
@@ -21,7 +21,12 @@ void LBSimpleGaussian::process(const cv::Mat &img_input, cv::Mat &img_output, cv
 {
   init(img_input, img_output, img_bgmodel);
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  IplImage *frame = &_frame;
+#else
   IplImage *frame = new IplImage(img_input);
+#endif
 
   if (firstTime) {
     int w = cvGetSize(frame).width;
diff --git a/src/algorithms/MultiCue.cpp b/src/algorithms/MultiCue.cpp
index 72fbab1a8904b5c5f8b1ed9cefab613c1a7fea16..4d0d065b7268a0c7e9beda7107b789243e73eedb 100644
--- a/src/algorithms/MultiCue.cpp
+++ b/src/algorithms/MultiCue.cpp
@@ -74,8 +74,14 @@ 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);
+
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  IplImage* frame = &_frame;
+#else
   IplImage* frame = new IplImage(img_input);
+#endif
+
   IplImage* result_image = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
   cvSetZero(result_image);
   if (g_iFrameCount <= g_iTrainingPeriod) {
@@ -537,8 +543,14 @@ 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);
+    
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+    IplImage _img = cvIplImage(temp_img);
+    IplImage* img = &_img;
+#else
     IplImage* img = new IplImage(temp_img);
+#endif
+
     //int iWidthStep = img->widthStep;
 
     for (int i = 0; i < g_iRHeight; i++) {
diff --git a/src/algorithms/MultiLayer.cpp b/src/algorithms/MultiLayer.cpp
index 1914785815ed7780af4925ab6fcd40f045a8b4c4..e5223b587451484f2fa8182ed71034d2b0ceeaed 100644
--- a/src/algorithms/MultiLayer.cpp
+++ b/src/algorithms/MultiLayer.cpp
@@ -57,8 +57,13 @@ 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;
-
+    
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+    IplImage _frame = cvIplImage(img_input);
+    org_img = &_frame;
+#else
     org_img = new IplImage(img_input);
+#endif
 
     fg_img = cvCreateImage(img_size, org_img->depth, org_img->nChannels);
     bg_img = cvCreateImage(img_size, org_img->depth, org_img->nChannels);
@@ -181,7 +186,12 @@ void MultiLayer::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat
       std::cout << algorithmName + " enabled learning in DETECT mode" << std::endl;
   }
 
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  IplImage* img = &_frame;
+#else
   IplImage* img = new IplImage(img_input);
+#endif
 
   BGS->SetRGBInputImage(img);
   BGS->Process();
diff --git a/src/algorithms/MultiLayer/CMultiLayerBGS.cpp b/src/algorithms/MultiLayer/CMultiLayerBGS.cpp
index 481f41662fbbc02a8b63ac82ca57b62a0dadd91e..cc5f3389f9ac5668083d8caccc54493c6743c417 100644
--- a/src/algorithms/MultiLayer/CMultiLayerBGS.cpp
+++ b/src/algorithms/MultiLayer/CMultiLayerBGS.cpp
@@ -1,3 +1,6 @@
+#include "opencv2/core/version.hpp"
+#if CV_MAJOR_VERSION >= 2 && CV_MAJOR_VERSION <= 3
+
 #include <ctime>
 #include <cstdlib>
 #include <cstdio>
@@ -6,12 +9,14 @@
 #include <cmath>
 #include <iostream>
 
-#include "opencv2/core/version.hpp"
-#if CV_MAJOR_VERSION >= 2 && CV_MAJOR_VERSION <= 3
-
 #include "CMultiLayerBGS.h"
 #include "OpenCvLegacyIncludes.h"
 
+//#if CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9
+//#define CV_RGB(r, g, b) cvScalar((b), (g), (r), 0)
+//#endif
+//#define CV_RGB_LEGACY(r, g, b) cvScalar((b), (g), (r), 0)
+
 using namespace bgslibrary::algorithms::multilayer;
 using namespace bgslibrary::algorithms::multilayer::blob;
 
@@ -1233,7 +1238,7 @@ void CMultiLayerBGS::GetBgLayerNoImage(IplImage *bg_layer_no_img, CvScalar *laye
     rgb[0] = rgb[1] = rgb[2] = 0;
     int rgb_idx = 0;
     for (int l = 0; l < bg_layer_color_num; l++) {
-      bg_layer_colors[l] = CV_RGB(rgb[0], rgb[1], rgb[2]);
+      bg_layer_colors[l] = CV_RGB_LEGACY(rgb[0], rgb[1], rgb[2]);
       rgb[rgb_idx] += 200;
       rgb[rgb_idx] %= 255;
       rgb_idx++;
diff --git a/src/algorithms/MultiLayer/CMultiLayerBGS.h b/src/algorithms/MultiLayer/CMultiLayerBGS.h
index 5ff976c59dfc79888ce537c529c11f8765f805a8..7bf46309d0c77807b00fa1bb8ee68019e1039cc5 100644
--- a/src/algorithms/MultiLayer/CMultiLayerBGS.h
+++ b/src/algorithms/MultiLayer/CMultiLayerBGS.h
@@ -1,5 +1,8 @@
 #pragma once
 
+#include "opencv2/core/version.hpp"
+#if CV_MAJOR_VERSION >= 2 && CV_MAJOR_VERSION <= 3
+
 /*
 Since the used fast cross bilateral filter codes can not be compiled under Windows,
 we don't use the bilateral filter to remove the noise in the foreground detection
@@ -18,10 +21,10 @@ step. If you compile it under Linux, please uncomment it.
 #include <cmath>
 #include <iostream>
 
-#include <opencv2/imgproc.hpp>
+//#include <opencv2/imgproc.hpp>
 
-#include "opencv2/core/version.hpp"
-#if CV_MAJOR_VERSION >= 2 && CV_MAJOR_VERSION <= 3
+// opencv legacy includes
+#include <opencv2/imgproc/imgproc_c.h>
 
 #include "LocalBinaryPattern.h"
 #include "BlobResult.h"
@@ -32,6 +35,8 @@ step. If you compile it under Linux, please uncomment it.
 #include "CrossBilateralFilter.h"
 #endif
 
+#define CV_RGB_LEGACY(r, g, b) cvScalar((b), (g), (r), 0)
+
 namespace bgslibrary
 {
   namespace algorithms
@@ -134,14 +139,14 @@ namespace bgslibrary
         void SetCurrentFrameNumber(unsigned long cur_frame_no);
 
         void GetForegroundMaskImage(IplImage *fg_mask_img);
-        void GetForegroundImage(IplImage *fg_img, CvScalar bg_color = CV_RGB(0, 255, 0));
+        void GetForegroundImage(IplImage *fg_img, CvScalar bg_color = CV_RGB_LEGACY(0, 255, 0));
         void GetBackgroundImage(IplImage *bk_img);
         void GetForegroundProbabilityImage(IplImage* fg_prob_img);
 
         void GetBgLayerNoImage(IplImage *bg_layer_no_img, CvScalar* layer_colors = NULL, int layer_num = 0);
-        void GetLayeredBackgroundImage(int layered_no, IplImage *layered_bg_img, CvScalar empty_color = CV_RGB(0, 0, 0));
+        void GetLayeredBackgroundImage(int layered_no, IplImage *layered_bg_img, CvScalar empty_color = CV_RGB_LEGACY(0, 0, 0));
         void GetCurrentLayeredBackgroundImage(int layered_no, IplImage *layered_bg_img, IplImage *layered_fg_img = NULL,
-          CvScalar layered_bg_bk_color = CV_RGB(0, 0, 0), CvScalar layered_fg_color = CV_RGB(255, 0, 0),
+          CvScalar layered_bg_bk_color = CV_RGB_LEGACY(0, 0, 0), CvScalar layered_fg_color = CV_RGB_LEGACY(255, 0, 0),
           int smooth_win = 13, float smooth_sigma = 3.0f, float below_layer_noise = 0.5f, float above_layer_noise = 0.3f, int min_blob_size = 50);
         float DistLBP(LBPStruct *LBP1, LBPStruct *LBP2);
         void GetColoredBgMultiLayeredImage(IplImage *bg_multi_layer_img, CvScalar *layer_colors);
diff --git a/src/algorithms/MultiLayer/LocalBinaryPattern.h b/src/algorithms/MultiLayer/LocalBinaryPattern.h
index 69a119e21da3598244afce1585474fea93e6535a..23d96368c301df4c7d76408f167acf297a1c9310 100644
--- a/src/algorithms/MultiLayer/LocalBinaryPattern.h
+++ b/src/algorithms/MultiLayer/LocalBinaryPattern.h
@@ -1,10 +1,10 @@
 #pragma once
 
-#include <cstdio>
-
 #include "opencv2/core/version.hpp"
 #if CV_MAJOR_VERSION >= 2 && CV_MAJOR_VERSION <= 3
 
+#include <cstdio>
+
 #include "BGS.h"
 #include "OpenCvDataConversion.h"
 
diff --git a/src/algorithms/MultiLayer/OpenCvDataConversion.h b/src/algorithms/MultiLayer/OpenCvDataConversion.h
index 9e78c6afc7ec311cad9a802ccea24e99a7c9bad4..76af1d5d35435eee44b1619089eeaf87b7f7a4fa 100644
--- a/src/algorithms/MultiLayer/OpenCvDataConversion.h
+++ b/src/algorithms/MultiLayer/OpenCvDataConversion.h
@@ -1,10 +1,10 @@
 #pragma once
 
-#include <stdio.h>
-
 #include "opencv2/core/version.hpp"
 #if CV_MAJOR_VERSION >= 2 && CV_MAJOR_VERSION <= 3
 
+#include <stdio.h>
+
 // opencv legacy includes
 #include "OpenCvLegacyIncludes.h"
 
diff --git a/src/algorithms/MultiLayer/OpenCvLegacyIncludes.h b/src/algorithms/MultiLayer/OpenCvLegacyIncludes.h
index a539092196f41dd3c4e08063a714e5a2e9abce6c..4c2e5409342da270145c13f97c566d68b6073015 100644
--- a/src/algorithms/MultiLayer/OpenCvLegacyIncludes.h
+++ b/src/algorithms/MultiLayer/OpenCvLegacyIncludes.h
@@ -5,7 +5,6 @@
 
 // opencv legacy includes
 #include "opencv2/core/core_c.h"
-#include "opencv2/core/types_c.h"
 #include "opencv2/imgproc/imgproc_c.h"
 
 #endif
diff --git a/src/algorithms/T2FGMM_UM.cpp b/src/algorithms/T2FGMM_UM.cpp
index 7c95dfe58cab47bd1f9f2c36c50bc6776bb62fa2..808eea1dfb694cf4692403e4a582e1e09eb89105 100644
--- a/src/algorithms/T2FGMM_UM.cpp
+++ b/src/algorithms/T2FGMM_UM.cpp
@@ -20,7 +20,13 @@ 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);
+
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  frame = &_frame;
+#else
   frame = new IplImage(img_input);
+#endif
 
   if (firstTime)
     frame_data.ReleaseMemory(false);
diff --git a/src/algorithms/T2FGMM_UV.cpp b/src/algorithms/T2FGMM_UV.cpp
index cff817bd82f0266bedea0cb809edbcbdc1a21084..e7137a167b5be5f923bdc76da9dd4b8c2c667552 100644
--- a/src/algorithms/T2FGMM_UV.cpp
+++ b/src/algorithms/T2FGMM_UV.cpp
@@ -20,7 +20,13 @@ 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);
+  
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  frame = &_frame;
+#else
   frame = new IplImage(img_input);
+#endif
 
   if (firstTime)
     frame_data.ReleaseMemory(false);
diff --git a/src/algorithms/T2FMRF_UM.cpp b/src/algorithms/T2FMRF_UM.cpp
index 208be5d63d5fa7199e79885fd76fd2a44d6fac40..3b712f9fbbc1c5ec4c7ee4b727719c5448b0507f 100644
--- a/src/algorithms/T2FMRF_UM.cpp
+++ b/src/algorithms/T2FMRF_UM.cpp
@@ -20,7 +20,13 @@ 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);
+  
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  frame = &_frame;
+#else
   frame = new IplImage(img_input);
+#endif
 
   if (firstTime)
     frame_data.ReleaseMemory(false);
diff --git a/src/algorithms/T2FMRF_UV.cpp b/src/algorithms/T2FMRF_UV.cpp
index c0a1c5ff65c2f13f3427f44178c43bfdb807934d..f918c6f47b23fa6e423fc29bc6135fbc123ced2c 100644
--- a/src/algorithms/T2FMRF_UV.cpp
+++ b/src/algorithms/T2FMRF_UV.cpp
@@ -20,7 +20,13 @@ 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);
+  
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  frame = &_frame;
+#else
   frame = new IplImage(img_input);
+#endif
 
   if (firstTime)
     frame_data.ReleaseMemory(false);
diff --git a/src/algorithms/VuMeter.cpp b/src/algorithms/VuMeter.cpp
index c83d6b8f1f0046287f3403a8efacfb4459cb8f4a..f42c3455dce5733967257c5323ae0ed03620d022 100644
--- a/src/algorithms/VuMeter.cpp
+++ b/src/algorithms/VuMeter.cpp
@@ -23,7 +23,13 @@ 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);
+
+#if CV_MAJOR_VERSION > 3 || (CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9)
+  IplImage _frame = cvIplImage(img_input);
+  frame = &_frame;
+#else
   frame = new IplImage(img_input);
+#endif
 
   if (firstTime) {
     bgs.SetAlpha(alpha);
diff --git a/src/algorithms/dp/AdaptiveMedianBGS.cpp b/src/algorithms/dp/AdaptiveMedianBGS.cpp
index 2609fb0906cc11900502b8fa6afb650b64db5471..53c1e3de579f3158ba3ccc14003422f6a188dac3 100644
--- a/src/algorithms/dp/AdaptiveMedianBGS.cpp
+++ b/src/algorithms/dp/AdaptiveMedianBGS.cpp
@@ -2,13 +2,18 @@
 
 #if CV_MAJOR_VERSION >= 2 && CV_MAJOR_VERSION <= 3
 
+//#if CV_MAJOR_VERSION == 3 && CV_SUBMINOR_VERSION >= 9
+//#define CV_RGB(r, g, b) cvScalar((b), (g), (r), 0)
+//#endif
+#define CV_RGB_LEGACY(r, g, b) cvScalar((b), (g), (r), 0)
+
 using namespace bgslibrary::algorithms::dp;
 
 void AdaptiveMedianBGS::Initalize(const BgsParams& param)
 {
   m_params = (AdaptiveMedianParams&)param;
   m_median = cvCreateImage(cvSize(m_params.Width(), m_params.Height()), IPL_DEPTH_8U, 3);
-  cvSet(m_median.Ptr(), CV_RGB(BACKGROUND, BACKGROUND, BACKGROUND));
+  cvSet(m_median.Ptr(), CV_RGB_LEGACY(BACKGROUND, BACKGROUND, BACKGROUND));
 }
 
 RgbImage* AdaptiveMedianBGS::Background()