opencv: make compatible with opencv 4
authorNicola Murino <nicola.murino@gmail.com>
Sun, 25 Nov 2018 15:13:28 +0000 (16:13 +0100)
committerNicolas Dufresne <nicolas@ndufresne.ca>
Mon, 26 Nov 2018 13:29:03 +0000 (13:29 +0000)
Closed #826

32 files changed:
configure.ac
ext/opencv/MotionCells.cpp
ext/opencv/MotionCells.h
ext/opencv/camerautils.hpp
ext/opencv/gstcameracalibrate.cpp
ext/opencv/gstcameraundistort.cpp
ext/opencv/gstcvdilate.cpp
ext/opencv/gstcvequalizehist.cpp
ext/opencv/gstcverode.cpp
ext/opencv/gstcvlaplace.cpp
ext/opencv/gstcvsmooth.cpp
ext/opencv/gstcvsobel.cpp
ext/opencv/gstdewarp.h
ext/opencv/gstdisparity.cpp
ext/opencv/gstdisparity.h
ext/opencv/gstedgedetect.cpp
ext/opencv/gstfaceblur.cpp
ext/opencv/gstfacedetect.cpp
ext/opencv/gstgrabcut.cpp
ext/opencv/gstgrabcut.h
ext/opencv/gsthanddetect.cpp
ext/opencv/gsthanddetect.h
ext/opencv/gstretinex.cpp
ext/opencv/gstsegmentation.cpp
ext/opencv/gstsegmentation.h
ext/opencv/gstskindetect.cpp
ext/opencv/gsttemplatematch.cpp
ext/opencv/gsttextoverlay.h
ext/opencv/meson.build
gst-libs/gst/opencv/gstopencvutils.cpp
gst-libs/gst/opencv/gstopencvvideofilter.cpp
gst-libs/gst/opencv/meson.build

