vscode基于cmake安装opencv库

一、安装相关依赖库

首先更新源

sudo apt update

安装相关包

sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libjasper

若是报错:无法定位到 libjasper软件包
则依次执行以下命令

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

安装常用图像工具包

sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

安装视频I/O包

sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

安装gtk2.0

sudo apt install libgtk2.0-dev
sudo apt install pkg-config

优化函数包

sudo apt-get install libatlas-base-dev gfortran
sudo apt install libcanberra-gtk-module

二、安装OpenCV

1、在OpenCV官网下载sources。

https://opencv.org/releases/

2、解压完成后,在当前目录下会生成opencv-x.x.x文件夹

3、新建一个编译目录build,并进入。

4、进行cmake-make编译

cmake ..
make -j2

5、添加动态库

终端输入:sudo gedit /etc/ld.so.conf添加动态库
/usr/local/lib
终端输入:sudo ldconfig
修改 bash.bashrc 文件,打开文件后在文末加入以下两行代码

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

6、OpenCV测试

终端进入:/home/sen/motan/postprocess/third_parties/opencv-4.8.0/samples

cmake .
make
./opencv_example

三、配置VSCODE

cmakelists.txt输入:

set(OpenCV_DIR /home/sen/motan/postprocess/third_parties/opencv-4.8.0/build)
find_package(OpenCV REQUIRED)#REQUIRED是find_package命令的一个选项,它指定了一个库是否是必需的
include_directories(${OpenCV_INCLUDE_DIRS})
message(${OpenCV_INCLUDE_DIRS})
message(${OpenCV_LIBS})
target_link_libraries(HelloWorld PUBLIC ${OpenCV_LIBS})

四、调用摄像头

#include 
#include 
#include 
using namespace cv;
using namespace std;
 
int main()
{
    // 视频保存位置
    string outputVideoPath = "./test.avi";  
 
    // 打开摄像头
    VideoCapture capture0(0);  
 
    VideoWriter outputVideo;
    
    // 获取摄像机帧率
    int fps = capture0.get(CAP_PROP_FPS);  
 
    // 获取当前摄像头的视频信息
    cv::Size S = cv::Size((int)capture0.get(CAP_PROP_FRAME_WIDTH),
                          (int)capture0.get(CAP_PROP_FRAME_HEIGHT));
    // 打开视频路径,设置基本信息 open函数中你参数跟上面给出的VideoWriter函数是一样的
    outputVideo.open(outputVideoPath, cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), fps, S, true);
 
    if (!outputVideo.isOpened()) {
        cout << "fail to open!" << endl;
        return -1;
    }
 
    // 图片帧
    cv::Mat frameImage;
    int count = 0;
 
    while(true){
        // 读取当前帧
        capture0 >> frameImage;
        if(frameImage.empty()) break;
        ++count;
        // 输出当前帧
        cv::imshow("output", frameImage);
        // 保存当前帧
        outputVideo << frameImage;
        if (char(waitKey(1)) == 'q') break;
    }
	return 0;
}

你可能感兴趣的:(vscode,opencv,webpack)