From ca381fbd8e7300a668c0d1f68e72037b15bd60bb Mon Sep 17 00:00:00 2001
From: Andrews Sobral <andrewssobral@gmail.com>
Date: Sun, 1 Mar 2020 17:06:41 +0100
Subject: [PATCH] first fixes for opencv 3.4.9

---
 src/algorithms/DPAdaptiveMedian.cpp           |  5 +++++
 src/algorithms/DPEigenbackground.cpp          |  5 +++++
 src/algorithms/DPGrimsonGMM.cpp               |  5 +++++
 src/algorithms/DPMean.cpp                     |  5 +++++
 src/algorithms/DPPratiMediod.cpp              |  5 +++++
 src/algorithms/DPTexture.cpp                  |  5 +++++
 src/algorithms/DPWrenGA.cpp                   |  5 +++++
 src/algorithms/DPZivkovicAGMM.cpp             |  5 +++++
 src/algorithms/FuzzyChoquetIntegral.cpp       | 14 ++++++++++++++
 src/algorithms/FuzzySugenoIntegral.cpp        | 14 ++++++++++++++
 src/algorithms/IBGS.h                         |  2 +-
 src/algorithms/LBAdaptiveSOM.cpp              |  6 +++++-
 src/algorithms/LBFuzzyAdaptiveSOM.cpp         |  5 +++++
 src/algorithms/LBFuzzyGaussian.cpp            |  6 +++++-
 src/algorithms/LBMixtureOfGaussians.cpp       |  6 +++++-
 src/algorithms/LBP_MRF.cpp                    |  5 +++++
 src/algorithms/LBP_MRF/MEImage.cpp            | 13 +++++++------
 src/algorithms/LBP_MRF/MEImage.hpp            |  6 ++++++
 src/algorithms/LBP_MRF/MotionDetection.cpp    | 19 +++++++++++--------
 src/algorithms/LBP_MRF/MotionDetection.hpp    |  7 ++++++-
 src/algorithms/LBSimpleGaussian.cpp           |  5 +++++
 src/algorithms/MultiCue.cpp                   | 16 ++++++++++++++--
 src/algorithms/MultiLayer.cpp                 | 12 +++++++++++-
 src/algorithms/MultiLayer/CMultiLayerBGS.cpp  | 13 +++++++++----
 src/algorithms/MultiLayer/CMultiLayerBGS.h    | 17 +++++++++++------
 .../MultiLayer/LocalBinaryPattern.h           |  4 ++--
 .../MultiLayer/OpenCvDataConversion.h         |  4 ++--
 .../MultiLayer/OpenCvLegacyIncludes.h         |  1 -
 src/algorithms/T2FGMM_UM.cpp                  |  6 ++++++
 src/algorithms/T2FGMM_UV.cpp                  |  6 ++++++
 src/algorithms/T2FMRF_UM.cpp                  |  6 ++++++
 src/algorithms/T2FMRF_UV.cpp                  |  6 ++++++
 src/algorithms/VuMeter.cpp                    |  6 ++++++
 src/algorithms/dp/AdaptiveMedianBGS.cpp       |  7 ++++++-
 34 files changed, 214 insertions(+), 38 deletions(-)

diff --git a/src/algorithms/DPAdaptiveMedian.cpp b/src/algorithms/DPAdaptiveMedian.cpp
index e121bdf..d8c0bc8 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 dfc7d64..770cc61 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 743acf1..77e2693 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 18320cd..766e379 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 3cade20..b53552b 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 7f58230..ba33e9f 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 a7f9128..e3f5df2 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 289f5cc..6fd403c 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 90dfe6e..2145eae 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 5ac009a..1c70dc4 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 bbf835f..d92b5df 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 b051ed5..4c03035 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 a4feed6..a88b578 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 c501848..31e3232 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 219cd1b..449b99c 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 e0d4c64..c0f31de 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 7899b52..3103e73 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 9bc9e72..9d9e079 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 b434806..80ff513 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 f7720e5..a944ef2 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 6586833..ab7e776 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 72fbab1..4d0d065 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 1914785..e5223b5 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 481f416..cc5f338 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 5ff976c..7bf4630 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 69a119e..23d9636 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 9e78c6a..76af1d5 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 a539092..4c2e540 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 7c95dfe..808eea1 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 cff817b..e7137a1 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 208be5d..3b712f9 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 c0a1c5f..f918c6f 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 c83d6b8..f42c345 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 2609fb0..53c1e3d 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()
-- 
GitLab