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
#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->img_input_as_cvMat = (void *) new Mat (cvarrToMat (filter->cvYUV, false));
filter->img_fg_as_cvMat = (void *) new Mat (cvarrToMat(filter->cvFG, false));
filter->mog = (void *) new cv::BackgroundSubtractorMOG ();
filter->mog2 = (void *) new cv::BackgroundSubtractorMOG2 ();
#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);
}

View file

@ -47,7 +47,11 @@
#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;