Skip to content

Commit

Permalink
fix compilation with OpenCV3
Browse files Browse the repository at this point in the history
  • Loading branch information
vrabaud committed Apr 10, 2016
1 parent f14a6db commit f316dde
Show file tree
Hide file tree
Showing 25 changed files with 271 additions and 178 deletions.
16 changes: 14 additions & 2 deletions cells/cv_bp/opencv/cv_highgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <boost/python/dict.hpp>
#include <boost/python/tuple.hpp>

#include <iostream>
#include <fstream>
#include <opencv2/highgui/highgui.hpp>
#if CV_MAJOR_VERSION == 2 && CV_MINOR_VERSION == 4 && CV_SUBMINOR_VERSION < 10
#include <cv_backports/imshow.hpp>
Expand All @@ -13,12 +11,17 @@ namespace cv_backports {
using cv::destroyWindow;
using cv::imshow;
using cv::namedWindow;
using cv::setMouseCallback;
using cv::setWindowProperty;
using cv::startWindowThread;
using cv::waitKey;
}
#endif

#include <fstream>
#include <iostream>
#include <map>

namespace bp = boost::python;

namespace
Expand Down Expand Up @@ -70,8 +73,13 @@ namespace opencv_wrappers
VideoCapture_.def(bp::init<>());
VideoCapture_.def(bp::init<std::string>());
VideoCapture_.def(bp::init<int>());
#if CV_MAJOR_VERSION == 3
typedef bool(cv::VideoCapture::*open_1)(const cv::String&);
typedef bool(cv::VideoCapture::*open_2)(int);
#else
typedef bool(cv::VideoCapture::*open_1)(const std::string&);
typedef bool(cv::VideoCapture::*open_2)(int);
#endif
VideoCapture_.def("open", open_1(&cv::VideoCapture::open));
VideoCapture_.def("open", open_2(&cv::VideoCapture::open));
VideoCapture_.def("isOpened", &cv::VideoCapture::isOpened);
Expand Down Expand Up @@ -106,7 +114,11 @@ namespace opencv_wrappers
wrap_video_writer();

