opencv: segmentation: Ported to OpenCV version 3.1
authorVanessa Chipirras Navalon <vanechipi@qtec.com>
Fri, 1 Apr 2016 10:59:19 +0000 (12:59 +0200)
committerLuis de Bethencourt <luisbg@osg.samsung.com>
Fri, 1 Apr 2016 11:24:13 +0000 (12:24 +0100)
Add namespace bgsegm, replacement functions and Template class for new
OpenCV versions because these functions have been removed. cvarrToMat() is
added because it is compatible with all versions of OpenCV and the use of
class Mat constructor is eliminated, it is also deprecated in 3.X versions.

Use the namespace cv because some functions are called many times.

This patch keeps compatibility with 2.4

https://bugzilla.gnome.org/show_bug.cgi?id=760473

ext/opencv/gstsegmentation.cpp
ext/opencv/gstsegmentation.h

index 864a809..472a3c4 100644 (file)
 #endif
 
 #include "gstsegmentation.h"
-#include <opencv2/video/background_segm.hpp>
 #include <opencv2/imgproc/imgproc_c.h>
 
 GST_DEBUG_CATEGORY_STATIC (gst_segmentation_debug);
 #define GST_CAT_DEFAULT gst_segmentation_debug
 
+using namespace cv;
+#if (CV_MAJOR_VERSION >= 3)
+  using namespace cv::bgsegm;
+#endif
 /* Filter signals and args */
 enum
 {
@@ -777,11 +780,16 @@ find_connected_components (IplImage * mask, int poly1_hull0, float perimScale,
 int
 initialise_mog (GstSegmentation * filter)
 {
-  filter->img_input_as_cvMat = (void *) new cv::Mat (filter->cvYUV, false);
-  filter->img_fg_as_cvMat = (void *) new cv::Mat (filter->cvFG, false);
-
-  filter->mog = (void *) new cv::BackgroundSubtractorMOG ();
-  filter->mog2 = (void *) new cv::BackgroundSubtractorMOG2 ();
+  filter->img_input_as_cvMat = (void *) new Mat (cvarrToMat (filter->cvYUV, false));
+  filter->img_fg_as_cvMat = (void *) new Mat (cvarrToMat(filter->cvFG, false));
+
+#if (CV_MAJOR_VERSION >= 3)
+  filter->mog = bgsegm::createBackgroundSubtractorMOG ();
+  filter->mog2 = createBackgroundSubtractorMOG2 ();
+#else
+  filter->mog = (void *) new BackgroundSubtractorMOG ();
+  filter->mog2 = (void *) new BackgroundSubtractorMOG2 ();
+#endif
 
   return (0);
 }
@@ -804,9 +812,15 @@ run_mog_iteration (GstSegmentation * filter)
      European Workshop on Advanced Video-Based Surveillance Systems, 2001
    */
 
-  (*((cv::BackgroundSubtractorMOG *) filter->mog)) (*((cv::Mat *) filter->
-          img_input_as_cvMat), *((cv::Mat *) filter->img_fg_as_cvMat),
+#if (CV_MAJOR_VERSION >= 3)
+  filter->mog->apply (*((Mat *) filter->
+          img_input_as_cvMat), *((Mat *) filter->img_fg_as_cvMat),
       filter->learning_rate);
+#else
+  (*((BackgroundSubtractorMOG *) filter->mog)) (*((Mat *) filter->
+          img_input_as_cvMat), *((Mat *) filter->img_fg_as_cvMat),
+      filter->learning_rate);
+#endif
 
   return (0);
 }
@@ -814,10 +828,10 @@ run_mog_iteration (GstSegmentation * filter)
 int
 run_mog2_iteration (GstSegmentation * filter)
 {
-  ((cv::Mat *) filter->img_input_as_cvMat)->data =
-      (uchar *) filter->cvYUV->imageData;
-  ((cv::Mat *) filter->img_fg_as_cvMat)->data =
-      (uchar *) filter->cvFG->imageData;
+  ((Mat *) filter->img_input_as_cvMat)->data =
+       (uchar *) filter->cvYUV->imageData;
+  ((Mat *) filter->img_fg_as_cvMat)->data =
+       (uchar *) filter->cvFG->imageData;
 
   /*
      BackgroundSubtractorMOG2 [1], Gaussian Mixture-based Background/Foreground
@@ -832,9 +846,15 @@ run_mog2_iteration (GstSegmentation * filter)
      Letters, vol. 27, no. 7, pages 773-780, 2006.
    */
 
-  (*((cv::BackgroundSubtractorMOG *) filter->mog2)) (*((cv::Mat *) filter->
-          img_input_as_cvMat), *((cv::Mat *) filter->img_fg_as_cvMat),
+#if (CV_MAJOR_VERSION >= 3)
+  filter->mog2->apply (*((Mat *) filter->
+          img_input_as_cvMat), *((Mat *) filter->img_fg_as_cvMat),
       filter->learning_rate);
+#else
+  (*((BackgroundSubtractorMOG *) filter->mog2)) (*((Mat *) filter->
+          img_input_as_cvMat), *((Mat *) filter->img_fg_as_cvMat),
+      filter->learning_rate);
+#endif
 
   return (0);
 }
@@ -842,9 +862,14 @@ run_mog2_iteration (GstSegmentation * filter)
 int
 finalise_mog (GstSegmentation * filter)
 {
-  delete (cv::Mat *) filter->img_input_as_cvMat;
-  delete (cv::Mat *) filter->img_fg_as_cvMat;
-  delete (cv::BackgroundSubtractorMOG *) filter->mog;
-  delete (cv::BackgroundSubtractorMOG2 *) filter->mog2;
+  delete (Mat *) filter->img_input_as_cvMat;
+  delete (Mat *) filter->img_fg_as_cvMat;
+#if (CV_MAJOR_VERSION >= 3)
+  filter->mog.release ();
+  filter->mog2.release ();
+#else
+  delete (BackgroundSubtractorMOG *) filter->mog;
+  delete (BackgroundSubtractorMOG2 *) filter->mog2;
+#endif
   return (0);
 }
index 9400853..982e2ba 100644 (file)
 #include <gst/gst.h>
 #include <gst/video/gstvideofilter.h>
 #include <opencv2/core/core_c.h>
-
+#include <opencv2/video/background_segm.hpp>
+#include <opencv2/core/version.hpp>
+#if (CV_MAJOR_VERSION >= 3)
+  #include <opencv2/bgsegm.hpp>
+#endif
 
 
 G_BEGIN_DECLS
@@ -107,8 +111,13 @@ struct _GstSegmentation
   CvSeq *contours;
 
   /* for MOG methods */
+#if (CV_MAJOR_VERSION >= 3)
+  cv::Ptr<cv::BackgroundSubtractor> mog;                   /* cv::BackgroundSubtractorMOG */
+  cv::Ptr<cv::BackgroundSubtractorMOG2> mog2;                   /* cv::BackgroundSubtractorMOG2 */
+#else
   void *mog;                    /* cv::BackgroundSubtractorMOG */
   void *mog2;                   /* cv::BackgroundSubtractorMOG2 */
+#endif
   void *img_input_as_cvMat;     /* cv::Mat */
   void *img_fg_as_cvMat;        /* cv::Mat */
   double learning_rate;