注意事项:变异contribute模块需要在OPENCV_EXTRA_MODULES_PATH中填入opencv_contrib下一级的modules文件夹。
如果不勾选,后面查看配置时会找不到:
$ pkg-config --cflags --libs opencv4
Package opencv4 was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv4.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv4' found
参考下方的博客,编译还是遇到了一些错误,这些错误不一定每一个人都会遇到。
参考博客:Ubuntu18.04安装OpenCV4.2.0+OpenCV_Contrib4.2.0库
[ 85%] Built target opencv_perf_video
[ 85%] Linking CXX executable ../../bin/opencv_test_gapi
[ 85%] Built target opencv_test_gapi
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2
向上查到错误:
/home/wmz/Downloads/opencv/opencv_contrib/modules/xfeatures2d/src/boostdesc.cpp:654:20: fatal error: boostdesc_bgm.i: No such file or directory
#include "boostdesc_bgm.i"
^~~~~~~~~~~~~~~~~
compilation terminated.
modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/build.make:91: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/boostdesc.cpp.o' failed
make[2]: *** [modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/boostdesc.cpp.o] Error 1
CMakeFiles/Makefile2:6648: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/all' failed
make[1]: *** [modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
解决办法参考:编译OpenCV以及openc_contrib提示缺少boostdesc_bgm.i文件出错的解决
参考:https://github.com/opencv/opencv_contrib/issues/1301
下载参考:安装OpenCV时提示缺少boostdesc_bgm.i文件的问题解决方案(附带百度云资源)
解决问题后重新编译:
sudo make clean
sudo make -j8
然而还是没有成功,不过错误换了:
/home/wmz/Downloads/opencv/opencv_contrib/modules/xfeatures2d/test/test_features2d.cpp:51:10: fatal error: features2d/test/test_detectors_regression.impl.hpp: No such file or directory
#include "features2d/test/test_detectors_regression.impl.hpp"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/build.make:86: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/test/test_features2d.cpp.o' failed
make[2]: *** [modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/test/test_features2d.cpp.o] Error 1
CMakeFiles/Makefile2:6746: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/all' failed
make[1]: *** [modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
参考:opencv 找不到 feature2d/test/test_detectors_regression.impl.hpp 文件
在下面这里尝试一下,在559行添加:
include_directories("modules")
改后:
好了,编译成功。
继续执行:
sudo make install
终端输入或用gedit(替换vim)打开:
sudo vim /etc/ld.so.conf.d/opencv.conf
在里面添加:
/usr/local/lib
保存退出,配置库:
sudo ldconfig
更改环境变量:
sudo gedit /etc/bash.bashrc
保存退出,这时候测试一下,输入:
pkg-config --cflags --libs opencv4
注意:opencv4以上才是pkg-config --cflags --libs opencv4,之前版本是pkg-config --cflags --libs opencv。
查看结果:
$ pkg-config --cflags --libs opencv4
-I/usr/local/include/opencv4/opencv -I/usr/local/include/opencv4 -L/usr/local/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm -lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_cudacodec -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_videoio -lopencv_cudawarping -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev
(1)测试程序1
#include
#include
using namespace std;
using namespace cv;
int main()
{
Mat img=imread("test.jpg");//
imshow("test",img);
waitKey(5000);
return 0;
}
编译:
g++ -std=c++11 test.cpp -o test `pkg-config --cflags --libs opencv4`
报错:
/usr/local/lib/libopencv_core.a(cuda_host_mem.cpp.o): In function `cv::cuda::registerPageLocked(cv::Mat&)':
cuda_host_mem.cpp:(.text._ZN2cv4cuda18registerPageLockedERNS_3MatE+0x31): undefined reference to `cudaHostRegister'
cuda_host_mem.cpp:(.text._ZN2cv4cuda18registerPageLockedERNS_3MatE+0xab): undefined reference to `cudaGetErrorString'
/usr/local/lib/libopencv_core.a(cuda_host_mem.cpp.o): In function `cv::cuda::unregisterPageLocked(cv::Mat&)':
cuda_host_mem.cpp:(.text._ZN2cv4cuda20unregisterPageLockedERNS_3MatE+0x1b): undefined reference to `cudaHostUnregister'
cuda_host_mem.cpp:(.text._ZN2cv4cuda20unregisterPageLockedERNS_3MatE+0x40): undefined reference to `cudaGetErrorString'
collect2: error: ld returned 1 exit status
明天继续解决。
(2)测试程序2
文件名:DisplayImage.cpp
#include
#include
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
printf("usage: DisplayImage.out \n");
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
创建一个CMakeLists.txt 文件:
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
在当前路径下执行:
mkdir build
cd build
cmake ..
make
./DisplayImg lena.jpg
需要将lena.jpg图片复制到build文件夹。
执行效果如下:
wmz@wmz:~/Documents/test/opencv-t2$ mkdir build
wmz@wmz:~/Documents/test/opencv-t2$ cd build
wmz@wmz:~/Documents/test/opencv-t2/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Found CUDA: /usr/local/cuda (found suitable exact version "10.2")
-- Found OpenCV: /usr/local (found version "4.2.0")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wmz/Documents/test/opencv-t2/build
wmz@wmz:~/Documents/test/opencv-t2/build$ make
Scanning dependencies of target DisplayImage
[ 50%] Building CXX object CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o
[100%] Linking CXX executable DisplayImage
[100%] Built target DisplayImage
wmz@wmz:~/Documents/test/opencv-t2/build$ ./DisplayImage lena.jpg
wmz@wmz:~/Documents/test/opencv-t2/build$
参考:Opencv在linux下的安装以及简单的测试
也可以直接去samples/cpp/example_cmake目录下 按顺序执行
cmake . #这个是以字母C开头的
make
./opencv_example
会调用摄像头,并输出“Hello world"。