编译过程中,cmake ..报错如下:
CMake Error at CMakeLists.txt:8 (find_package):
Could not find a configuration file for package "OpenCV" that is compatible
with requested version "4".
The following configuration files were considered but not accepted:
/usr/share/OpenCV/OpenCVConfig.cmake, version: 3.2.0
-- Configuring incomplete, errors occurred!
See also "/home/nanorobot/slambook2_1/ch8/build/CMakeFiles/CMakeOutput.log".
定位到这句提示:Could not find a configuration file for package "OpenCV" that is compatible
with requested version "4".
原因在于:代码中的一些函数是opencv4中的函数。因此,我们要安装opencv4。
安装opencv4步骤:
OpenCV4的安装与错误解决-linux_Canminem的博客-CSDN博客
安装opencv4后,再次执行cmake ..,提示如下,说明opencv的问题已经解决:
nanorobot@ubuntu:~/slambook2_1/ch8/build$ cmake ..
-- Found OpenCV: /usr/local (found suitable version "4.5.0", minimum required is "4")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/nanorobot/slambook2_1/ch8/build
接下来,make编译, optical_flow.cpp出现报错:
nanorobot@ubuntu:~/slambook2_1/ch8/build$ make
Scanning dependencies of target optical_flow
[ 25%] Building CXX object CMakeFiles/optical_flow.dir/optical_flow.cpp.o
/home/nanorobot/slambook2_1/ch8/optical_flow.cpp: In function ‘int main(int, char**)’:
/home/nanorobot/slambook2_1/ch8/optical_flow.cpp:143:37: error: ‘CV_GRAY2BGR’ was not declared in this scope
cv::cvtColor(img2, img2_single, CV_GRAY2BGR);
^~~~~~~~~~~
CMakeFiles/optical_flow.dir/build.make:62: recipe for target 'CMakeFiles/optical_flow.dir/optical_flow.cpp.o' failed
make[2]: *** [CMakeFiles/optical_flow.dir/optical_flow.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/optical_flow.dir/all' failed
make[1]: *** [CMakeFiles/optical_flow.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
此错误原因出在代码上,原因在于opencv4把CV_GRAY2BGR改为了COLOR_GRAY2BGR,我们定位到CV_GRAY2BGR,将CV_GRAY2BGR,更新为COLOR_GRAY2BGR(direct_method.cpp也要修改)。改正保存即可。再次编译,出现direct_method.cpp依然报错,如下:
nanorobot@ubuntu:~/slambook2_1/ch8/build$ make
Scanning dependencies of target optical_flow
[ 25%] Building CXX object CMakeFiles/optical_flow.dir/optical_flow.cpp.o
[ 50%] Linking CXX executable optical_flow
[ 50%] Built target optical_flow
Scanning dependencies of target direct_method
[ 75%] Building CXX object CMakeFiles/direct_method.dir/direct_method.cpp.o
/home/nanorobot/slambook2_1/ch8/direct_method.cpp: In function ‘void DirectPoseEstimationSingleLayer(const cv::Mat&, const cv::Mat&, const VecVector2d&, std::vector >, Sophus::SE3d&)’:
/home/nanorobot/slambook2_1/ch8/direct_method.cpp:206:35: error: ‘CV_GRAY2BGR’ was not declared in this scope
cv::cvtColor(img2, img2_show, CV_GRAY2BGR);
^~~~~~~~~~~
CMakeFiles/direct_method.dir/build.make:62: recipe for target 'CMakeFiles/direct_method.dir/direct_method.cpp.o' failed
make[2]: *** [CMakeFiles/direct_method.dir/direct_method.cpp.o] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/direct_method.dir/all' failed
make[1]: *** [CMakeFiles/direct_method.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
解决方法:
在direct_method.cpp加入:
using namespace cv;
代码片段如下:
#include
#include
#include
#include
using namespace std;
using namespace cv;
typedef vector> VecVector2d;
再次编译,问题解决。