- # 0. 将contrib文件夹移动到opencv文件夹中去
- mkdir build
- cd build
-
- # 1. cmake生成配置文件,其中OPENCV_EXTRA_MODULES_PATH的路径中写明contrib模块的路径
- cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_BUILD_TYPE=Release -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.5.5/modules ..
-
- # 2. 编译和生成
- make -j16
- # 3. 安装
- sudo make install # 将库拷贝到/usr/local中
-
- # 4. 生成配置文件
- sudo gedit /etc/bash.bashrc # 文末添加以下内容
- PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
- export PKG_CONFIG_PATH
-
- # 5.向系统更新配置文件
- source /etc/bash.bashrc
-
- # 6. 生成opencv.conf文件,就可以利用pkg-config链接到库了
- sudo gedit /etc/ld.so.conf.d/opencv.conf # 添加库的路径: /usr/local/lib
- # 7. 更新pkg-config文件
- sudo ldconfig
- sudo updatedb
-
-
- # 8.测试是否安装好了
- pkg-config --modversion opencv
- pkg-config --libs opencv
-
- 出现以下内容,说明安装成功了!
- -L/usr/local/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
-
-
- # 9. 测试程序
- /home/helen/code/C++code/yolov5_zhr/main$ g++ main.cpp -o main `pkg-config --cflags --libs opencv`
- /usr/bin/ld: 找不到 -lopencv_shape
- /usr/bin/ld: 找不到 -lopencv_superres
- /usr/bin/ld: 找不到 -lopencv_videostab
- collect2: error: ld returned 1 exit status
出现的bug为没有编译contrib文件夹,重新编译contrib后再试:编译成功!
- ./main: error while loading shared libraries: libopencv_highgui.so.405: cannot open shared object file: No such file or directory
- # 解决办法
- sudo ldconfig # 更新一下库文件,因为刚刚才执行了 make install
main.cpp的代码如下:
- #include
- #include
- #include
-
- int main(){
- // printf("hello world\n");
- cv::Mat img;
- img = cv::imread("/home/helen/dataset/boat/FN-frames/0a19.jpg");
- if(img.empty()){
- std::cout << "fail to read img" << std::endl;
- }
- cv::namedWindow("Image");
- cv::imshow("Image", img);
- cv::waitKey(0);
-
- return 0;
- }
- // g++ main.cpp -o main 编译并生成C++文件 -o是目标文件
- // 执行main文件 "/home/helen/code/C++code/yolov5_zhr/"main