Skip to content

Commit

Permalink
opencv4 support for msys2 and all linux platforms where available (#6252
Browse files Browse the repository at this point in the history
)
  • Loading branch information
oxillo authored and arturoc committed Mar 15, 2019
1 parent 4ca4f6e commit 529ffd8
Show file tree
Hide file tree
Showing 16 changed files with 246 additions and 29 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
ADDONS
------
### ofxOpenCv
- added support for OpenCV4 : deprecated C functions replaced by their C++ counterpart. Also fix issue due to incorrect pkg-config package [commit](https://github.com/openframeworks/openFrameworks/commit/)

--------------------------------------

```
.----. .---. .----. .---.
/ .. \ /_ | / .. \ /_ |
Expand Down
27 changes: 10 additions & 17 deletions addons/ofxOpenCv/addon_config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ common:
# ADDON_LIBS_EXCLUDE =

linux64:
ADDON_PKG_CONFIG_LIBRARIES = opencv harfbuzz
ADDON_PKG_CONFIG_LIBRARIES = opencv4 harfbuzz
ADDON_LIBS_EXCLUDE = libs/opencv/%
ADDON_INCLUDES_EXCLUDE = libs/opencv
ADDON_INCLUDES_EXCLUDE += libs/opencv/%
Expand All @@ -79,67 +79,60 @@ linuxarmv6l:
ADDON_INCLUDES_EXCLUDE += libs/opencv/%

linuxarmv7l:
ADDON_PKG_CONFIG_LIBRARIES = opencv
ADDON_PKG_CONFIG_LIBRARIES = opencv4
ADDON_LIBS_EXCLUDE = libs/opencv/%
ADDON_INCLUDES_EXCLUDE = libs/opencv
ADDON_INCLUDES_EXCLUDE += libs/opencv/%

msys2:
ADDON_PKG_CONFIG_LIBRARIES = opencv
ADDON_PKG_CONFIG_LIBRARIES = opencv4
ADDON_LIBS_EXCLUDE = libs/opencv/%
ADDON_INCLUDES_EXCLUDE = libs/opencv
ADDON_INCLUDES_EXCLUDE += libs/opencv/%

android/x86:
ADDON_LIBS =
ADDON_LIBS += libs/opencv/lib/android/x86/libopencv_shape.a
ADDON_LIBS += libs/opencv/lib/android/x86/libopencv_dnn.a
ADDON_LIBS += libs/opencv/lib/android/x86/libopencv_photo.a
ADDON_LIBS += libs/opencv/lib/android/x86/libopencv_superres.a
ADDON_LIBS += libs/opencv/lib/android/x86/libopencv_stitching.a
ADDON_LIBS += libs/opencv/lib/android/x86/libopencv_calib3d.a
ADDON_LIBS += libs/opencv/lib/android/x86/libopencv_features2d.a
ADDON_LIBS += libs/opencv/lib/android/x86/libopencv_objdetect.a
ADDON_LIBS += libs/opencv/lib/android/x86/libopencv_videostab.a
ADDON_LIBS += libs/opencv/lib/android/x86/libopencv_videoio.a
ADDON_LIBS += libs/opencv/lib/android/x86/libopencv_video.a
ADDON_LIBS += libs/opencv/lib/android/x86/libopencv_imgproc.a
ADDON_LIBS += libs/opencv/lib/android/x86/libopencv_ml.a
ADDON_LIBS += libs/opencv/lib/android/x86/libopencv_core.a
ADDON_LIBS += libs/opencv/lib/android/x86/libopencv_flann.a
ADDON_LIBS += libs/opencv/lib/android/x86/libopencv_contrib.a

android/armeabi-v7a:
ADDON_LIBS =
ADDON_LIBS += libs/opencv/lib/android/armeabi-v7a/libopencv_shape.a
ADDON_LIBS += libs/opencv/lib/android/armeabi-v7a/libopencv_dnn.a
ADDON_LIBS += libs/opencv/lib/android/armeabi-v7a/libopencv_photo.a
ADDON_LIBS += libs/opencv/lib/android/armeabi-v7a/libopencv_superres.a
ADDON_LIBS += libs/opencv/lib/android/armeabi-v7a/libopencv_stitching.a
ADDON_LIBS += libs/opencv/lib/android/armeabi-v7a/libopencv_calib3d.a
ADDON_LIBS += libs/opencv/lib/android/armeabi-v7a/libopencv_features2d.a
ADDON_LIBS += libs/opencv/lib/android/armeabi-v7a/libopencv_objdetect.a
ADDON_LIBS += libs/opencv/lib/android/armeabi-v7a/libopencv_videostab.a
ADDON_LIBS += libs/opencv/lib/android/armeabi-v7a/libopencv_videoio.a
ADDON_LIBS += libs/opencv/lib/android/armeabi-v7a/libopencv_video.a
ADDON_LIBS += libs/opencv/lib/android/armeabi-v7a/libopencv_imgproc.a
ADDON_LIBS += libs/opencv/lib/android/armeabi-v7a/libopencv_ml.a
ADDON_LIBS += libs/opencv/lib/android/armeabi-v7a/libopencv_core.a
ADDON_LIBS += libs/opencv/lib/android/armeabi-v7a/libopencv_flann.a
ADDON_LIBS += libs/opencv/lib/android/armeabi-v7a/libopencv_contrib.a

emscripten:
ADDON_LIBS =
ADDON_LIBS += libs/opencv/lib/emscripten/libopencv_shape.a
ADDON_LIBS += libs/opencv/lib/emscripten/libopencv_dnn.a
ADDON_LIBS += libs/opencv/lib/emscripten/libopencv_photo.a
ADDON_LIBS += libs/opencv/lib/emscripten/libopencv_superres.a
ADDON_LIBS += libs/opencv/lib/emscripten/libopencv_stitching.a
ADDON_LIBS += libs/opencv/lib/emscripten/libopencv_calib3d.a
ADDON_LIBS += libs/opencv/lib/emscripten/libopencv_features2d.a
ADDON_LIBS += libs/opencv/lib/emscripten/libopencv_objdetect.a
ADDON_LIBS += libs/opencv/lib/emscripten/libopencv_videostab.a
ADDON_LIBS += libs/opencv/lib/emscripten/libopencv_video.a
ADDON_LIBS += libs/opencv/lib/emscripten/libopencv_imgproc.a
ADDON_LIBS += libs/opencv/lib/emscripten/libopencv_ml.a
ADDON_LIBS += libs/opencv/lib/emscripten/libopencv_core.a
ADDON_LIBS += libs/opencv/lib/emscripten/libopencv_flann.a
ADDON_LIBS += libs/opencv/lib/emscripten/libopencv_contrib.a
ADDON_LIBS += libs/opencv/lib/emscripten/liblibprotobuf.a
ADDON_LIBS += libs/opencv/lib/emscripten/libquirc.a
ADDON_LIBS += libs/opencv/lib/emscripten/libzlib.a


Expand Down
11 changes: 9 additions & 2 deletions addons/ofxOpenCv/src/ofxCvConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
#undef MAX
#endif

#include "cv.h"
#include "opencv2/opencv.hpp"
#include "opencv2/core/version.hpp"
#if CV_MAJOR_VERSION < 4
#include "cv.h"
#define USE_OLD_CV
#else
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#endif

#include <vector>
#include "ofMain.h"

Expand Down
95 changes: 93 additions & 2 deletions addons/ofxOpenCv/src/ofxCvHaarFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,29 @@ static bool sort_carea_compare( const ofxCvBlob & a, const ofxCvBlob & b) {
}

ofxCvHaarFinder::ofxCvHaarFinder() {
#ifdef USE_OLD_CV
cascade = NULL;
#endif
scaleHaar = 1.08;
neighbors = 2;
img.setUseTexture(false);
}

ofxCvHaarFinder::ofxCvHaarFinder(const ofxCvHaarFinder& finder) {
#ifdef USE_OLD_CV
cascade = NULL;
#endif
scaleHaar = finder.scaleHaar;
neighbors = finder.neighbors;
img.setUseTexture(false);
setup(finder.haarFile);
}

ofxCvHaarFinder::~ofxCvHaarFinder() {
#ifdef USE_OLD_CV
if(cascade != NULL)
cvReleaseHaarClassifierCascade(&cascade);
#endif
}

// low values - more accurate - eg: 1.01
Expand All @@ -44,12 +50,13 @@ void ofxCvHaarFinder::setNeighbors(unsigned neighbors) {
}

void ofxCvHaarFinder::setup(std::string haarFile) {
if(cascade != NULL)
cvReleaseHaarClassifierCascade(&cascade);

this->haarFile = haarFile;

haarFile = ofToDataPath(haarFile);
#ifdef USE_OLD_CV
if(cascade != NULL)
cvReleaseHaarClassifierCascade(&cascade);
cascade = (CvHaarClassifierCascade*) cvLoad(haarFile.c_str(), 0, 0, 0);

#ifdef HAAR_HACK
Expand All @@ -65,6 +72,11 @@ void ofxCvHaarFinder::setup(std::string haarFile) {

if (!cascade)
ofLogError("ofxCvHaarFinder") << "setup(): couldn't load Haar cascade file: \"" << haarFile << "\"";
#else
cascade.load( haarFile );
if( cascade.empty() )
ofLogError("ofxCvHaarFinder") << "setup(): couldn't load Haar cascade file: \"" << haarFile << "\"";
#endif //USE_OLD_CV
}


Expand Down Expand Up @@ -161,6 +173,7 @@ int ofxCvHaarFinder::findHaarObjects(const ofxCvGrayscaleImage& input,

int nHaarResults = 0;

#ifdef USE_OLD_CV
if (cascade) {
if (!blobs.empty())
blobs.clear();
Expand Down Expand Up @@ -243,5 +256,83 @@ int ofxCvHaarFinder::findHaarObjects(const ofxCvGrayscaleImage& input,
cvReleaseMemStorage(&storage);
}

#else
if( cascade.empty() )
return 0;

// we make a copy of the input image here
// because we need to equalize it.

if (img.width == input.width && img.height == input.height) {
img.resetROI();
img = input;
} else {
img.clear();
img.allocate(input.width, input.height);
img = input;
}

img.setROI(x, y, w, h);
cvEqualizeHist(img.getCvImage(), img.getCvImage());

/*
Alternative modes:
cv::CASCADE_DO_CANNY_PRUNING
Regions without edges are ignored.
cv::CASCADE_SCALE_IMAGE
Scale the image rather than the detector
(sometimes yields speed increases).
cv::CASCADE_FIND_BIGGEST_OBJECT
Only return the largest result.
cv::CASCADE_DO_ROUGH_SEARCH
When BIGGEST_OBJECT is enabled, stop at
the first scale for which multiple results
are found.
*/

std::vector<cv::Rect> haarResults;
cascade.detectMultiScale(cv::cvarrToMat(img.getCvImage()), haarResults, scaleHaar, neighbors, cv::CASCADE_DO_CANNY_PRUNING,
cv::Size(minWidth, minHeight) );

nHaarResults = haarResults.size();

for (int i = 0; i < nHaarResults; i++ ) {
//ofLogNotice("ofxCvHaarFinder") << "findHaarObjects(): " << i << " objects";

ofxCvBlob blob;

cv::Rect r = haarResults[i];

float area = r.width * r.height;
float length = (r.width * 2) + (r.height * 2);
float centerx = (r.x) + (r.width / 2.0);
float centery = (r.y) + (r.height / 2.0);

blob.area = fabs(area);
blob.hole = area < 0 ? true : false;
blob.length = length;
blob.boundingRect.x = r.x + x;
blob.boundingRect.y = r.y + y;
blob.boundingRect.width = r.width;
blob.boundingRect.height = r.height;
blob.centroid.x = centerx;
blob.centroid.y = centery;
blob.pts.push_back(ofPoint(r.x, r.y));
blob.pts.push_back(ofPoint(r.x + r.width, r.y));
blob.pts.push_back(ofPoint(r.x + r.width, r.y + r.height));
blob.pts.push_back(ofPoint(r.x, r.y + r.height));

blobs.push_back(blob);
}

// sort the pointers based on size
if( blobs.size() > 1 ) {
sort( blobs.begin(), blobs.end(), sort_carea_compare );
}
#endif // USE_OLD_CV
return nHaarResults;
}
4 changes: 4 additions & 0 deletions addons/ofxOpenCv/src/ofxCvHaarFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ class ofxCvHaarFinder {
void draw(float x, float y);

protected:
#ifdef USE_OLD_CV
CvHaarClassifierCascade* cascade;
#else
cv::CascadeClassifier cascade;
#endif //USE_OLD_CV
std::string haarFile;
ofxCvGrayscaleImage img;
float scaleHaar;
Expand Down
8 changes: 8 additions & 0 deletions addons/ofxOpenCv/src/ofxCvImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,15 @@ void ofxCvImage::undistort( float radialDistX, float radialDistY,
float distortionCoeffs[] = { radialDistX, radialDistY, tangentDistX, tangentDistY };
CvMat _a = cvMat( 3, 3, CV_32F, (void*)camIntrinsics );
CvMat _k = cvMat( 4, 1, CV_32F, (void*)distortionCoeffs );
#ifdef USE_OLD_CV
cvUndistort2( cvImage, cvImageTemp, &_a, &_k, 0 );
#else
cv::Mat src = cv::cvarrToMat(cvImage), dst = cv::cvarrToMat(cvImageTemp);
cv::Mat A = cv::cvarrToMat(&_a), distCoeffs = cv::cvarrToMat(&_k);

CV_Assert( src.size() == dst.size() && src.type() == dst.type() );
cv::undistort( src, dst, A, distCoeffs );
#endif // USE_OLD_CV
swapTemp();
flagImageChanged();
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/ci/ios/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ echo "Building openFrameworks - iOS Template Project"
ROOT=${TRAVIS_BUILD_DIR:-"$( cd "$(dirname "$0")/../../.." ; pwd -P )"}
source $ROOT/scripts/ci/ccache.sh

xcodebuild -project "$ROOT/scripts/templates/ios/emptyExample.xcodeproj" -target emptyExample -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO
xcodebuild -project "$ROOT/scripts/templates/ios/emptyExample.xcodeproj" -target emptyExample -sdk 'iphoneos8.0' ARCHS='armv7'
1 change: 1 addition & 0 deletions scripts/ci/linuxarmv7l/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ createArchImg(){
tar xzf ~/ArchLinuxARM-rpi-2-latest.tar.gz --no-same-owner -C ~/archlinux/ 2>&1 >/dev/null | grep -v "tar: Ignoring unknown extended header keyword"
sed -i s_/etc/pacman_$HOME/archlinux/etc/pacman_g ~/archlinux/etc/pacman.conf
sed -i "s/Required DatabaseOptional/Never/g" ~/archlinux/etc/pacman.conf
pacman -Sy archlinux-keyring && pacman -Syyu
pacman --noconfirm -S ccache
pacman --noconfirm -r ~/archlinux/ --config ~/archlinux/etc/pacman.conf --arch=armv7h -Syu
pacman --noconfirm -r ~/archlinux/ --config ~/archlinux/etc/pacman.conf --arch=armv7h -S \
Expand Down
13 changes: 13 additions & 0 deletions scripts/linux/archlinux/install_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,16 @@ if [ $exit_code != 0 ]; then
echo "error installing packages, there could be an error with your internet connection"
exit $exit_code
fi

# Update addon_config.mk files to use OpenCV 3 or 4 depending on what's installed
addons_dir="$(readlink -f "$ROOT/../../../addons")"
$(pkg-config opencv4 --exists)
exit_code=$?
if [ $exit_code != 0 ]; then
echo "Updating ofxOpenCV to use openCV3"
sed -i -E 's/ADDON_PKG_CONFIG_LIBRARIES =(.*)opencv4(.*)$/ADDON_PKG_CONFIG_LIBRARIES =\1opencv\2/' "$addons_dir/ofxOpenCv/addon_config.mk"
else
echo "Updating ofxOpenCV to use openCV4"
sed -i -E 's/ADDON_PKG_CONFIG_LIBRARIES =(.*)opencv\s/ADDON_PKG_CONFIG_LIBRARIES =\1opencv4 /g' "$addons_dir/ofxOpenCv/addon_config.mk"
sed -i -E 's/ADDON_PKG_CONFIG_LIBRARIES =(.*)opencv$/ADDON_PKG_CONFIG_LIBRARIES =\1opencv4/g' "$addons_dir/ofxOpenCv/addon_config.mk"
fi
13 changes: 13 additions & 0 deletions scripts/linux/archlinux_armv7/install_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,16 @@ if [ $exit_code != 0 ]; then
echo "error installing packages, there could be an error with your internet connection"
exit $exit_code
fi

# Update addon_config.mk files to use OpenCV 3 or 4 depending on what's installed
addons_dir="$(readlink -f "$ROOT/../../../addons")"
$(pkg-config opencv4 --exists)
exit_code=$?
if [ $exit_code != 0 ]; then
echo "Updating ofxOpenCV to use openCV3"
sed -i -E 's/ADDON_PKG_CONFIG_LIBRARIES =(.*)opencv4(.*)$/ADDON_PKG_CONFIG_LIBRARIES =\1opencv\2/' "$addons_dir/ofxOpenCv/addon_config.mk"
else
echo "Updating ofxOpenCV to use openCV4"
sed -i -E 's/ADDON_PKG_CONFIG_LIBRARIES =(.*)opencv\s/ADDON_PKG_CONFIG_LIBRARIES =\1opencv4 /g' "$addons_dir/ofxOpenCv/addon_config.mk"
sed -i -E 's/ADDON_PKG_CONFIG_LIBRARIES =(.*)opencv$/ADDON_PKG_CONFIG_LIBRARIES =\1opencv4/g' "$addons_dir/ofxOpenCv/addon_config.mk"
fi
17 changes: 15 additions & 2 deletions scripts/linux/chip/install_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,28 @@ if [ $GCC_MAJOR_GT_4 -eq 1 ]; then
echo "It seems you are running gcc 5 or later, due to incomatible ABI with previous versions"
echo "we need to recompile poco. This will take a while"
read -p "Press any key to continue... " -n1 -s

sys_cores=$(getconf _NPROCESSORS_ONLN)
if [ $sys_cores -gt 1 ]; then
cores=$(($sys_cores-1))
else
cores=1
fi

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd ${DIR}/../../apothecary/apothecary
./apothecary -j${cores} update poco
fi

# Update addon_config.mk files to use OpenCV 3 or 4 depending on what's installed
addons_dir="$(readlink -f "$ROOT/../../../addons")"
$(pkg-config opencv4 --exists)
exit_code=$?
if [ $exit_code != 0 ]; then
echo "Updating ofxOpenCV to use openCV3"
sed -i -E 's/ADDON_PKG_CONFIG_LIBRARIES =(.*)opencv4(.*)$/ADDON_PKG_CONFIG_LIBRARIES =\1opencv\2/' "$addons_dir/ofxOpenCv/addon_config.mk"
else
echo "Updating ofxOpenCV to use openCV4"
sed -i -E 's/ADDON_PKG_CONFIG_LIBRARIES =(.*)opencv\s/ADDON_PKG_CONFIG_LIBRARIES =\1opencv4 /g' "$addons_dir/ofxOpenCv/addon_config.mk"
sed -i -E 's/ADDON_PKG_CONFIG_LIBRARIES =(.*)opencv$/ADDON_PKG_CONFIG_LIBRARIES =\1opencv4/g' "$addons_dir/ofxOpenCv/addon_config.mk"
fi
12 changes: 12 additions & 0 deletions scripts/linux/debian/install_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,15 @@ echo "If you are running a version of debian greated than 8 OF needs to install
apt-get install libpoco-dev
cp $ROOT/../extra/poco_config.mk $ROOT/../../../addons/ofxPoco/addon_config.mk

# Update addon_config.mk files to use OpenCV 3 or 4 depending on what's installed
addons_dir="$(readlink -f "$ROOT/../../../addons")"
$(pkg-config opencv4 --exists)
exit_code=$?
if [ $exit_code != 0 ]; then
echo "Updating ofxOpenCV to use openCV3"
sed -i -E 's/ADDON_PKG_CONFIG_LIBRARIES =(.*)opencv4(.*)$/ADDON_PKG_CONFIG_LIBRARIES =\1opencv\2/' "$addons_dir/ofxOpenCv/addon_config.mk"
else
echo "Updating ofxOpenCV to use openCV4"
sed -i -E 's/ADDON_PKG_CONFIG_LIBRARIES =(.*)opencv\s/ADDON_PKG_CONFIG_LIBRARIES =\1opencv4 /g' "$addons_dir/ofxOpenCv/addon_config.mk"
sed -i -E 's/ADDON_PKG_CONFIG_LIBRARIES =(.*)opencv$/ADDON_PKG_CONFIG_LIBRARIES =\1opencv4/g' "$addons_dir/ofxOpenCv/addon_config.mk"
fi
Loading

0 comments on commit 529ffd8

Please sign in to comment.