From 188884f8575575c0b54c658fdb1402676e69b053 Mon Sep 17 00:00:00 2001 From: Jeroen Ooms Date: Mon, 16 Mar 2020 16:40:44 +0100 Subject: [PATCH 1/5] Try to make it build for opencv2 --- .travis.yml | 3 +++ DESCRIPTION | 2 +- configure | 2 +- src/effects.cpp | 8 ++++++++ src/face.cpp | 8 ++++++++ src/hog.cpp | 3 ++- src/util.cpp | 31 ------------------------------- src/util.hpp | 1 - 8 files changed, 23 insertions(+), 35 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4ec667a..2fbad1a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,6 +20,9 @@ matrix: - os: osx brew_packages: opencv@3 env: PKG_CONFIG_PATH="/usr/local/opt/opencv@3/lib/pkgconfig" + - os: osx + brew_packages: opencv@2 + env: PKG_CONFIG_PATH="/usr/local/opt/opencv@2/lib/pkgconfig" - os: osx brew_packages: opencv - os: osx diff --git a/DESCRIPTION b/DESCRIPTION index 5dace77..eb6990b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,7 +9,7 @@ Description: Experimenting with computer vision and machine learning in R. This such as edge, body or face detection. These can either be applied to analyze static images, or to filter live video footage from a camera device. License: MIT + file LICENSE -SystemRequirements: OpenCV: libopencv-dev (Debian, Ubuntu) or opencv-devel (Fedora) +SystemRequirements: OpenCV 3 or newer: libopencv-dev (Debian, Ubuntu) or opencv-devel (Fedora) URL: https://docs.ropensci.org/opencv, https://opencv.org (upstream) https://github.com/ropensci/opencv (devel) BugReports: https://github.com/ropensci/opencv/issues LinkingTo: Rcpp diff --git a/configure b/configure index 569dbdf..d2a3fd2 100755 --- a/configure +++ b/configure @@ -4,7 +4,7 @@ PKG_CONFIG_NAME="opencv4" PKG_CONFIG_NAME_ALT="opencv" PKG_DEB_NAME="libopencv-dev" PKG_RPM_NAME="opencv-devel" -PKG_BREW_NAME="opencv" +PKG_BREW_NAME="opencv@2" PKG_TEST_HEADER="" PKG_LIBS="-lopencv_{stitching,superres,videostab,aruco,bgsegm,bioinspired,ccalib,dnn_objdetect,\ dpm,face,photo,fuzzy,hfs,img_hash,line_descriptor,optflow,reg,rgbd,saliency,stereo,structured_light,\ diff --git a/src/effects.cpp b/src/effects.cpp index 9c3eb18..f894a03 100644 --- a/src/effects.cpp +++ b/src/effects.cpp @@ -13,13 +13,21 @@ XPtrMat cvmat_blur(XPtrMat ptr, size_t ksize){ XPtrMat cvmat_sketch(XPtrMat ptr, bool color){ Mat out1; Mat out2; +#if CV_VERSION_EPOCH < 3 + throw std::runtime_error("pencilSketch requires OpenCV 3 or newer"); +#else pencilSketch(get_mat(ptr),out1, out2, 10 , 0.1f, 0.03f); +#endif return cvmat_xptr(color ? out2 : out1); } // [[Rcpp::export]] XPtrMat cvmat_stylize(XPtrMat ptr){ Mat out; +#if CV_VERSION_EPOCH < 3 + throw std::runtime_error("stylization requires OpenCV 3 or newer"); +#else stylization(get_mat(ptr), out); +#endif return cvmat_xptr(out); } diff --git a/src/face.cpp b/src/face.cpp index 3c49b7f..ae2ec67 100644 --- a/src/face.cpp +++ b/src/face.cpp @@ -60,6 +60,9 @@ XPtrMat cvmat_facemask(XPtrMat ptr, const char * facedata){ // [[Rcpp::export]] XPtrMat cvmat_mog2(XPtrMat ptr) { +#if CV_VERSION_EPOCH < 3 + throw std::runtime_error("createBackgroundSubtractorMOG2 requires OpenCV 3 or newer"); +#else static Ptr model = createBackgroundSubtractorMOG2(); model->setVarThreshold(10); cv::Mat frame = get_mat(ptr); @@ -67,15 +70,20 @@ XPtrMat cvmat_mog2(XPtrMat ptr) { model->apply(frame, mask); //refineSegments(frame, mask, out_frame); return cvmat_xptr(mask); +#endif } // [[Rcpp::export]] XPtrMat cvmat_knn(XPtrMat ptr) { +#if CV_VERSION_EPOCH < 3 + throw std::runtime_error("createBackgroundSubtractorKNN requires OpenCV 3 or newer"); +#else static Ptr model = createBackgroundSubtractorKNN(); cv::Mat frame = get_mat(ptr); cv::Mat mask, out_frame; model->apply(frame, mask); return cvmat_xptr(mask); +#endif } // [[Rcpp::export]] diff --git a/src/hog.cpp b/src/hog.cpp index 9f3f54e..9dd18f1 100644 --- a/src/hog.cpp +++ b/src/hog.cpp @@ -35,7 +35,8 @@ XPtrMat cvmat_hog(XPtrMat ptr){ r.width = cvRound(r.width*0.8); r.y += cvRound(r.height*0.07); r.height = cvRound(r.height*0.8); - rectangle(get_mat(ptr), r.tl(), r.br(), cv::Scalar(0,255,0), 3); + Mat img = get_mat(ptr); + rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3); } return ptr; } diff --git a/src/util.cpp b/src/util.cpp index ca10c7b..7b1740a 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -89,34 +89,3 @@ void detectAndDraw( Mat img, CascadeClassifier& cascade, } //imshow( "result", img ); } - -void refineSegments(const Mat& img, Mat& mask, Mat& dst) -{ - int niters = 3; - vector > contours; - vector hierarchy; - Mat temp; - dilate(mask, temp, Mat(), Point(-1,-1), niters); - erode(temp, temp, Mat(), Point(-1,-1), niters*2); - dilate(temp, temp, Mat(), Point(-1,-1), niters); - findContours( temp, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE ); - dst = Mat::zeros(img.size(), CV_8UC3); - if( contours.size() == 0 ) - return; - // iterate through all the top-level contours, - // draw each connected component with its own random color - int idx = 0, largestComp = 0; - double maxArea = 0; - for( ; idx >= 0; idx = hierarchy[idx][0] ) - { - const vector& c = contours[idx]; - double area = fabs(contourArea(Mat(c))); - if( area > maxArea ) - { - maxArea = area; - largestComp = idx; - } - } - Scalar color( 0, 0, 255 ); - drawContours( dst, contours, largestComp, color, FILLED, LINE_8, hierarchy ); -} diff --git a/src/util.hpp b/src/util.hpp index 6f4fff9..da1c13b 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -2,7 +2,6 @@ void detectAndDraw( cv::Mat img, cv::CascadeClassifier& cascade, cv::CascadeClassifier& nestedCascade, double scale, bool tryflip ); -void refineSegments(const cv::Mat& img, cv::Mat& mask, cv::Mat& dst); cv::Mat get_mat(XPtrMat image); XPtrMat cvmat_xptr(cv::Mat *frame); XPtrMat cvmat_xptr(cv::Mat orig); From ff6e87583791be3de4129146dfe3087b53d8cc6e Mon Sep 17 00:00:00 2001 From: Jeroen Ooms Date: Mon, 16 Mar 2020 17:03:04 +0100 Subject: [PATCH 2/5] Test with legacy xenial --- .travis.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2fbad1a..2753e85 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,16 @@ pandoc: false matrix: include: + - dist: xenial # legacy opencv2 - dist: xenial # up-to-date 3.4 branch + addons: + apt: + update: true + sources: + - sourceline: 'ppa:cran/opencv' + packages: + - libopencv-dev + - opencv-data - dist: xenial # 3.2 branch (Bionic / Buster) addons: apt: @@ -29,12 +38,3 @@ matrix: osx_image: xcode7.3 disable_homebrew: true before_install: sed -i.bak 's/-isysroot /-I/g' $(R RHOME)/etc/Makeconf - -addons: - apt: - update: true - sources: - - sourceline: 'ppa:cran/opencv' - packages: - - libopencv-dev - - opencv-data From 0f8300ed46dd9925e4b8652e7d778a2d4a76b3d8 Mon Sep 17 00:00:00 2001 From: Jeroen Ooms Date: Mon, 16 Mar 2020 17:45:15 +0100 Subject: [PATCH 3/5] Fix for Xenial --- .travis.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.travis.yml b/.travis.yml index 2753e85..093e49d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,12 @@ pandoc: false matrix: include: - dist: xenial # legacy opencv2 + addons: + apt: + update: true + packages: + - libopencv-dev + - opencv-data - dist: xenial # up-to-date 3.4 branch addons: apt: From 7366b34f674236e685369effc7211dc748a69177 Mon Sep 17 00:00:00 2001 From: Jeroen Ooms Date: Mon, 16 Mar 2020 18:10:21 +0100 Subject: [PATCH 4/5] Doesn't work --- .travis.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 093e49d..f97e957 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,13 +6,6 @@ pandoc: false matrix: include: - - dist: xenial # legacy opencv2 - addons: - apt: - update: true - packages: - - libopencv-dev - - opencv-data - dist: xenial # up-to-date 3.4 branch addons: apt: From 7db4939de7e7288d2fb2f6b2f0c49272fc092a52 Mon Sep 17 00:00:00 2001 From: Jeroen Ooms Date: Mon, 16 Mar 2020 20:32:21 +0100 Subject: [PATCH 5/5] Fix check warning --- R/init.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/init.R b/R/init.R index 62ac501..528ae6a 100644 --- a/R/init.R +++ b/R/init.R @@ -30,7 +30,7 @@ is_check <- function(){ } is_mojave <- function(){ - ver <- utils::tail(strsplit(sessionInfo()$running, ' ')[[1]], 1) + ver <- utils::tail(strsplit(utils::sessionInfo()$running, ' ')[[1]], 1) as.numeric_version(ver) >= "10.14" }