Skip to content

Commit 928023f

Browse files
authored
Fix the build for libopencv2 legacy versions (#16)
1 parent d8858c2 commit 928023f

File tree

9 files changed

+32
-45
lines changed

9 files changed

+32
-45
lines changed

.travis.yml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ pandoc: false
77
matrix:
88
include:
99
- dist: xenial # up-to-date 3.4 branch
10+
addons:
11+
apt:
12+
update: true
13+
sources:
14+
- sourceline: 'ppa:cran/opencv'
15+
packages:
16+
- libopencv-dev
17+
- opencv-data
1018
- dist: xenial # 3.2 branch (Bionic / Buster)
1119
addons:
1220
apt:
@@ -20,18 +28,12 @@ matrix:
2028
- os: osx
2129
brew_packages: opencv@3
2230
env: PKG_CONFIG_PATH="/usr/local/opt/opencv@3/lib/pkgconfig"
31+
- os: osx
32+
brew_packages: opencv@2
33+
env: PKG_CONFIG_PATH="/usr/local/opt/opencv@2/lib/pkgconfig"
2334
- os: osx
2435
brew_packages: opencv
2536
- os: osx
2637
osx_image: xcode7.3
2738
disable_homebrew: true
2839
before_install: sed -i.bak 's/-isysroot /-I/g' $(R RHOME)/etc/Makeconf
29-
30-
addons:
31-
apt:
32-
update: true
33-
sources:
34-
- sourceline: 'ppa:cran/opencv'
35-
packages:
36-
- libopencv-dev
37-
- opencv-data

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Description: Experimenting with computer vision and machine learning in R. This
99
such as edge, body or face detection. These can either be applied to analyze
1010
static images, or to filter live video footage from a camera device.
1111
License: MIT + file LICENSE
12-
SystemRequirements: OpenCV: libopencv-dev (Debian, Ubuntu) or opencv-devel (Fedora)
12+
SystemRequirements: OpenCV 3 or newer: libopencv-dev (Debian, Ubuntu) or opencv-devel (Fedora)
1313
URL: https://docs.ropensci.org/opencv, https://opencv.org (upstream) https://github.com/ropensci/opencv (devel)
1414
BugReports: https://github.com/ropensci/opencv/issues
1515
LinkingTo: Rcpp

R/init.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ is_check <- function(){
3030
}
3131

3232
is_mojave <- function(){
33-
ver <- utils::tail(strsplit(sessionInfo()$running, ' ')[[1]], 1)
33+
ver <- utils::tail(strsplit(utils::sessionInfo()$running, ' ')[[1]], 1)
3434
as.numeric_version(ver) >= "10.14"
3535
}
3636

configure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PKG_CONFIG_NAME="opencv4"
44
PKG_CONFIG_NAME_ALT="opencv"
55
PKG_DEB_NAME="libopencv-dev"
66
PKG_RPM_NAME="opencv-devel"
7-
PKG_BREW_NAME="opencv"
7+
PKG_BREW_NAME="opencv@2"
88
PKG_TEST_HEADER="<opencv2/opencv.hpp>"
99
PKG_LIBS="-lopencv_{stitching,superres,videostab,aruco,bgsegm,bioinspired,ccalib,dnn_objdetect,\
1010
dpm,face,photo,fuzzy,hfs,img_hash,line_descriptor,optflow,reg,rgbd,saliency,stereo,structured_light,\

src/effects.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,21 @@ XPtrMat cvmat_blur(XPtrMat ptr, size_t ksize){
1313
XPtrMat cvmat_sketch(XPtrMat ptr, bool color){
1414
Mat out1;
1515
Mat out2;
16+
#if CV_VERSION_EPOCH < 3
17+
throw std::runtime_error("pencilSketch requires OpenCV 3 or newer");
18+
#else
1619
pencilSketch(get_mat(ptr),out1, out2, 10 , 0.1f, 0.03f);
20+
#endif
1721
return cvmat_xptr(color ? out2 : out1);
1822
}
1923

2024
// [[Rcpp::export]]
2125
XPtrMat cvmat_stylize(XPtrMat ptr){
2226
Mat out;
27+
#if CV_VERSION_EPOCH < 3
28+
throw std::runtime_error("stylization requires OpenCV 3 or newer");
29+
#else
2330
stylization(get_mat(ptr), out);
31+
#endif
2432
return cvmat_xptr(out);
2533
}

src/face.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,30 @@ XPtrMat cvmat_facemask(XPtrMat ptr, const char * facedata){
6060

6161
// [[Rcpp::export]]
6262
XPtrMat cvmat_mog2(XPtrMat ptr) {
63+
#if CV_VERSION_EPOCH < 3
64+
throw std::runtime_error("createBackgroundSubtractorMOG2 requires OpenCV 3 or newer");
65+
#else
6366
static Ptr<BackgroundSubtractorMOG2> model = createBackgroundSubtractorMOG2();
6467
model->setVarThreshold(10);
6568
cv::Mat frame = get_mat(ptr);
6669
cv::Mat mask, out_frame;
6770
model->apply(frame, mask);
6871
//refineSegments(frame, mask, out_frame);
6972
return cvmat_xptr(mask);
73+
#endif
7074
}
7175

7276
// [[Rcpp::export]]
7377
XPtrMat cvmat_knn(XPtrMat ptr) {
78+
#if CV_VERSION_EPOCH < 3
79+
throw std::runtime_error("createBackgroundSubtractorKNN requires OpenCV 3 or newer");
80+
#else
7481
static Ptr<BackgroundSubtractorKNN> model = createBackgroundSubtractorKNN();
7582
cv::Mat frame = get_mat(ptr);
7683
cv::Mat mask, out_frame;
7784
model->apply(frame, mask);
7885
return cvmat_xptr(mask);
86+
#endif
7987
}
8088

8189
// [[Rcpp::export]]

src/hog.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ XPtrMat cvmat_hog(XPtrMat ptr){
3535
r.width = cvRound(r.width*0.8);
3636
r.y += cvRound(r.height*0.07);
3737
r.height = cvRound(r.height*0.8);
38-
rectangle(get_mat(ptr), r.tl(), r.br(), cv::Scalar(0,255,0), 3);
38+
Mat img = get_mat(ptr);
39+
rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3);
3940
}
4041
return ptr;
4142
}

src/util.cpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -89,34 +89,3 @@ void detectAndDraw( Mat img, CascadeClassifier& cascade,
8989
}
9090
//imshow( "result", img );
9191
}
92-
93-
void refineSegments(const Mat& img, Mat& mask, Mat& dst)
94-
{
95-
int niters = 3;
96-
vector<vector<Point> > contours;
97-
vector<Vec4i> hierarchy;
98-
Mat temp;
99-
dilate(mask, temp, Mat(), Point(-1,-1), niters);
100-
erode(temp, temp, Mat(), Point(-1,-1), niters*2);
101-
dilate(temp, temp, Mat(), Point(-1,-1), niters);
102-
findContours( temp, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE );
103-
dst = Mat::zeros(img.size(), CV_8UC3);
104-
if( contours.size() == 0 )
105-
return;
106-
// iterate through all the top-level contours,
107-
// draw each connected component with its own random color
108-
int idx = 0, largestComp = 0;
109-
double maxArea = 0;
110-
for( ; idx >= 0; idx = hierarchy[idx][0] )
111-
{
112-
const vector<Point>& c = contours[idx];
113-
double area = fabs(contourArea(Mat(c)));
114-
if( area > maxArea )
115-
{
116-
maxArea = area;
117-
largestComp = idx;
118-
}
119-
}
120-
Scalar color( 0, 0, 255 );
121-
drawContours( dst, contours, largestComp, color, FILLED, LINE_8, hierarchy );
122-
}

src/util.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
void detectAndDraw( cv::Mat img, cv::CascadeClassifier& cascade,
33
cv::CascadeClassifier& nestedCascade,
44
double scale, bool tryflip );
5-
void refineSegments(const cv::Mat& img, cv::Mat& mask, cv::Mat& dst);
65
cv::Mat get_mat(XPtrMat image);
76
XPtrMat cvmat_xptr(cv::Mat *frame);
87
XPtrMat cvmat_xptr(cv::Mat orig);

0 commit comments

Comments
 (0)