BPI-M4 Berry安装opencv并使用canny边缘检测

这里我用的板子是BPI-M4 Berry
镜像是20240327_Bpi-m4berry_1.0.2_ubuntu22.04_jammy_desktop_xfce_linux6.1.31
摄像头是随便找的摄像头

opencv安装

opencv安装比较简单,一行命令就行

sudo apt-get update
sudo apt-get install -y libopencv-dev python3-opencv

输入以下命令,如果有输出版本号就是对的

pi@bpi-m4berry:~$ python3 -c "import cv2; print(cv2.version)"
4.5.4

编译canny

创建目录并进入

midir ~/ReadVideo
cd ~/ReadVideo

编辑ReadVideo.cpp文件

nano ReadVideo.cpp

将下面代码放入,其中VideoCapture capture(1);是摄像头的名字,如果是video0就改成0,如果是video1就改成1,我这里是video1

#include <opencv2/opencv.hpp>

using namespace cv;

int main()

{

  VideoCapture capture(1);

  Mat edges;

  while (1)

  {

    Mat frame;

    capture >> frame;

    cvtColor(frame, edges, COLOR_BGR2GRAY);

    blur(edges, edges, Size(7, 7));

    Canny(edges, edges, 0, 30, 3);

    imshow("被canny后的视频", edges);

    if (waitKey(30) >= 0) break;

  }

  return 0;

}

编辑CMakeLists
nano CMakeLists.txt
放入下面的代码

project( ReadVideo )
find_package( OpenCV REQUIRED )
add_executable( ReadVideo ReadVideo )
target_link_libraries( ReadVideo ${OpenCV_LIBS} )

编译代码

cmake .
make

文件夹内会输出一个叫ReadVideo的文件,直接执行即可