Ubuntu20.04 编译 Caffe 时与 OpenCV4.X 的问题记录与解决.

Caffe - Ubuntu 安装与测试实例及问题解决 - AIUAI

Ubuntu - OpenCV 源码安装与测试 - AIUAI

参考:How to Install OpenCV on Ubuntu 20.04 - 2020.07.05

参考:Install OpenCV 4.4.0 and Caffe on Ubuntu 20.04 for Python 3

Ubuntu20.04 仓库安装 OpenCV:

sudo apt install libopencv-dev python3-opencv

查看版本:

pkg-config --modversion opencv4
# 4.2.0

问题1 - src/caffe/data_transformer.cpp:2:33: fatal error: opencv2/core/core.hpp

原因:找不到头文件.

解决方案:将 OpenCV 的头文件加入:

原:
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/
新:
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/ /usr/include/opencv4/

问题2 - error: ‘CV_INTER_CUBIC’ was not declared in this scope

问题:

src/caffe/data_transformer.cpp:781:84: error: ‘CV_INTER_CUBIC’ was not declared in this scope
     cv::resize(crop_bufferM, cv_cropped_img, cv::Size(crop_size, crop_size), 0, 0, CV_INTER_CUBIC);
                                                                                    ^~~~~~~~~~~~~~
src/caffe/data_transformer.cpp:781:84: note: suggested alternative: ‘SA_INTERRUPT’
     cv::resize(crop_bufferM, cv_cropped_img, cv::Size(crop_size, crop_size), 0, 0, CV_INTER_CUBIC);
                                                                                    ^~~~~~~~~~~~~~
                                                                                    SA_INTERRUPT
src/caffe/data_transformer.cpp: In member function ‘void caffe::DataTransformer<Dtype>::Transform(const caffe::Datum&, const caffe::Datum&, caffe::Blob<Dtype>*, caffe::Blob<Dtype>*)’:
src/caffe/data_transformer.cpp:954:51: error: ‘CV_INTER_NN’ was not declared in this scope
   cv::resize(M, M, cv::Size(width, height), 0, 0, CV_INTER_NN);
                                                   ^~~~~~~~~~~
src/caffe/data_transformer.cpp:954:51: note: suggested alternative: ‘CV_INLINE’
   cv::resize(M, M, cv::Size(width, height), 0, 0, CV_INTER_NN);
                                                   ^~~~~~~~~~~
                                                   CV_INLINE
make: *** [Makefile:592: .build_release/src/caffe/data_transformer.o] Error 1

解决方案:

src/caffe/data_transformer.cpp 中添加:include <opencv2/imgproc/imgproc_c.h>

#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/core/mat.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/imgproc_c.h> // new add
#include <opencv2/opencv.hpp>
#endif  // USE_OPENCV

问题3 - error: ‘CV_LOAD_IMAGE_COLOR’ was not declared in this scope

问题:

src/caffe/layers/window_data_layer.cpp: In member function ‘virtual void caffe::WindowDataLayer<Dtype>::load_batch(caffe::Batch<Dtype>*)’:
src/caffe/layers/window_data_layer.cpp:293:42: error: ‘CV_LOAD_IMAGE_COLOR’ was not declared in this scope
         cv_img = cv::imread(image.first, CV_LOAD_IMAGE_COLOR);
                                          ^~~~~~~~~~~~~~~~~~~
src/caffe/layers/window_data_layer.cpp:293:42: note: suggested alternative: ‘CV_IS_IMAGE_HDR’
         cv_img = cv::imread(image.first, CV_LOAD_IMAGE_COLOR);
                                          ^~~~~~~~~~~~~~~~~~~
                                          CV_IS_IMAGE_HDR
make: *** [Makefile:592: .build_release/src/caffe/layers/window_data_layer.o] Error 1

原因:OpenCV 版本问题,OpenCV4 变量名发生变动.

解决方案:

include/caffe/common.hpp 中新增如下代码(大概 70 行前后):

// Supporting OpenCV4
#if (CV_MAJOR_VERSION == 4)
#define CV_LOAD_IMAGE_COLOR cv::IMREAD_COLOR
#define CV_LOAD_IMAGE_GRAYSCALE cv::IMREAD_GRAYSCALE
#endif

//以上为新增代码(以下为原有代码,用于定位新增代码位置)
// See PR #1236
namespace cv { class Mat; }

另一种解决方案:

对报错文件进行如下修改:

CV_LOAD_IMAGE_COLOR 修改为 cv::IMREAD_COLOR
CV_LOAD_IMAGE_GRAYSCALE 修改为 cv::IMREAD_GRAYSCALE
Last modification:October 8th, 2020 at 12:34 pm