//image windows
#if CV_MAJOR_VERSION == 3
bp::def("imshow", static_cast<void (*)(const cv::String&, cv::InputArray)>(cv::imshow));
#else
bp::def("imshow", cv_backports::imshow);
#endif
bp::def("waitKey", waitKey);
bp::def("namedWindow", cv_backports::namedWindow);
//CV_EXPORTS void setMouseCallback( const string& windowName, MouseCallback onMouse, void* param=0);
Expand Down
4 changes: 4 additions & 0 deletions cells/cv_bp/opencv/cv_mat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,11 @@ namespace
tostr(cv::Mat& m)
{
std::stringstream ss;
#if CV_MAJOR_VERSION == 3
ss << cv::Formatter::get(cv::Formatter::FMT_PYTHON)->format(m);
#else
cv::Formatter::get("python")->write(ss,m);
#endif
return ss.str();
}
inline cv::Mat
Expand Down
14 changes: 12 additions & 2 deletions cells/cv_bp/opencv/highgui_defines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,19 @@ namespace opencv_wrappers
opencv.attr("CV_CAP_PROP_GAIN") = int(CV_CAP_PROP_GAIN);
opencv.attr("CV_CAP_PROP_EXPOSURE") = int(CV_CAP_PROP_EXPOSURE);
opencv.attr("CV_CAP_PROP_CONVERT_RGB") = int(CV_CAP_PROP_CONVERT_RGB);
#if CV_MAJOR_VERSION > 2 || (CV_MAJOR_VERSION == 2 && (CV_MINOR_VERSION > 4 || (CV_MINOR_VERSION == 4 && CV_SUBMINOR_VERSION > 10)))
#if CV_MAJOR_VERSION == 3
opencv.attr("CV_CAP_PROP_WHITE_BALANCE_BLUE_U") = int(CV_CAP_PROP_WHITE_BALANCE_BLUE_U);
#elif CV_MAJOR_VERSION == 2 && (CV_MINOR_VERSION > 4 || (CV_MINOR_VERSION == 4 && CV_SUBMINOR_VERSION > 10))
opencv.attr("CV_CAP_PROP_WHITE_BALANCE_U") = int(CV_CAP_PROP_WHITE_BALANCE_U);
#else
opencv.attr("CV_CAP_PROP_WHITE_BALANCE_BLUE_U") = int(CV_CAP_PROP_WHITE_BALANCE_BLUE_U);
#endif
opencv.attr("CV_CAP_PROP_RECTIFICATION") = int(CV_CAP_PROP_RECTIFICATION);
#if CV_MAJOR_VERSION == 3
opencv.attr("CV_CAP_PROP_MONOCHROME") = int(CV_CAP_PROP_MONOCHROME);
#else
opencv.attr("CV_CAP_PROP_MONOCROME") = int(CV_CAP_PROP_MONOCROME);
#endif
opencv.attr("CV_CAP_PROP_SHARPNESS") = int(CV_CAP_PROP_SHARPNESS);
opencv.attr("CV_CAP_PROP_AUTO_EXPOSURE") = int(CV_CAP_PROP_AUTO_EXPOSURE);
// user can adjust refernce level
Expand All @@ -135,7 +141,9 @@ namespace opencv_wrappers
opencv.attr("CV_CAP_PROP_TEMPERATURE") = int(CV_CAP_PROP_TEMPERATURE);
opencv.attr("CV_CAP_PROP_TRIGGER") = int(CV_CAP_PROP_TRIGGER);
opencv.attr("CV_CAP_PROP_TRIGGER_DELAY") = int(CV_CAP_PROP_TRIGGER_DELAY);
#if CV_MAJOR_VERSION > 2 || (CV_MAJOR_VERSION == 2 && (CV_MINOR_VERSION > 4 || (CV_MINOR_VERSION == 4 && CV_SUBMINOR_VERSION > 10)))
#if CV_MAJOR_VERSION == 3
opencv.attr("CV_CAP_PROP_WHITE_BALANCE_RED_V") = int(CV_CAP_PROP_WHITE_BALANCE_RED_V);
#elif CV_MAJOR_VERSION > 2 || (CV_MAJOR_VERSION == 2 && (CV_MINOR_VERSION > 4 || (CV_MINOR_VERSION == 4 && CV_SUBMINOR_VERSION > 10)))
opencv.attr("CV_CAP_PROP_WHITE_BALANCE_V") = int(CV_CAP_PROP_WHITE_BALANCE_V);
#else
opencv.attr("CV_CAP_PROP_WHITE_BALANCE_RED_V") = int(CV_CAP_PROP_WHITE_BALANCE_RED_V);
Expand Down Expand Up @@ -166,8 +174,10 @@ namespace opencv_wrappers
opencv.attr("CV_CAP_OPENNI_VGA_30HZ") = int(CV_CAP_OPENNI_VGA_30HZ);
opencv.attr("CV_CAP_OPENNI_SXGA_15HZ") = int(CV_CAP_OPENNI_SXGA_15HZ);

#if CV_MAJOR_VERSION == 2
opencv.attr("CV_CAP_ANDROID_COLOR_FRAME") = int(CV_CAP_ANDROID_COLOR_FRAME);
opencv.attr("CV_CAP_ANDROID_GREY_FRAME") = int(CV_CAP_ANDROID_GREY_FRAME);
#endif
//opencv.attr("CV_CAP_ANDROID_YUV_FRAME") = int(CV_CAP_ANDROID_YUV_FRAME);
}
}
1 change: 1 addition & 0 deletions cells/opencv/calib/CircleDrawer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <ecto/ecto.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using ecto::tendrils;

Expand Down
8 changes: 7 additions & 1 deletion cells/opencv/calib/DepthTo3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>

#if CV_MAJOR_VERSION == 2
#include <opencv2/rgbd/rgbd.hpp>
using namespace cv;
#else
#include <opencv2/rgbd.hpp>
using namespace cv::rgbd;
#endif

