diff --git a/Makefile b/Makefile index 2b0ad1aeba4..938bb9449f3 100644 --- a/Makefile +++ b/Makefile @@ -215,9 +215,17 @@ endif ifeq ($(USE_OPENCV), 1) LIBRARIES += opencv_core opencv_highgui opencv_imgproc - ifeq ($(OPENCV_VERSION), 3) + ifeq ($(OPENCV_VERSION), $(filter $(OPENCV_VERSION), 3 4)) LIBRARIES += opencv_imgcodecs opencv_videoio endif + ifeq ($(OPENCV_VERSION), 4) + ifeq ($(USE_PKG_CONFIG), 1) + INCLUDE_DIRS += $(shell pkg-config opencv4 --cflags-only-I | sed 's/-I//g') + else + INCLUDE_DIRS += /usr/include/opencv4 /usr/local/include/opencv4 + INCLUDE_DIRS += /usr/include/opencv4/opencv /usr/local/include/opencv4/opencv + endif + endif endif @@ -447,7 +455,11 @@ LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) USE_PKG_CONFIG ?= 0 ifeq ($(USE_PKG_CONFIG), 1) - PKG_CONFIG := $(shell pkg-config opencv --libs) + ifeq ($(OPENCV_VERSION), 4) + PKG_CONFIG := $(shell pkg-config opencv4 --libs) + else + PKG_CONFIG := $(shell pkg-config opencv --libs) + endif else PKG_CONFIG := endif diff --git a/Makefile.config.example b/Makefile.config.example index 43ef562002e..d7893954ef6 100644 --- a/Makefile.config.example +++ b/Makefile.config.example @@ -17,7 +17,7 @@ # USE_LEVELDB := 0 # USE_LMDB := 0 -# Uncomment if you're using OpenCV 3 +# Uncomment and set accordingly if you're using OpenCV 3/4 # OPENCV_VERSION := 3 # To customize your choice of compiler, uncomment and set the following. diff --git a/src/caffe/layers/video_data_layer.cpp b/src/caffe/layers/video_data_layer.cpp index 9015c20219d..6e71ae741f2 100644 --- a/src/caffe/layers/video_data_layer.cpp +++ b/src/caffe/layers/video_data_layer.cpp @@ -50,12 +50,12 @@ void VideoDataLayer::DataLayerSetUp( if (!cap_.open(video_file)) { LOG(FATAL) << "Failed to open video: " << video_file; } - total_frames_ = cap_.get(CV_CAP_PROP_FRAME_COUNT); + total_frames_ = cap_.get(cv::CAP_PROP_FRAME_COUNT); processed_frames_ = 0; // Read image to infer shape. cap_ >> cv_img; // Set index back to the first frame. - cap_.set(CV_CAP_PROP_POS_FRAMES, 0); + cap_.set(cv::CAP_PROP_POS_FRAMES, 0); } else { LOG(FATAL) << "Unknow video type!"; } diff --git a/src/caffe/layers/window_data_layer.cpp b/src/caffe/layers/window_data_layer.cpp index bd8780171b0..73f35922ca1 100644 --- a/src/caffe/layers/window_data_layer.cpp +++ b/src/caffe/layers/window_data_layer.cpp @@ -275,7 +275,7 @@ bool WindowDataLayer::load_batch(Batch* batch, image_database_cache_[window[WindowDataLayer::IMAGE_INDEX]]; DecodeDatumToCVMat(image_cached.second, true, cv_img, false); } else { - cv_img = cv::imread(image.first, CV_LOAD_IMAGE_COLOR); + cv_img = cv::imread(image.first, cv::IMREAD_COLOR); if (!cv_img.data) { LOG(ERROR) << "Could not open or find file " << image.first; return true; diff --git a/src/caffe/test/test_io.cpp b/src/caffe/test/test_io.cpp index dc86da56951..72246f5b3aa 100644 --- a/src/caffe/test/test_io.cpp +++ b/src/caffe/test/test_io.cpp @@ -15,8 +15,8 @@ class IOTest : public ::testing::Test {}; bool ReadImageToDatumReference(const string& filename, const int label, const int height, const int width, const bool is_color, Datum* datum) { cv::Mat cv_img; - int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR : - CV_LOAD_IMAGE_GRAYSCALE); + int cv_read_flag = (is_color ? cv::IMREAD_COLOR : + cv::IMREAD_GRAYSCALE); cv::Mat cv_img_origin = cv::imread(filename, cv_read_flag); if (!cv_img_origin.data) { diff --git a/src/caffe/util/bbox_util.cpp b/src/caffe/util/bbox_util.cpp index a9e7408d9db..5e060e40477 100644 --- a/src/caffe/util/bbox_util.cpp +++ b/src/caffe/util/bbox_util.cpp @@ -2236,7 +2236,7 @@ void VisualizeBBox(const vector& images, const Blob* detections, &baseline); cv::rectangle(image, cv::Point(0, 0), cv::Point(text.width, text.height + baseline), - CV_RGB(255, 255, 255), CV_FILLED); + CV_RGB(255, 255, 255), cv::FILLED); cv::putText(image, buffer, cv::Point(0, text.height + baseline / 2.), fontface, scale, CV_RGB(0, 0, 0), thickness, 8); // Draw bboxes. @@ -2262,7 +2262,7 @@ void VisualizeBBox(const vector& images, const Blob* detections, cv::rectangle( image, bottom_left_pt + cv::Point(0, 0), bottom_left_pt + cv::Point(text.width, -text.height-baseline), - color, CV_FILLED); + color, cv::FILLED); cv::putText(image, buffer, bottom_left_pt - cv::Point(0, baseline), fontface, scale, CV_RGB(0, 0, 0), thickness, 8); } @@ -2271,7 +2271,7 @@ void VisualizeBBox(const vector& images, const Blob* detections, if (!save_file.empty()) { if (!cap_out.isOpened()) { cv::Size size(image.size().width, image.size().height); - cv::VideoWriter outputVideo(save_file, CV_FOURCC('D', 'I', 'V', 'X'), + cv::VideoWriter outputVideo(save_file, cv::VideoWriter::fourcc('D', 'I', 'V', 'X'), 30, size, true); cap_out = outputVideo; } diff --git a/src/caffe/util/im_transforms.cpp b/src/caffe/util/im_transforms.cpp index 2f179bc4ff0..1ec7af0e483 100644 --- a/src/caffe/util/im_transforms.cpp +++ b/src/caffe/util/im_transforms.cpp @@ -1,6 +1,6 @@ #include -#if CV_VERSION_MAJOR == 3 +#if CV_VERSION_MAJOR == 3 || CV_VERSION_MAJOR == 4 #include #define CV_GRAY2BGR cv::COLOR_GRAY2BGR #define CV_BGR2GRAY cv::COLOR_BGR2GRAY @@ -11,6 +11,11 @@ #define CV_THRESH_BINARY_INV cv::THRESH_BINARY_INV #define CV_THRESH_OTSU cv::THRESH_OTSU #endif +#if CV_VERSION_MAJOR == 4 +#define CV_BGR2HSV cv::COLOR_BGR2HSV +#define CV_HSV2BGR cv::COLOR_HSV2BGR +#define CV_BGR2Lab cv::COLOR_BGR2Lab +#endif #include #include diff --git a/src/caffe/util/io.cpp b/src/caffe/util/io.cpp index bc4b1961271..537f1417d52 100644 --- a/src/caffe/util/io.cpp +++ b/src/caffe/util/io.cpp @@ -196,7 +196,7 @@ cv::Mat ReadImageToCVMat(const string& filename, } if (cv_img_origin.data) { if (is_color && cv_img_origin.channels() < 3) { - cv::cvtColor(cv_img_origin, cv_img, CV_GRAY2RGB); + cv::cvtColor(cv_img_origin, cv_img, cv::COLOR_GRAY2RGB); } if (short_side > 0) { if (cv_img_origin.rows > cv_img_origin.cols) { @@ -212,10 +212,10 @@ cv::Mat ReadImageToCVMat(const string& filename, } cv::Size sz(width, height); if (cv_img.data) { - cv::resize(cv_img, cv_img_origin, sz, 0., 0., CV_INTER_LINEAR); + cv::resize(cv_img, cv_img_origin, sz, 0., 0., cv::INTER_LINEAR); return cv_img_origin; } - cv::resize(cv_img_origin, cv_img, sz, 0., 0., CV_INTER_LINEAR); + cv::resize(cv_img_origin, cv_img, sz, 0., 0., cv::INTER_LINEAR); } else { LOG(ERROR) << "Could not decode file " << filename; } @@ -226,8 +226,8 @@ cv::Mat ReadImageToCVMat(const string& filename, const int height, const int width, const int min_dim, const int max_dim, const bool is_color) { cv::Mat cv_img; - int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR : - CV_LOAD_IMAGE_GRAYSCALE); + int cv_read_flag = (is_color ? cv::IMREAD_COLOR : + cv::IMREAD_GRAYSCALE); cv::Mat cv_img_origin = cv::imread(filename, cv_read_flag); if (!cv_img_origin.data) { LOG(ERROR) << "Could not open or find file " << filename;