最近入职的一家测控公司,项目中有涉及到机器视觉,对于机器视觉这一方向我完全是小白,准备作下知识储备,在Linux C++环境下学习OpenCV;
按照官方 官方 手册,下载源码编译成功后,运行源码目录samples/cpp/example_cmake
工程,编译通过,但运行的时候报错
gjj@gjj-QTJ5:~/code/example_cmake$ cmake .
-- OpenCV library status:
-- config: /usr/local/lib/cmake/opencv4
-- version: 4.7.0
-- libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_aruco;opencv_barcode;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_datasets;opencv_dnn_objdetect;opencv_dnn_superres;opencv_dpm;opencv_face;opencv_fuzzy;opencv_hfs;opencv_img_hash;opencv_intensity_transform;opencv_line_descriptor;opencv_mcc;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_quality;opencv_rapid;opencv_reg;opencv_rgbd;opencv_saliency;opencv_shape;opencv_stereo;opencv_structured_light;opencv_superres;opencv_surface_matching;opencv_text;opencv_tracking;opencv_videostab;opencv_wechat_qrcode;opencv_xfeatures2d;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto
-- include path: /usr/local/include/opencv4
-- Configuring done
-- Generating done
-- Build files have been written to: /home/gjj/code/example_cmake
gjj@gjj-QTJ5:~/code/example_cmake$ make
Consolidate compiler generated dependencies of target opencv_example
[100%] Built target opencv_example
gjj@gjj-QTJ5:~/code/example_cmake$ ./opencv_example
Built with OpenCV 4.7.0-dev
Capture is opened
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.7.0-dev) /home/gjj/src/opencv-4.x/modules/highgui/src/window.cpp:1272: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
已放弃 (核心已转储)
错误很明显了,If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config,
让装两个依赖,装上后还是错误依旧,重启依旧不能解决;
网上扒拉了一下,应该是缺其他依赖,也有可能是需要先装这两个依赖,再进行源码编译。不管了,执行了make uninstall
,卸载掉编译后的库;
先更新apt
sudo apt-get update
sudo apt-get upgrade
安装必要依赖(安装过了就自动忽略了)
sudo apt install git gcc g++ ffmpeg cmake make python3-dev python3-numpy libavcodec-dev libavformat-dev libswscale-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libgtk-3-dev libpng-dev libjpeg-dev libopenexr-dev libtiff-dev libwebp-dev
然后安装了libgtk-2.0-dev
和pkg-config
(不确定是不是必须安装,但是上一次编译安装了也报错)
接下来安装官方手册里的安装步骤,执行源码编译,官方cmkae没有指定多线程编译,比较慢,多线程编译参考如下:
sudo make -j $(nproc)
sudo cmake --build . --target all -- -j $(nproc)
其中nproc是本机最大可用线程数
编译时间视机器配置不同,编译成功后,在build目录下执行make install
成功后,把源码目录samples/cpp/example_cmake
文件夹,复制一份放到其他目录,就可以运行cpp的示例了,大致功能就是打开本机的第一个摄像头,然后在窗体上绘制了文本
小贴士:
如果使用终端,直接就能跑起来,vs code头文件有红色波浪线,安装C++扩展后,按下CTRL + Shift + P
,搜索C/C++编辑配置
,会生成一个jc_cpp_properties.json配置文件,在includePath数组里面添加一个新成员——当前版本opencv编译安装后生成的头文件目录,我的是usr/local/include/opencv4
总结
先安装依赖,然后按照官方手册去编译源码;
标签:4.7,cmake,opencv,--,dev,22.10,OpenCV,编译,example From: https://www.cnblogs.com/g-j-j/p/17181162.html