opencv: segmentation: Ported to OpenCV version 3.1

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
This commit is contained in:
Vanessa Chipirras Navalon 2016-04-01 12:59:19 +02:00 committed by Luis de Bethencourt
parent 88a3b4da3c
commit 4e1aba0923
2 changed files with 52 additions and 18 deletions

View File

@ -91,12 +91,15 @@
#endif #endif
#include "gstsegmentation.h" #include "gstsegmentation.h"
#include <opencv2/video/background_segm.hpp>
#include <opencv2/imgproc/imgproc_c.h> #include <opencv2/imgproc/imgproc_c.h>
GST_DEBUG_CATEGORY_STATIC (gst_segmentation_debug); GST_DEBUG_CATEGORY_STATIC (gst_segmentation_debug);
#define GST_CAT_DEFAULT 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 */ /* Filter signals and args */
enum enum
{ {
@ -777,11 +780,16 @@ find_connected_components (IplImage * mask, int poly1_hull0, float perimScale,
int int
initialise_mog (GstSegmentation * filter) initialise_mog (GstSegmentation * filter)
{ {
filter->img_input_as_cvMat = (void *) new cv::Mat (filter->cvYUV, false); filter->img_input_as_cvMat = (void *) new Mat (cvarrToMat (filter->cvYUV, false));
filter->img_fg_as_cvMat = (void *) new cv::Mat (filter->cvFG, false); filter->img_fg_as_cvMat = (void *) new Mat (cvarrToMat(filter->cvFG, false));
filter->mog = (void *) new cv::BackgroundSubtractorMOG (); #if (CV_MAJOR_VERSION >= 3)
filter->mog2 = (void *) new cv::BackgroundSubtractorMOG2 (); filter->mog = bgsegm::createBackgroundSubtractorMOG ();
filter->mog2 = createBackgroundSubtractorMOG2 ();
#else
filter->mog = (void *) new BackgroundSubtractorMOG ();
filter->mog2 = (void *) new BackgroundSubtractorMOG2 ();
#endif
return (0); return (0);
} }
@ -804,9 +812,15 @@ run_mog_iteration (GstSegmentation * filter)
European Workshop on Advanced Video-Based Surveillance Systems, 2001 European Workshop on Advanced Video-Based Surveillance Systems, 2001
*/ */
(*((cv::BackgroundSubtractorMOG *) filter->mog)) (*((cv::Mat *) filter-> #if (CV_MAJOR_VERSION >= 3)
img_input_as_cvMat), *((cv::Mat *) filter->img_fg_as_cvMat), filter->mog->apply (*((Mat *) filter->
img_input_as_cvMat), *((Mat *) filter->img_fg_as_cvMat),
filter->learning_rate); 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); return (0);
} }
@ -814,10 +828,10 @@ run_mog_iteration (GstSegmentation * filter)
int int
run_mog2_iteration (GstSegmentation * filter) run_mog2_iteration (GstSegmentation * filter)
{ {
((cv::Mat *) filter->img_input_as_cvMat)->data = ((Mat *) filter->img_input_as_cvMat)->data =
(uchar *) filter->cvYUV->imageData; (uchar *) filter->cvYUV->imageData;
((cv::Mat *) filter->img_fg_as_cvMat)->data = ((Mat *) filter->img_fg_as_cvMat)->data =
(uchar *) filter->cvFG->imageData; (uchar *) filter->cvFG->imageData;
/* /*
BackgroundSubtractorMOG2 [1], Gaussian Mixture-based Background/Foreground 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. Letters, vol. 27, no. 7, pages 773-780, 2006.
*/ */
(*((cv::BackgroundSubtractorMOG *) filter->mog2)) (*((cv::Mat *) filter-> #if (CV_MAJOR_VERSION >= 3)
img_input_as_cvMat), *((cv::Mat *) filter->img_fg_as_cvMat), filter->mog2->apply (*((Mat *) filter->
img_input_as_cvMat), *((Mat *) filter->img_fg_as_cvMat),
filter->learning_rate); 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); return (0);
} }
@ -842,9 +862,14 @@ run_mog2_iteration (GstSegmentation * filter)
int int
finalise_mog (GstSegmentation * filter) finalise_mog (GstSegmentation * filter)
{ {
delete (cv::Mat *) filter->img_input_as_cvMat; delete (Mat *) filter->img_input_as_cvMat;
delete (cv::Mat *) filter->img_fg_as_cvMat; delete (Mat *) filter->img_fg_as_cvMat;
delete (cv::BackgroundSubtractorMOG *) filter->mog; #if (CV_MAJOR_VERSION >= 3)
delete (cv::BackgroundSubtractorMOG2 *) filter->mog2; filter->mog.release ();
filter->mog2.release ();
#else
delete (BackgroundSubtractorMOG *) filter->mog;
delete (BackgroundSubtractorMOG2 *) filter->mog2;
#endif
return (0); return (0);
} }

View File

@ -47,7 +47,11 @@
#include <gst/gst.h> #include <gst/gst.h>
#include <gst/video/gstvideofilter.h> #include <gst/video/gstvideofilter.h>
#include <opencv2/core/core_c.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 G_BEGIN_DECLS
@ -107,8 +111,13 @@ struct _GstSegmentation
CvSeq *contours; CvSeq *contours;
/* for MOG methods */ /* 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 *mog; /* cv::BackgroundSubtractorMOG */
void *mog2; /* cv::BackgroundSubtractorMOG2 */ void *mog2; /* cv::BackgroundSubtractorMOG2 */
#endif
void *img_input_as_cvMat; /* cv::Mat */ void *img_input_as_cvMat; /* cv::Mat */
void *img_fg_as_cvMat; /* cv::Mat */ void *img_fg_as_cvMat; /* cv::Mat */
double learning_rate; double learning_rate;