using ecto::tendrils;
namespace calib
Expand Down Expand Up @@ -71,7 +77,7 @@ namespace calib
const cv::Mat &depth = inputs.get<cv::Mat>("depth"), &uv = inputs.get<cv::Mat>("points");

cv::Mat points3d;
cv::depthTo3dSparse(depth, K, uv, points3d);
depthTo3dSparse(depth, K, uv, points3d);

outputs["points3d"] << points3d;

Expand Down
5 changes: 4 additions & 1 deletion cells/opencv/calib/SubrectRectifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#include <opencv2/imgproc/imgproc.hpp>
#include <boost/filesystem.hpp>

#include <vector>

using namespace ecto;
using std::vector;
namespace calib
{
struct SubrectRectifier
Expand Down Expand Up @@ -64,7 +67,7 @@ struct SubrectRectifier

if (R.rows == 0 || R.cols == 0 || T.rows == 0 || T.cols == 0)
{
*output = cvCreateMat(*xsize_pixels, *ysize_pixels, image.type());
*output = cv::Mat(*xsize_pixels, *ysize_pixels, image.type());
return 0;
}

Expand Down
6 changes: 5 additions & 1 deletion cells/opencv/calib/pose_drawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

#include "calib.hpp"
#include <boost/format.hpp>

using ecto::tendrils;
using std::string;
using std::vector;

namespace calib
{
struct PoseDrawer
Expand Down Expand Up @@ -38,7 +42,7 @@ namespace calib
line(drawImage, ip[0], ip[3], c[3]);
string scaleText = boost::str(boost::format("scale %0.2f meters")%scale);
int baseline = 0;
Size sz = getTextSize(scaleText, CV_FONT_HERSHEY_SIMPLEX, 1, 1, &baseline);
Size sz = cv::getTextSize(scaleText, CV_FONT_HERSHEY_SIMPLEX, 1, 1, &baseline);
Point box_origin(10, drawImage.size().height - 10);
rectangle(drawImage, box_origin + Point(0, 5), box_origin + Point(sz.width, -sz.height - 5), Scalar::all(0), -1);
putText(drawImage, scaleText, box_origin, CV_FONT_HERSHEY_SIMPLEX, 1.0, c[0], 1, CV_AA, false);
Expand Down
1 change: 0 additions & 1 deletion cells/opencv/features2d/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ ectomodule(features2d DESTINATION ecto_opencv/ecto_cells
DescriptorExtractor.cpp
DrawKeypoints.cpp
FeatureDetector.cpp
interfaces.cpp
KeypointsToMat.cpp
LshMatcher.cpp
MatchesToMat.cpp
Expand Down
24 changes: 22 additions & 2 deletions cells/opencv/features2d/DescriptorExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>

#if CV_MAJOR_VERSION == 3
#include <opencv2/xfeatures2d.hpp>
#endif

#include "interfaces.h"

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -82,9 +86,25 @@ struct EctoDescriptorExtractor
void
configure(const tendrils& params, const tendrils& inputs, const tendrils& outputs)
{
#if CV_MAJOR_VERSION == 3
switch(T) {
case SIFT:
descriptor_extractor_ = cv::xfeatures2d::SIFT::create();
break;
case SURF:
descriptor_extractor_ = cv::xfeatures2d::SURF::create();
break;
case ORB:
descriptor_extractor_ = cv::ORB::create();
break;
case BRIEF:
descriptor_extractor_ = cv::xfeatures2d::BriefDescriptorExtractor::create();
break;
}
#else
descriptor_extractor_ = cv::DescriptorExtractor::create(descriptor_extractor_type_names[T]);

read_tendrils_as_file_node(params, boost::bind(&cv::DescriptorExtractor::read, &(*descriptor_extractor_), _1));
#endif
read_tendrils_as_file_node(params, *descriptor_extractor_);
}

int
Expand Down
20 changes: 19 additions & 1 deletion cells/opencv/features2d/FeatureDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>

#if CV_MAJOR_VERSION == 3
#include <opencv2/xfeatures2d.hpp>
#endif

#include "interfaces.h"

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -83,9 +87,23 @@ struct EctoFeatureDetector
void
configure(const tendrils& params, const tendrils& inputs, const tendrils& outputs)
{
#if CV_MAJOR_VERSION == 3
switch(T) {
case FAST:
feature_detector_ = cv::FastFeatureDetector::create();
break;
case ORB:
feature_detector_ = cv::ORB::create();
break;
case SIFT:
feature_detector_ = cv::xfeatures2d::SIFT::create();
break;
}
#else
feature_detector_ = cv::FeatureDetector::create(feature_detector_type_names[T]);
#endif

read_tendrils_as_file_node(params, boost::bind(&cv::FeatureDetector::read, &(*feature_detector_), _1));
read_tendrils_as_file_node(params, *feature_detector_);
}

int
Expand Down
34 changes: 23 additions & 11 deletions cells/opencv/features2d/ORB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
struct ORB
{
static void
declare_params(tendrils& p)
declare_params(tendrils& params)
{
p.declare<int>("n_features", "The number of desired features", 1000);
p.declare<int>("n_levels", "The number of scales", 3);
p.declare<float>("scale_factor", "The factor between scales", 1.2);
params.declare(&ORB::n_features_, "n_features", "The number of keypoints to use", 1000);
params.declare(&ORB::n_levels_, "n_levels", "The number of levels to use for ORB", 3);
params.declare(&ORB::scale_factor_, "scale_factor", "The scale factor to use for ORB", 1.2);
}

static void
Expand All @@ -29,14 +29,16 @@ struct ORB
void
configure(const tendrils& params, const tendrils& inputs, const tendrils& outputs)
{
#if (CV_MAJOR_VERSION > 2) || ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >= 4))
orb_ = cv::ORB(params.get<int>("n_features"), params.get<float>("scale_factor"), params.get<int>("n_levels"));
#if CV_MAJOR_VERSION ==3
orb_ = cv::ORB::create(*n_features_, *scale_factor_, *n_levels_);
#elif (CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >= 4)
orb = cv::Ptr<cv::ORB>(new cv::ORB(*n_features_, *scale_factor_, *n_levels_));
#else
cv::ORB::CommonParams orb_params;
orb_params.first_level_ = 0;
orb_params.n_levels_ = params.get<int>("n_levels");
orb_params.scale_factor_ = params.get<float>("scale_factor");
orb_ = cv::ORB(params.get<int>("n_features"), orb_params);
orb_params.n_levels_ = *n_levels_;
orb_params.scale_factor_ = *scale_factor_;
orb_ = cv::Ptr<cv::ORB>(new cv::ORB(*n_features_, orb_params));
#endif
}

Expand All @@ -49,7 +51,11 @@ struct ORB
inputs["image"] >> image;
inputs["mask"] >> mask;
cv::Mat desc;
orb_(image, mask, keypoints, desc, !keypoints.empty()); //use the provided keypoints if they were given.
#if CV_MAJOR_VERSION == 3
orb_->detectAndCompute(image, mask, keypoints, desc, !keypoints.empty()); //use the provided keypoints if they were given.
#else
(*orb_)(image, mask, keypoints, desc, !keypoints.empty()); //use the provided keypoints if they were given.
#endif
if (!mask.empty())
{
//need to do keypoint validation as ORB is broken.
Expand Down Expand Up @@ -80,7 +86,9 @@ struct ORB
return ecto::OK;
}

cv::ORB orb_;
ecto::spore<int> n_features_, n_levels_;
ecto::spore<float> scale_factor_;
cv::Ptr<cv::ORB> orb_;
};
struct DescriptorAccumulator
{
Expand Down Expand Up @@ -136,7 +144,11 @@ struct ORBstats
desc.pop_back(1);
for (int i = 0, end = desc.rows; i < end; i++)
{
#if CV_MAJOR_VERSION == 3
size_t distance = cv::norm(desc_i, desc.row(i), cv::NORM_HAMMING);
#else
size_t distance = cv::normHamming(desc_i.data, desc.row(i).data, desc.cols);
#endif
distances[distance]++;
}
}
Expand Down
Loading

0 comments on commit f316dde

Please sign in to comment.