在树莓派上安装openCV

网上有现成的方法,按照步骤一步一步走下来基本没什么问题,就是在树莓派中编译的时间比较长,我这里使用的树莓派3b+,在树莓派安装openCv

进行编程

  • 1 用vscode 使用ssh 连接树莓派
  • 2 创建工程目录,编写Cmakelists.text,工程目录如下图
    项目配置

    • 3 编写我们的CMakeLists.txt
      “`
      cmake_minimum_required(VERSION 3.5)
      project (opencv_test)
      set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${opencv_test_SOURCE_DIR/bin})</li>
      </ul></li>
      </ul>

      <h1>寻找OpenCV库</h1>

      find_package(OpenCV REQUIRED)

      <h1>包含头文件</h1>

      include_directories(${OpenCV_INCLUDE_DIRS})
      add_executable(opencv_test src/opencv_test)
      link_directories(${OpenCV_LIBRARY_DIRS})
      target_link_libraries(opencv_test ${OpenCV_LIBS})
      “`

      • 4 编写我们的cpp 文件
      opencv_test.cpp
      #include <opencv2/core/core.hpp>
      #include <opencv2/highgui/highgui.hpp>
      #include <opencv2/imgproc/imgproc.hpp>
      #include <iostream>
      
      using namespace std;
      using namespace cv;
      
      int main(int argc,char** argv) {
          if (argc != 2)
          {
              cout << "Usage: opencv_test <image path>" << endl;
              return -1;
          }
      
          char *imgName = argv[1];
          Mat image;
      
          image = imread(imgName, 1);
          if (!image.data)
          {
              cout << "No image data" << endl;
              return -1;
          }
          Mat gray_img;
      
          cvtColor(image, gray_img, CV_BGR2GRAY);
          imwrite("images/result.jpg", gray_img);
      
          return 0;
      }
      
      
      • 5 进入build 目录中进行编译
      cd build
      cmake ..
      

发表评论

电子邮件地址不会被公开。