简体   繁体   English

使用 OpenCV 和 BCM2835 在 Raspberry Pi 上工作

[英]Working on Raspberry Pi with OpenCV and BCM2835

I am currently working on Raspberry Pi and using the Raspberry Pi camera module.我目前正在研究 Raspberry Pi 并使用 Raspberry Pi 相机模块。 I plan to use OpenCV for image processing on the RPi and currently it seems not much of a problem.我计划在 RPi 上使用 OpenCV 进行图像处理,目前这似乎不是什么大问题。 However, I am trying to use the BCM2835 library along with OpenCV and not able to integrate it.但是,我试图将 BCM2835 库与 OpenCV 一起使用,但无法将其集成。 I tried to make changes in the Makefile and adding the bcm library and also adding path of the BCM library but nothing seems to work.我尝试在 Makefile 中进行更改并添加 bcm 库并添加 BCM 库的路径,但似乎没有任何效果。 Please help me to integrate both the libraries as I wish to drive the GPIOs after doing some image processing on the input video.请帮助我集成这两个库,因为我希望在对输入视频进行一些图像处理后驱动 GPIO。

Thank you.谢谢你。

I finally found the answer to my own question.我终于找到了我自己问题的答案。 The bcm library which can be downloaded here: http://www.airspayce.com/mikem/bcm2835/ bcm 库可以在这里下载: http : //www.airspayce.com/mikem/bcm2835/

are unziped and the following files are pasted in the current folder where we are working which contains the cpp files.已解压缩,并将以下文件粘贴到我们正在工作的当前文件夹中,其中包含 cpp 文件。 bcm2835.h bcm2835.o bcm2835.c bcm2835.h bcm2835.o bcm2835.c

I added the bcm2835.c in the add_executable of CMakeLists.txt as follows: add_executable(camcv_vid2 bcm2835.c RaspiCamControl.c RaspiCLI.c RaspiPreview.c camcv_vid2.cpp)我在 CMakeLists.txt 的 add_executable 中添加了 bcm2835.c,如下所示: add_executable(camcv_vid2 bcm2835.c RaspiCamControl.c RaspiCLI.c RaspiPreview.c camcv_vid2.cpp)

Add the bcm2835.h in the program: extern "C" { ..在程序中添加bcm2835.h:extern "C" { ..

include "bcm2835.h"包括“bcm2835.h”

.. } .. }

Initialize the GPIO using bcm2835_init() in the main function and you are good to go.在主函数中使用 bcm2835_init() 初始化 GPIO,您就可以开始了。 Use the GPIOs to your benefit.使用 GPIO 为您带来好处。 Overall, it is a combination of combining C and C++ files along with modifications in the CMakeLists.txt总的来说,它是结合 C 和 C++ 文件以及 CMakeLists.txt 中的修改的组合

Enjoy!享受!

Although the topic is over 5 years old, I still find the need to add an answer, since I have been working on this now as well and found an alternative method.虽然这个话题已经超过 5 年了,但我仍然发现需要添加一个答案,因为我现在也一直在研究这个并找到了一个替代方法。 This method does not require to add the bcm2835.c file to add_executable but uses the pre-compiled library and links it该方法不需要将bcm2835.c文件添加到add_executable而是使用预编译库并链接

I am running Ubuntu 20.04.1 LTS on a Raspberry Pi 3 B我在 Raspberry Pi 3 B 上运行 Ubuntu 20.04.1 LTS

Setting up the library设置库

  1. Download bcm2835 library using: wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.xx.tar.gz;使用以下方法下载bcm2835库: wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.xx.tar.gz;
  2. Install libary using instructions on the website :使用网站上的说明安装库:
    1. # download the latest version of the library, say bcm2835-1.xx.tar.gz, then:
    2. tar zxvf bcm2835-1.xx.tar.gz
    3. cd bcm2835-1.xx
    4. ./configure
    5. make
    6. sudo make check
    7. sudo make install
  3. In the CMakeLists.txt file add find_library(BCM2835_LIB bcm2835)CMakeLists.txt文件中添加find_library(BCM2835_LIB bcm2835)
  4. Make sure the library is linked to the target using target_link_libraries(<target name> ${BCM2835_LIB})确保使用target_link_libraries(<target name> ${BCM2835_LIB})将库链接到目标
  5. Adding the extern "C" part is not necessary (anymore), since this is taken care of in the header file of the library.添加extern "C"部分是不必要的(不再),因为这在库的头文件中得到了处理。
  6. Initialize the GPIO's bcm2835_init() in the script file在脚本文件中初始化GPIO的bcm2835_init()

For building I did the following:对于构建,我做了以下工作:

  1. Create a build folder using mkdir build and go to the folder cd build使用mkdir build创建一个 build 文件夹并转到文件夹cd build
  2. Run cmake .. which sets up the compiling settings and environment运行cmake ..设置编译设置和环境
  3. Run make <target name> to create the target executable or run make all to create all targets executables运行make <target name>创建目标可执行文件或运行make all创建所有目标可执行文件

Created a cpp-file called main.cpp based off blink.c ( source ) that worked:创建了一个名为main.cpp的 cpp 文件基于blink.c ( source ) 工作:

#include "bcm2835.h"
using namespace std;
#define PIN RPI_BPLUS_GPIO_J8_07 // pin 4

int main(int argc, char **argv)
{
    if (!bcm2835_init())
        return 1;

    // Set the pin to be an output
    bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);

    // Blink
    while (1)
    {
        // Turn it on
        bcm2835_gpio_write(PIN, HIGH);

        // wait a bit
        delay(500);
        // turn it off
        bcm2835_gpio_write(PIN, LOW);
        // wait a bit
        delay(500);
    }

    return 0;

}

CMakeLists.txt file CMakeLists.txt 文件

#Declare the version of the CMake API for forward-compatibility
cmake_minimum_required(VERSION 2.8)

#Declare the name of the CMake Project
project(main)

find_library(BCM2835_LIB bcm2835)
if(NOT BCM2835_LIB)
  message(FATAL_ERROR "bcm2835_lib library not found")
else()
  message(STATUS "bcm2835_lib library found")
endif()

# Add the directory to search for header files
include_directories(include)

# Define an executable target 
add_executable(main main.cpp)
target_link_libraries(main ${BCM2835_LIB})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM