树莓派C++语言通过opencv调用csi摄像头显示图像

opencv安装过程:

  • 首先克隆opencv仓库:

git clone https://github.com/opencv/opencv.git

  • 安装必要依赖

    sudo apt-get install cmake
    sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
    sudo apt-get install libgtk2.0-dev
    sudo apt-get install pkg-config
    
  • 在opencv3.4.15文件夹下新建build文件夹配置安装路径并编译(时间很久)

    mkdir build
    cd build
    cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
    sudo make
    sudo make install
    
  • 配置opencv使用环境

    sudo gedit /etc/ld.so.conf.d/opencv.conf
    

    添加以下内容:

    /usr/local/lib
    

    保存文件修改,然后执行命令使配置生效

    sudo ldconfig
    
  • 修改环境变量

    sudo gedit /etc/bash.bashrc
    

    在文尾加上:

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

    然后在输入命令:

    source /etc/bash.bashrc
    pkg-config opencv --modversion
    

    如果出现版本号则opencv安装成功

  • 如果不存在opencv.pc导致报错,需要手动创建opencv.pc

    cd /usr/local/lib
    sudo mkdir pkgconfig
    cd pkgconfig
    sudo touch opencv.pc
    

    在opencv.pc中添加以下信息,注意这些信息需要与自己安装opencv时的库路径对应:

    prefix=/usr/local
    exec_prefix=${prefix}
    includedir=${prefix}/include
    libdir=${exec_prefix}/lib
    
    Name: opencv
    Description: The opencv library
    Version:4.0.1
    Cflags: -I${includedir}/opencv4
    Libs: -L${libdir} -lopencv_stitching -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann  -lopencv_core
    ~                   
    

    再重新检验一下opencv版本

    pkg-config opencv --modversion
    

参考:

  • opencv下载:

    Releases - OpenCV

  • 安装过程:

    (148条消息) ubuntu环境下安装opencv教程及测试_Tilldark的博客-CSDN博客_ubuntu安装opencv

    (148条消息) 在Ubuntu上使用opencv_23度ovo的博客-CSDN博客_ubuntu怎么调用opencv

  • include 时pkg-config找不到opencv.pc文件

    (148条消息) Package opencv was not found in the pkg-config search path._stdcoutzrh的博客-CSDN博客

  • 编译demo源码时找不到自定义的opencv.pc中的一些库:/usr/bin/ld: 找不到 -lopencv_shape

    /usr/bin/ld: 找不到 -lopencv_shape_wx610213d5aa4a5的技术博客_51CTO博客

  • cv2.VideoCapture(0)无法读取csi摄像头图像:

    (148条消息) 树莓派——opencv读取摄像头_IMWUGE的博客-CSDN博客_opencv调用树莓派摄像头

读取csi摄像头图像demo:

cd
sudo gedit test.cpp
#include 

using namespace cv;
using namespace std;

int main()
{
	VideoCapture capture(0);
	while(1)
	{
		Mat frame;
		capture >> frame;
		imshow("real_time_image",frame);
		waitKey(30);
	}
}

执行指令编译:

g++ test.cpp -o test `pkg-config --cflags --libs opencv`

运行
sudo ./test

你可能感兴趣的:(树莓派,opencv,c++,计算机视觉)