index 1bb19f0..c43a1f0 100644 (file)
@@ -1846,7 +1846,7 @@ AG_GST_CHECK_FEATURE(OPENCV, [opencv plugins], opencv, [
   dnl a new version and the no-backward-compatibility define. (There doesn't
   dnl seem to be a switch to suppress the warnings the cvcompat.h header
   dnl causes.)
-  PKG_CHECK_MODULES(OPENCV, opencv >= 3.0.0 opencv < 3.5.0 , [
+  PKG_CHECK_MODULES([OPENCV], [opencv >= 3.0.0 opencv < 3.5.0] , [
     AC_PROG_CXX
     AC_LANG([C++])
     OLD_CPPFLAGS=$CPPFLAGS
@@ -1872,8 +1872,41 @@ AG_GST_CHECK_FEATURE(OPENCV, [opencv plugins], opencv, [
       HAVE_OPENCV="yes"
     fi
   ], [
+    PKG_CHECK_MODULES([OPENCV], [opencv4 >= 4.0.0 opencv4 < 4.1.0] , [
+    AC_PROG_CXX
+    AC_LANG([C++])
+    OLD_CPPFLAGS=$CPPFLAGS
+    CPPFLAGS=$OPENCV_CFLAGS
+    something_not_found=no
+    AC_CHECK_HEADERS([opencv2/bgsegm.hpp \
+                      opencv2/calib3d.hpp \
+                      opencv2/core.hpp \
+                      opencv2/imgcodecs/legacy/constants_c.h \
+                      opencv2/imgproc/imgproc_c.h \
+                      opencv2/core/types_c.h \
+                      opencv2/core/core_c.h \
+                      opencv2/imgproc.hpp \
+                      opencv2/objdetect.hpp \
+                      opencv2/opencv.hpp \
+                      opencv2/video.hpp \
+                      opencv2/bgsegm.hpp], [], [something_not_found=yes])
+
+    CPPFLAGS=$OLD_CPPFLAGS
+    AC_LANG([C])
+
+    if test $something_not_found = "yes"; then
+      AC_MSG_RESULT([not all opencv2 headers were found])
+      HAVE_OPENCV="no"
+    else
+      HAVE_OPENCV="yes"
+    fi
+  ], [
     HAVE_OPENCV="no"
   ])
+  ])
+  OPENCV_PREFIX="`$PKG_CONFIG --variable=prefix opencv`"
+  AC_DEFINE_UNQUOTED(OPENCV_PREFIX, "$OPENCV_PREFIX", [opencv install prefix])
+  AC_SUBST(OPENCV_PREFIX)
   OPENCV_PREFIX="`$PKG_CONFIG --variable=prefix opencv`"
   AC_DEFINE_UNQUOTED(OPENCV_PREFIX, "$OPENCV_PREFIX", [opencv install prefix])
   AC_SUBST(OPENCV_PREFIX)
index f26f9ec..4c11995 100644 (file)
@@ -49,6 +49,9 @@
 #include <errno.h>
 #include "MotionCells.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 MotionCells::MotionCells ()
 {
index 6df49ab..0658a31 100644 (file)
 #define MOTIONCELLS_H_
 
 #include <opencv2/core.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/core/types_c.h>
+#include <opencv2/core/core_c.h>
+#endif
 
 #include <fstream>
 #include <vector>
index 39e7891..968e964 100644 (file)
@@ -21,7 +21,7 @@
 #define __GST_CAMERA_UTILS_H__
 
 #include <gst/gst.h>
-#include <cv.h>
+#include <opencv2/core.hpp>
 
 G_BEGIN_DECLS
 
index 1ea7e5c..a84672b 100644 (file)
@@ -85,6 +85,9 @@
 #include "gstcameracalibrate.h"
 
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 #include <opencv2/calib3d.hpp>
 
 #include <gst/opencv/gstopencvutils.h>
index 1296551..93934be 100644 (file)
@@ -78,6 +78,9 @@
 #include "gstcameraundistort.h"
 
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 #include <opencv2/calib3d.hpp>
 
 #include <gst/opencv/gstopencvutils.h>
index 673df6b..2d2df7e 100644 (file)
@@ -60,6 +60,9 @@
 
 #include "gstcvdilate.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 GST_DEBUG_CATEGORY_STATIC (gst_cv_dilate_debug);
 #define GST_CAT_DEFAULT gst_cv_dilate_debug
index 7f6327e..3024bd9 100644 (file)
@@ -62,6 +62,9 @@
 
 #include "gstcvequalizehist.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 GST_DEBUG_CATEGORY_STATIC (gst_cv_equalize_hist_debug);
 #define GST_CAT_DEFAULT gst_cv_equalize_hist_debug
index 65e4a55..49ee4ba 100644 (file)
@@ -60,6 +60,9 @@
 
 #include "gstcverode.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 GST_DEBUG_CATEGORY_STATIC (gst_cv_erode_debug);
 #define GST_CAT_DEFAULT gst_cv_erode_debug
index a84372c..49d6d5f 100644 (file)
 
 #include "gstcvlaplace.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/core/core_c.h>
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
+
 
 GST_DEBUG_CATEGORY_STATIC (gst_cv_laplace_debug);
 #define GST_CAT_DEFAULT gst_cv_laplace_debug
index 5f1e9ea..ac6f0f5 100644 (file)
@@ -61,6 +61,9 @@
 #include "gst/opencv/gstopencvutils.h"
 #include "gstcvsmooth.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 
 GST_DEBUG_CATEGORY_STATIC (gst_cv_smooth_debug);
index 0f11c39..7601ba7 100644 (file)
@@ -60,6 +60,9 @@
 
 #include "gstcvsobel.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 GST_DEBUG_CATEGORY_STATIC (gst_cv_sobel_debug);
 #define GST_CAT_DEFAULT gst_cv_sobel_debug
index 3ef260d..61b319b 100644 (file)
@@ -49,6 +49,9 @@
 #include <gst/opencv/gstopencvvideofilter.h>
 #include <opencv2/core.hpp>
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 G_BEGIN_DECLS
 /* #defines don't like whitespacey bits */
index 4d13fc2..79a7660 100644 (file)
@@ -121,6 +121,9 @@ gst-launch-1.0    multifilesrc  location=~/im3.png ! pngdec ! videoconvert  ! di
 
 #include "gstdisparity.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 GST_DEBUG_CATEGORY_STATIC (gst_disparity_debug);
 #define GST_CAT_DEFAULT gst_disparity_debug
index 863348e..9bd2c52 100644 (file)
@@ -49,6 +49,9 @@
 
 #include <opencv2/core.hpp>
 #include <opencv2/calib3d.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/core/types_c.h>
+#endif
 
 G_BEGIN_DECLS
 /* #defines don't like whitespacey bits */
index 0ddf251..22f754d 100644 (file)
@@ -62,6 +62,9 @@
 
 #include "gstedgedetect.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 GST_DEBUG_CATEGORY_STATIC (gst_edge_detect_debug);
 #define GST_CAT_DEFAULT gst_edge_detect_debug
index f008481..bfaf6f3 100644 (file)
@@ -65,6 +65,9 @@
 
 #include "gstfaceblur.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 GST_DEBUG_CATEGORY_STATIC (gst_face_blur_debug);
 #define GST_CAT_DEFAULT gst_face_blur_debug
@@ -73,7 +76,11 @@ GST_DEBUG_CATEGORY_STATIC (gst_face_blur_debug);
     G_DIR_SEPARATOR_S OPENCV_PATH_NAME G_DIR_SEPARATOR_S "haarcascades" \
     G_DIR_SEPARATOR_S "haarcascade_frontalface_default.xml"
 #define DEFAULT_SCALE_FACTOR 1.25
+#if (CV_MAJOR_VERSION >= 4)
+#define DEFAULT_FLAGS CASCADE_DO_CANNY_PRUNING
+#else
 #define DEFAULT_FLAGS CV_HAAR_DO_CANNY_PRUNING
+#endif
 #define DEFAULT_MIN_NEIGHBORS 3
 #define DEFAULT_MIN_SIZE_WIDTH 30
 #define DEFAULT_MIN_SIZE_HEIGHT 30
index db6a1c4..3d7eea1 100644 (file)
@@ -82,6 +82,9 @@ using namespace std;
 
 #include "gstfacedetect.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 GST_DEBUG_CATEGORY_STATIC (gst_face_detect_debug);
 #define GST_CAT_DEFAULT gst_face_detect_debug
@@ -94,7 +97,11 @@ GST_DEBUG_CATEGORY_STATIC (gst_face_detect_debug);
 #define DEFAULT_MOUTH_PROFILE HAAR_CASCADES_DIR "haarcascade_mcs_mouth.xml"
 #define DEFAULT_EYES_PROFILE HAAR_CASCADES_DIR "haarcascade_mcs_eyepair_small.xml"
 #define DEFAULT_SCALE_FACTOR 1.25
+#if (CV_MAJOR_VERSION >= 4)
+#define DEFAULT_FLAGS CASCADE_DO_CANNY_PRUNING
+#else
 #define DEFAULT_FLAGS CV_HAAR_DO_CANNY_PRUNING
+#endif
 #define DEFAULT_MIN_NEIGHBORS 3
 #define DEFAULT_MIN_SIZE_WIDTH 30
 #define DEFAULT_MIN_SIZE_HEIGHT 30
index 25d9fd2..101d955 100644 (file)
@@ -86,6 +86,9 @@
 
 #include "gstgrabcut.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 GST_DEBUG_CATEGORY_STATIC (gst_grabcut_debug);
 #define GST_CAT_DEFAULT gst_grabcut_debug
index 03aacd1..fb0d3df 100644 (file)
@@ -49,7 +49,9 @@
 #include <gst/video/gstvideofilter.h>
 #include <gst/opencv/gstopencvvideofilter.h>
 #include <opencv2/core.hpp>
-
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/core/types_c.h>
+#endif
 
 
 G_BEGIN_DECLS
index 062f146..d5b7ebb 100644 (file)
@@ -63,6 +63,9 @@
 /* element header */
 #include "gsthanddetect.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 GST_DEBUG_CATEGORY_STATIC (gst_handdetect_debug);
 #define GST_CAT_DEFAULT gst_handdetect_debug
@@ -410,8 +413,13 @@ gst_handdetect_transform_ip (GstOpencvVideoFilter * transform,
     Mat roi (image, Rect (filter->cvGray->origin,
             filter->cvGray->origin, filter->cvGray->width,
             filter->cvGray->height));
+#if (CV_MAJOR_VERSION >= 4)
+    filter->cvCascade_fist->detectMultiScale (roi, hands, 1.1, 2,
+        CASCADE_DO_CANNY_PRUNING, cvSize (24, 24), cvSize (0, 0));
+#else
     filter->cvCascade_fist->detectMultiScale (roi, hands, 1.1, 2,
         CV_HAAR_DO_CANNY_PRUNING, cvSize (24, 24), cvSize (0, 0));
+#endif
 
     /* if FIST gesture detected */
     if (!hands.empty ()) {
@@ -498,8 +506,13 @@ gst_handdetect_transform_ip (GstOpencvVideoFilter * transform,
       }
     } else {
       /* if NO FIST gesture, detecting PALM gesture */
+#if (CV_MAJOR_VERSION >= 4)
+      filter->cvCascade_palm->detectMultiScale (roi, hands, 1.1, 2,
+          CASCADE_DO_CANNY_PRUNING, cvSize (24, 24), cvSize (0, 0));
+#else
       filter->cvCascade_palm->detectMultiScale (roi, hands, 1.1, 2,
           CV_HAAR_DO_CANNY_PRUNING, cvSize (24, 24), cvSize (0, 0));
+#endif
       /* if PALM detected */
       if (!hands.empty ()) {
         int min_distance, distance;
index 8fa0210..7869e9e 100644 (file)
@@ -45,8 +45,6 @@
 #ifndef __GST_HANDDETECT_H__
 #define __GST_HANDDETECT_H__
 
-#include <cv.h>
-
 #include <gst/opencv/gstopencvvideofilter.h>
 /* opencv */
 #include <opencv2/core.hpp>
index 9ec0396..e2bdcbb 100644 (file)
@@ -64,6 +64,9 @@
 
 #include "gstretinex.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 GST_DEBUG_CATEGORY_STATIC (gst_retinex_debug);
 #define GST_CAT_DEFAULT gst_retinex_debug
index 17c5d2b..ba4c569 100644 (file)
@@ -92,6 +92,9 @@
 
 #include "gstsegmentation.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 GST_DEBUG_CATEGORY_STATIC (gst_segmentation_debug);
 #define GST_CAT_DEFAULT gst_segmentation_debug
index 733f23c..918a079 100644 (file)
@@ -50,7 +50,9 @@
 #include <opencv2/video.hpp>
 #include <opencv2/core.hpp>
 #include <opencv2/bgsegm.hpp>
-
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/core/types_c.h>
+#endif
 
 G_BEGIN_DECLS
 /* #defines don't like whitespacey bits */
index 4d31bc3..e4e1cf2 100644 (file)
@@ -60,6 +60,9 @@
 
 #include "gstskindetect.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 GST_DEBUG_CATEGORY_STATIC (gst_skin_detect_debug);
 #define GST_CAT_DEFAULT gst_skin_detect_debug
index 8b4dcf9..a41e69e 100644 (file)
 #include "../../gst-libs/gst/gst-i18n-plugin.h"
 #include "gsttemplatematch.h"
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#include <opencv2/imgcodecs.hpp>
+#include <opencv2/imgcodecs/legacy/constants_c.h>
+#else
 #include <opencv2/imgcodecs/imgcodecs_c.h>
+#endif
 
 GST_DEBUG_CATEGORY_STATIC (gst_template_match_debug);
 #define GST_CAT_DEFAULT gst_template_match_debug
@@ -179,7 +185,16 @@ gst_template_match_load_template (GstTemplateMatch * filter, gchar * templ)
       NULL;
 
   if (templ) {
+#if (CV_MAJOR_VERSION >= 4)
+    cv::Mat mat = cv::imread (templ);
+    newTemplateImage =
+        cvCreateImage (cvSize (mat.cols, mat.rows), cvIplDepth (mat.flags),
+        mat.channels ());
+    IplImage ipltemp = mat;
+    cvCopy (&ipltemp, newTemplateImage);
+#else
     newTemplateImage = cvLoadImage (templ, CV_LOAD_IMAGE_COLOR);
+#endif
     if (!newTemplateImage) {
       /* Unfortunately OpenCV doesn't seem to provide any way of finding out
          why the image load failed, so we can't be more specific than FAILED: */
index 080904e..c1cc299 100644 (file)
@@ -49,6 +49,9 @@
 #include <gst/opencv/gstopencvvideofilter.h>
 #include <opencv2/core.hpp>
 #include <opencv2/imgproc.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/imgproc/imgproc_c.h>
+#endif
 
 G_BEGIN_DECLS
 
index ff2fc98..a1a09b1 100644 (file)
@@ -39,6 +39,20 @@ libopencv_headers = [
   'opencv2/video.hpp',
 ]
 
+libopencv4_headers = [
+  'opencv4/opencv2/bgsegm.hpp',
+  'opencv4/opencv2/calib3d.hpp',
+  'opencv4/opencv2/core.hpp',
+  'opencv4/opencv2/imgcodecs/legacy/constants_c.h',
+  'opencv4/opencv2/imgproc/imgproc_c.h',
+  'opencv4/opencv2/core/types_c.h',
+  'opencv4/opencv2/core/core_c.h',
+  'opencv4/opencv2/imgproc.hpp',
+  'opencv4/opencv2/objdetect.hpp',
+  'opencv4/opencv2/opencv.hpp',
+  'opencv4/opencv2/video.hpp',
+]
+
 gstopencv_cargs = ['-DGST_HAAR_CASCADES_DIR="@0@"']
 
 opencv_dep = dependency('opencv', version : ['>= 3.0.0', '< 3.5.0'], required : false)
@@ -51,6 +65,17 @@ if opencv_found
       opencv_found = false
     endif
   endforeach
+else
+  opencv_dep = dependency('opencv4', version : ['>= 4.0.0', '< 4.1.0'], required : false)
+  opencv_found = opencv_dep.found()
+  if opencv_found
+    foreach h : libopencv4_headers
+      if not cxx.has_header(h)
+        message('Needed header "' + h + '" not found')
+        opencv_found = false
+      endif
+    endforeach
+  endif
 endif
 
 if opencv_found
@@ -67,7 +92,12 @@ if opencv_found
     if r.returncode() == 0
       gstopencv_cargs += '-DOPENCV_PATH_NAME="OpenCV"'
     else
-      error('Unable to detect OpenCV data directory')
+      r = run_command('test', '-d', opencv_prefix + '/share/opencv4')
+      if r.returncode() == 0
+        gstopencv_cargs += '-DOPENCV_PATH_NAME="opencv4"'
+      else
+        error('Unable to detect OpenCV data directory')
+      endif
     endif
   endif
   gstopencv = library('gstopencv',
index bb5eb94..9d90da7 100644 (file)
@@ -25,6 +25,9 @@
 
 #include "gstopencvutils.h"
 #include <opencv2/core.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/core/types_c.h>
+#endif
 
 /*
 The various opencv image containers or headers store the following information:
index 94f3b6d..7232a0a 100644 (file)
@@ -51,6 +51,9 @@
 #include "gstopencvutils.h"
 
 #include <opencv2/core.hpp>
+#if (CV_MAJOR_VERSION >= 4)
+#include <opencv2/core/core_c.h>
+#endif
 
 GST_DEBUG_CATEGORY_STATIC (gst_opencv_video_filter_debug);
 #define GST_CAT_DEFAULT gst_opencv_video_filter_debug
index 2babdec..3fd472f 100644 (file)
@@ -10,6 +10,9 @@ opencv_headers = [
 ]
 
 opencv_dep = dependency('opencv', version : '>= 3.0.0', required : get_option('opencv'))
+if not opencv_dep.found()
+  opencv_dep = dependency('opencv4', version : '>= 4.0.0', required : get_option('opencv'))
+endif
 if opencv_dep.found()
   gstopencv = library('gstopencv-' + api_version,
     opencv_sources,