slambook2(ch8)—— Ubuntu18.04安装openCV4.5.2 + 排错 + 例程演示
- 一、使用OpenCV 3.2.0
- 1.修改图片路径:
- 2.修改CMakeLists.txt:
- 二、报错:By not providing "FindG2O.cmake" in CMAKE_MODULE_PATH
- 方式一:
- 方式二:(方式一对我没用!!方法二好使)
- 三、报错:(可能是OpenCV 3.2.0版本低导致的)
- 四、安装openCV4.5.2
- 1.依赖包的安装:
- 2.opencv4的安装:
- 五、例程
- 1.optical_flow
- 2.direct_method
一、使用OpenCV 3.2.0
1.修改图片路径:
string file_1 = "../LK1.png"; // first image
string file_2 = "../LK2.png"; // second image
2.修改CMakeLists.txt:
# cmake_minimum_required(VERSION 2.8)
# project(ch8)
#
# set(CMAKE_BUILD_TYPE "Release")
# add_definitions("-DENABLE_SSE")
# set(CMAKE_CXX_FLAGS "-std=c++17 ${SSE_FLAGS} -g -O3 -march=native")
# list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
# find_package(OpenCV REQUIRED)
# find_package(G2O REQUIRED)
# find_package(Sophus REQUIRED)
# find_package(Pangolin REQUIRED)
#
# include_directories(
# ${OpenCV_INCLUDE_DIRS}
# ${Sophus_INCLUDE_DIRS}
# ${G2O_INCLUDE_DIRS}
# "/usr/include/eigen3/"
# ${Pangolin_INCLUDE_DIRS}
# )
#
# add_executable(optical_flow optical_flow.cpp)
# target_link_libraries(optical_flow ${OpenCV_LIBS} fmt)
#
#
# add_executable(direct_method direct_method.cpp)
# target_link_libraries(direct_method ${OpenCV_LIBS} ${Pangolin_LIBRARIES} fmt)
二、报错:By not providing “FindG2O.cmake” in CMAKE_MODULE_PATH
在使用cmake编译中,出现了以下报错:
$ cmake ..
-- Found OpenCV: /usr/local/opencv3 (found version "3.2.0")
CMake Error at CMakeLists.txt:33 (find_package):
By not providing "FindG2O.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "G2O", but
CMake did not find one.
Could not find a package configuration file provided by "G2O" with any of
the following names:
G2OConfig.cmake
g2o-config.cmake
Add the installation prefix of "G2O" to CMAKE_PREFIX_PATH or set "G2O_DIR"
to a directory containing one of the above files. If "G2O" provides a
separate development package or SDK, be sure it has been installed.
-- Configuring incomplete, errors occurred!
See also "/home/sfann/sf_slam_temp_code/ch8/build/CMakeFiles/CMakeOutput.log".
方式一:
解决方式很简单:
主要是CMakeLists.txt上要修改几行代码
本来是这样的就会报错,
list( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake )
修改为:
list( APPEND CMAKE_MODULE_PATH /home/×××/g2o/cmake_modules )
set(G2O_ROOT /usr/local/include/g2o)
然后再cmake便不会出现问题了。
主要原因就是原始代码中
${PROJECT_SOURCE_DIR}/cmake
是ch8中的文件含有的cmake文件夹中FindG2O.cmake在你的文件夹不存在,只要把list那行路径改到FindG2O.cmake存在的文件夹就行。
方式二:(方式一对我没用!!方法二好使)
解决办法:找到findg2o.cmake文件的位置,一般在g2o安装包中的"cmake_modules"文件夹中,将整个文件夹复制到相应工程的下面,与build文件夹在同一目录,然后编译可以正常通过。
tar -czvf test.tar.gz a.c //压缩 a.c文件为test.tar.gz
tar -xzvf test.tar.gz //解压文件
cd g2o/ #进入g2o安装目录找到cmake_modules目录
tar -czvf cmake_modules.tar.gz cmake_modules #打包压缩为cmake_modules.tar.gz
cp cmake_modules.tar.gz -rf ../../ch8 #复制到build同一目录下
cd ../../ch8
tar -xzvf cmake_modules.tar.gz #解压
三、报错:(可能是OpenCV 3.2.0版本低导致的)
$ make
Scanning dependencies of target optical_flow
[ 25%] Building CXX object CMakeFiles/optical_flow.dir/optical_flow.cpp.o
/home/sfann/sf_slam_temp_code/ch8/optical_flow.cpp: In function ‘void OpticalFlowSingleLevel(const cv::Mat&, const cv::Mat&, const std::vector<cv::KeyPoint>&, std::vector<cv::KeyPoint>&, std::vector<bool>&, bool, bool)’:
/home/sfann/sf_slam_temp_code/ch8/optical_flow.cpp:190:28: error: invalid initialization of reference of type ‘const cv::ParallelLoopBody&’ from expression of type ‘std::_Bind_helper<false, void (OpticalFlowTracker::*)(const cv::Range&), OpticalFlowTracker*, const std::_Placeholder<1>&>::type {aka std::_Bind<void (OpticalFlowTracker::*(OpticalFlowTracker*, std::_Placeholder<1>))(const cv::Range&)>}’
std::bind(&OpticalFlowTracker::calculateOpticalFlow, &tracker, placeholders::_1));
~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/local/opencv3/include/opencv2/core.hpp:3216:0,
from /usr/local/opencv3/include/opencv2/opencv.hpp:52,
from /home/sfann/sf_slam_temp_code/ch8/optical_flow.cpp:5:
/usr/local/opencv3/include/opencv2/core/utility.hpp:478:17: note: in passing argument 2 of ‘void cv::parallel_for_(const cv::Range&, const cv::ParallelLoopBody&, double)’
CV_EXPORTS void parallel_for_(const Range& range, const ParallelLoopBody& body, double nstripes=-1.);
^~~~~~~~~~~~~
CMakeFiles/optical_flow.dir/build.make:81: 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:96: recipe for target 'CMakeFiles/optical_flow.dir/all' failed
make[1]: *** [CMakeFiles/optical_flow.dir/all] Error 2
Makefile:102: recipe for target 'all' failed
make: *** [all] Error 2
四、安装openCV4.5.2
需要重新安装opencv4(与opencv3共存的情况下)
1.依赖包的安装:
sudo apt-get install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
sudo apt install python3-dev python3-numpy
sudo apt install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
sudo apt install libpng-dev libopenexr-dev libtiff-dev libwebp-dev
2.opencv4的安装:
1.下载opencv4.5.2的包,链接在此阿里云盘下载 2.提取,然后进入解压后的文件夹,在终端打开
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4 ..
make -j4
sudo make install
注意点① 比opencv3的安装多一个 OPENCV_GENERATE_PKGCONFIG=ON -D,产生pkgconfig
注意点② 由于要共存,所以把opencv4安装于一个特定的位置,与之前的opencv3在同一个目录之下,以共存
目录为/usr/local/opencv4,与/usr/local/opencv3共存
五、例程
1.optical_flow
修改CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(ch8)
set(CMAKE_BUILD_TYPE "Release")
add_definitions("-DENABLE_SSE")
set(CMAKE_CXX_FLAGS "-std=c++17 ${SSE_FLAGS} -g -O3 -march=native")
set(OpenCV_DIR "/usr/local/opencv4/lib/cmake/opencv4")
find_package(OpenCV 4 REQUIRED)
# list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)
find_package(G2O REQUIRED)
find_package(Sophus REQUIRED)
find_package(Pangolin REQUIRED)
include_directories(
${OpenCV_INCLUDE_DIRS}
${G2O_INCLUDE_DIRS}
${Sophus_INCLUDE_DIRS}
"/usr/include/eigen3/"
${Pangolin_INCLUDE_DIRS}
)
add_executable(optical_flow optical_flow.cpp)
target_link_libraries(optical_flow ${OpenCV_LIBS} fmt)
add_executable(direct_method direct_method.cpp)
target_link_libraries(direct_method ${OpenCV_LIBS} ${Pangolin_LIBRARIES} fmt)
重点:
set(OpenCV_DIR "/usr/local/opencv4/lib/cmake/opencv4")
find_package(OpenCV 4 REQUIRED)
报错:
$ make
Scanning dependencies of target optical_flow
[ 25%] Building CXX object CMakeFiles/optical_flow.dir/optical_flow.cpp.o
/home/sfann/sf_slam_temp_code/ch8/optical_flow.cpp: In function ‘int main(int, char**)’:
/home/sfann/sf_slam_temp_code/ch8/optical_flow.cpp:145:37: error: ‘CV_GRAY2BGR’ was not declared in this scope
cv::cvtColor(img2, img2_single, CV_GRAY2BGR);
^~~~~~~~~~~
CMakeFiles/optical_flow.dir/build.make:81: 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:96: recipe for target 'CMakeFiles/optical_flow.dir/all' failed
make[1]: *** [CMakeFiles/optical_flow.dir/all] Error 2
Makefile:102: recipe for target 'all' failed
make: *** [all] Error 2
解决办法:需要将CV_GRAY2BGR,更新为COLOR_GRAY2BGR。
$ ./optical_flow
build pyramid time: 9.2984e-05
track pyr 3 cost time: 0.000619112
track pyr 2 cost time: 0.000590654
track pyr 1 cost time: 0.000554476
track pyr 0 cost time: 0.000542338
optical flow by gauss-newton: 0.00261315
optical flow by opencv: 0.0021993
2.direct_method
1.修改图片路径:
string left_file = "../left.png";
string disparity_file = "../disparity.png";
boost::format fmt_others("../%06d.png"); // other files
2.opencv4高版本,使用cv::COLOR_******
CV_GRAY2BGR 替换为 cv::COLOR_GRAY2BGR
3.修改CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(ch8)
set(CMAKE_BUILD_TYPE "Release")
add_definitions("-DENABLE_SSE")
set(CMAKE_CXX_FLAGS "-std=c++17 ${SSE_FLAGS} -g -O3 -march=native")
set(OpenCV_DIR "/usr/local/opencv4/lib/cmake/opencv4")
find_package(OpenCV 4 REQUIRED)
# list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)
find_package(G2O REQUIRED)
find_package(Sophus REQUIRED)
find_package(Pangolin REQUIRED)
include_directories(
${OpenCV_INCLUDE_DIRS}
${G2O_INCLUDE_DIRS}
${Sophus_INCLUDE_DIRS}
"/usr/include/eigen3/"
${Pangolin_INCLUDE_DIRS}
)
add_executable(optical_flow optical_flow.cpp)
target_link_libraries(optical_flow ${OpenCV_LIBS} fmt)
add_executable(direct_method direct_method.cpp)
target_link_libraries(direct_method ${OpenCV_LIBS} ${Pangolin_LIBRARIES} fmt)
4.结果:
$ ./direct_method
iteration: 0, cost: 396047
iteration: 1, cost: 170311
iteration: 2, cost: 68695.2
iteration: 3, cost: 61602.9
cost increased: 70395.7, 61602.9
T21 =
0.999991 0.00243822 0.00338756 0.00264872
-0.00244716 0.999994 0.00263916 0.000693819
-0.0033811 -0.00264742 0.999991 -0.7302
0 0 0 1
direct method for single layer: 0.0031927
iteration: 0, cost: 357860
iteration: 1, cost: 223936
iteration: 2, cost: 135219
iteration: 3, cost: 85333.7
iteration: 4, cost: 60622.6
iteration: 5, cost: 58047.2
cost increased: 58175.8, 58047.2
T21 =
0.999972 0.000931076 0.00743215 0.0122906
-0.000965053 0.999989 0.00456935 0.00263182
-0.00742781 -0.0045764 0.999962 -1.45965
0 0 0 1
direct method for single layer: 0.00701865
iteration: 0, cost: 82639.7
iteration: 1, cost: 79860.3
cost increased: 80087.8, 79860.3
T21 =
0.999972 0.00118943 0.00740126 0.00903257
-0.00121908 0.999991 0.00400262 0.0040497
-0.00739644 -0.00401153 0.999965 -1.46843
0 0 0 1
direct method for single layer: 0.00167727
iteration: 0, cost: 128292
iteration: 1, cost: 127945
cost increased: 129872, 127945
T21 =
0.99997 0.000724069 0.0076681 -0.00106069
-0.00075418 0.999992 0.00392457 0.00274581
-0.00766519 -0.00393024 0.999963 -1.48184
0 0 0 1
direct method for single layer: 0.00193025
iteration: 0, cost: 116088
iteration: 1, cost: 107616
T21 =
0.999971 0.000698869 0.00758987 -0.00249814
-0.000727147 0.999993 0.00372375 0.00398725
-0.00758721 -0.00372916 0.999964 -1.48136
0 0 0 1
direct method for single layer: 0.0023657
显示最近输入的命令:
history [n] #参数n :数字,意思是‘要列出最近的 n 笔命令列表’的意思!