一、opencv的安装
补充一下:
(1)make编译出现fatal error: boostdesc_bgm.i: No such file or directory Makefile:162: recipe for target ‘all’ failed make: *** [all] Error 2
解决参考:https://blog.csdn.net/weixin_39298885/article/details/110851373
Ubuntu20.04安装OpenCV4.5.1的详细步骤(一定要注意要重新删掉build文件夹,重新cmake和make,不然依然会爆那个错误)
编译OpenCV以及openc_contrib提示缺少boostdesc_bgm.i文件出错的解决
sudo ldconfig,出现:/sbin/ldconfig.real: /usr/local/lib/libcudnn.so.7 不是符号链接
解决使用:sudo ln -sf /usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn.so.7.6.5 /usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn.so.7 改成你自己的cudnn对应的版本即可
执行 cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_GTK=ON -D OPENCV_GENERATE_PKGCONFIG=YES -D OPENCV_DNN_CUDA=ON -D WITH_CUDA=ON -D BUILD_TBB=ON -D WITH_TBB=ON -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_EXTRA_MODULES_PATH=/media/yjy/DATA/opencv_contrib-4.5.0/modules
的时候报错:
The source directory “xxxx” does not appear to contain CMakeLists.txt
解决方法:在最後面空一下再加上两个小点:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_GTK=ON -D OPENCV_GENERATE_PKGCONFIG=YES -D OPENCV_DNN_CUDA=ON -D WITH_CUDA=ON -D BUILD_TBB=ON -D WITH_TBB=ON -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_EXTRA_MODULES_PATH=/media/yjy/DATA/opencv_contrib-4.5.0/modules ..
二、在cmakelists文件中添加opencv库
在cmakelists文件中添加opencv库,以使得在c++中使用opencv读取、处理图片
# cmake needs this line
cmake_minimum_required(VERSION 2.8)
# Define project name
project(opencvTest)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS})
# Declare the executable target built from your sources
add_executable(main main.cpp)
# Link your application with OpenCV libraries
target_link_libraries(main ${OpenCV_LIBS})
比如我测试opencv安装是否ok的cmakelist文件和代码如下:
cmake_minimum_required(VERSION 2.8)
project(cv_test)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} "main.cpp")
target_link_libraries(cv_test ${OpenCV_LIBS})
#include<opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img = imread("/home/yjy/qt/cv_test/2008_002278.jpg");
imshow("LOVE", img);
waitKey(0);
return 0;
}