RaspiCam: C++ API for using Raspberry camera with/without OpenCv

来源:互联网 发布:金税盘开票软件口令 编辑:程序博客网 时间:2024/05/21 23:33
This library allows to use the Raspberry Pi Camera under BSD License. 
Main Developer: Rafael Muñoz Salinas ( rmsalinas at uco dot es)
Acknowledgement: thanks to Josh-Larson for his contribution. 
 
Update 2015/02/28: version 0.1.3 released Download at SourceForge
 
Notes:
Requires to update the firmware to use shutterspeed (sudo rpi-update)
 

Main features

 - Provides  class RaspiCam for easy and full control of the camera
 - Provides class  RaspiCam_Still and RaspiCam_Still_Cv for controlling the camera in still mode
 - Provides class  RaspiCam_Cv for easy control of the camera with OpenCV.
 - Provides class  RaspiCam_Still and RaspiCam_Still_Cv for controlling the camera in still mode
 - Provides class RaspiCam_Still and RaspiCam_Still_Cv for using the still camera mode
 - Easy compilation/installation using cmake.
 - No need to install development file of userland. Implementation is hidden.
 - Many examples 
 

Compiling

Download the file to your raspberry. Then, uncompress the file and compile
 
tar xvzf raspicamxx.tgz
cd raspicamxx
mkdir build
cd build
cmake ..
 
At this point you'll see something like 
-- CREATE OPENCV MODULE=1
-- CMAKE_INSTALL_PREFIX=/usr/local
-- REQUIRED_LIBRARIES=/opt/vc/lib/libmmal_core.so;/opt/vc/lib/libmmal_util.so;/opt/vc/lib/libmmal.so
-- Change a value with: cmake -D<Variable>=<Value>
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/raspicam/trunk/build
 
If OpenCV development files are installed in your system, then  you see
-- CREATE OPENCV MODULE=1
otherwise this option will be 0 and the opencv module of the library will not be compiled.
 
Finally compile, install and update the ldconfig
make
sudo make install
sudo ldconfig
 
 
 
After that, you have the programs raspicam_test  and raspicam_cv_test (if opencv was enabled).
Run the first program to check that compilation is ok.
 

Using it in your projects

Download the project example at  SourceForge

 
You can learn how to use the library by taking a look at the examples in the utils directory and  by analyzing the header files. In addition, we  provide a some simple examples on how to use the library with cmake.
 
First, create a directory for our own project. Then, go in and create a file with the name simpletest_raspicam.cpp and add the following code
 
/**
*/
#include <ctime>
#include <fstream>
#include <iostream>
#include <raspicam/raspicam.h>
using namespace std;
 
int main ( int argc,char **argv ) {
    raspicam::RaspiCam Camera; //Cmaera object
    //Open camera 
    cout<<"Opening Camera..."<<endl;
    if ( !Camera.open()) {cerr<<"Error opening camera"<<endl;return -1;}
    //wait a while until camera stabilizes
    cout<<"Sleeping for 3 secs"<<endl;
    sleep(3);
    //capture
    Camera.grab();
    //allocate memory
    unsigned char *data=new unsigned char[  Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_RGB )];
    //extract the image in rgb format
    Camera.retrieve ( data,raspicam::RASPICAM_FORMAT_RGB );//get camera image
    //save
    std::ofstream outFile ( "raspicam_image.ppm",std::ios::binary );
    outFile<<"P6\n"<<Camera.getWidth() <<" "<<Camera.getHeight() <<" 255\n";
    outFile.write ( ( char* ) data, Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_RGB ) );
    cout<<"Image saved at raspicam_image.ppm"<<endl;
    //free resrources    
    delete data;
    return 0;
}
 
For cmake users,  create a file named CMakeLists.txt and add:
#####################################
cmake_minimum_required (VERSION 2.8) 
project (raspicam_test)
find_package(raspicam REQUIRED)
add_executable (simpletest_raspicam simpletest_raspicam.cpp)  
target_link_libraries (simpletest_raspicam ${raspicam_LIBS})
#####################################
 
Finally, create build dir,compile and execute
mkdir build
cd build
cmake ..
make
./simpletest_raspicam
 
If you do not like cmake, simply 
g++ simpletest_raspicam.cpp -o simpletest_raspicam -I/usr/local/include -lraspicam -lmmal -lmmal_core -lmmal_util
 

OpenCV Interface

 
If the OpenCV is found when compiling the library, the libraspicam_cv.so module is created and the RaspiCam_Cv class available. Take a look at the examples in utils to see how to use the class. In addition, we show here how you can use the RaspiCam_Cv in your own project using cmake.
 
First create a file with the name simpletest_raspicam_cv.cpp and add the following code
 
#include <ctime>
#include <iostream>
#include <raspicam/raspicam_cv.h>
using namespace std; 
 
int main ( int argc,char **argv ) {
   
    time_t timer_begin,timer_end;
    raspicam::RaspiCam_Cv Camera;
    cv::Mat image;
    int nCount=100;
    //set camera params
    Camera.set( CV_CAP_PROP_FORMAT, CV_8UC1 );
    //Open camera
    cout<<"Opening Camera..."<<endl;
    if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;return -1;}
    //Start capture
    cout<<"Capturing "<<nCount<<" frames ...."<<endl;
    time ( &timer_begin );
    for ( int i=0; i<nCount; i++ ) {
        Camera.grab();
        Camera.retrieve ( image);
        if ( i%5==0 )  cout<<"\r captured "<<i<<" images"<<std::flush;
    }
    cout<<"Stop camera..."<<endl;
    Camera.release();
    //show time statistics
    time ( &timer_end ); /* get current time; same as: timer = time(NULL)  */
    double secondsElapsed = difftime ( timer_end,timer_begin );
    cout<< secondsElapsed<<" seconds for "<< nCount<<"  frames : FPS = "<<  ( float ) ( ( float ) ( nCount ) /secondsElapsed ) <<endl;
    //save image 
    cv::imwrite("raspicam_cv_image.jpg",image);
    cout<<"Image saved at raspicam_cv_image.jpg"<<endl;
}
 
For cmake users, create a file named CMakeLists.txt and add:
#####################################
cmake_minimum_required (VERSION 2.8) 
project (raspicam_test)
find_package(raspicam REQUIRED)
find_package(OpenCV)
IF  ( OpenCV_FOUND AND raspicam_CV_FOUND)
MESSAGE(STATUS "COMPILING OPENCV TESTS")
add_executable (simpletest_raspicam_cv simpletest_raspicam_cv.cpp)  
target_link_libraries (simpletest_raspicam_cv ${raspicam_CV_LIBS})
ELSE()
MESSAGE(FATAL_ERROR "OPENCV NOT FOUND IN YOUR SYSTEM")
ENDIF()
#####################################
 
Finally, create,compile and execute
mkdir build
cd build
cmake ..
make
./simpletest_raspicam_cv
 
 
If you do not like cmake:
g++ simpletest_raspicam_cv.cpp -o  simpletest_raspicam_cv -I/usr/local/include/ -lraspicam -lraspicam_cv -lmmal -lmmal_core -lmmal_util -lopencv_core -lopencv_highgui 
 
or similar


原文:http://www.uco.es/investiga/grupos/ava/node/40

文章虽好,两个问题:

1. cmake problem

by srayner » Sat Dec 20, 2014 6:53 pm
I'm trying to compile a c++ program that uses this raspberry pi camera library; http://www.uco.es/investiga/grupos/ava/node/40

I'm new to c++ programming. I found the g++ compiler too confusing so i'm trying to use cmake.
However cmake give the following error output;

CODE: SELECT ALL
By not providing "Findraspicam.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "raspicam",
  but CMake did not find one.

  Could not find a package configuration file provided by "raspicam" with any
  of the following names:

    raspicamConfig.cmake
    raspicam-config.cmake

  Add the installation prefix of "raspicam" to CMAKE_PREFIX_PATH or set
  "raspicam_DIR" to a directory containing one of the above files.  If
  "raspicam" provides a separate development package or SDK, be sure it has
  been installed.



I see the .cmake files are located in /usr/local/lib/cmake

How do I tell cmake where to find them?
Posts: 10
Joined: Sun Mar 17, 2013 5:20 pm
Location: UK
by java » Sat Dec 20, 2014 8:46 pm
Normally, one runs a configure script whe compiling something with g++ "#.configure" which checks system variables and installed packages, which it saves. Then you run "make" which reads the data collected by the configure script, then 'makes' the binary, which is installed using " make install " normally. 

If a package deviates from this convention it is usually documented in a readme file, dand apptopriate instructions on how to progress from there.

Problems are caused buy noy following these instructings and not paying attention to error messages generated buy the convigure script.


when the congigure sfriptģ runsvthtpvwithoiy error it crsstes a nake gikrr thst shouldvworkj
Posts: 76
Joined: Mon Jul 21, 2014 9:41 am
by PeterO » Sat Dec 20, 2014 8:57 pm
cmake does not use a "configure" script so the previous reply does not apply.

What have you got in your CMakeLists.txt file ?

PeterO
Discoverer of the PI2 XENON DEATH FLASH !
User avatar
Posts: 1723
Joined: Sun Jul 22, 2012 4:14 pm
by srayner » Sat Dec 20, 2014 9:09 pm
CMakeList.txt contains the following;

CODE: SELECT ALL
cmake_minimum_required (VERSION 2.8) 
project (tracking)
find_package(raspicam REQUIRED)
find_package(OpenCV)
IF  ( OpenCV_FOUND AND raspicam_CV_FOUND)
    MESSAGE(STATUS "COMPILING OPENCV TESTS")
    add_executable (tracking tracking.cpp)  
    target_link_libraries (tracking ${raspicam_CV_LIBS})
ELSE()
    MESSAGE(FATAL_ERROR "OPENCV NOT FOUND IN YOUR SYSTEM")
ENDIF()


I tried adding this line just before the find_package...
CODE: SELECT ALL
set(CMAKE_MODULE_PATH "/usr/local/lib/cmake/${CMAKE_MODULE_PATH}") 


It then seems to be able to find raspicam, but then give a ton of errors which seem to relate to openCV

原文:https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=94426

2. Raspicam C++ API mmal linking



2down votefavorite
1

I have installed the raspicam API as instructed here: http://www.uco.es/investiga/grupos/ava/node/40

It appears to be installed correctly.

When I compile any programs linking libmmal I get the errors:
/usr/bin/ld: cannot find -lmmal
/usr/bin/ld: cannot find -lmmal_core
/usr/bin/ld: cannot find -lmmal_util

I have tried updating the pi and reinstalling the API but I just can't get it to compile.

shareimprove this question
 

1 Answer

activeoldestvotes
up vote0down voteaccepted

Try ldconfig -p | grep libmmal. On raspbian you should get:

    libmmal_vc_client.so (libc6,hard-float) => /opt/vc/lib/libmmal_vc_client.so    libmmal_util.so (libc6,hard-float) => /opt/vc/lib/libmmal_util.so    libmmal_core.so (libc6,hard-float) => /opt/vc/lib/libmmal_core.so    libmmal_components.so (libc6,hard-float) => /opt/vc/lib/libmmal_components.so    libmmal.so (libc6,hard-float) => /opt/vc/lib/libmmal.so

If not, add to your question the contents of the few short files in /etc/ld.so.conf.d. If you are not using raspbian, you should give your OS.


This means the relevant libraries are available on the system and if you build an executable linking them, there should not be a problem. However, there may be a problem building the executable; in this case it seems when gcc invokes the linker, /opt/vc/lib is not in its path even though it is in the runtime loader path (they are not quite the same). If you use gcc -v, you'll see there are some non-standard paths being used as options (e.g., /usr/lib/gcc/arm-linux-gnueabihf), but not that one. However, you can specify it yourself:

gcc test.c -L/opt/vc/lib -lmmal

Should work. You can also:

export LIBRARY_PATH=/opt/vc/lib

This variable is probably not predefined but you'll see those other paths added to it in gcc -v output. Note it is not the same as LD_LIBRARY_PATH, which is used by the runtime loader.

shareimprove this answer
 
 
I get exactly this output –  user2290362 Nov 10 '14 at 15:09
 
Hmm, I just downloaded v0.1.1 and test compiled it following the instructions from that page; make seems to have completed without error. There's a .so in build/src and some tests in build/utils. – goldilocks Nov 10 '14 at 15:33
 
Sorry, I wasn't clear I'll update the question - the error is when I compile a test program. So anything of the form: g++ -o out main.cpp -lraspicam -lmmal -lmmal_core -lmmal_util –  user2290362 Nov 10 '14 at 15:43
 
Okay, the issue is the difference between the paths used by the compile time linker and the runtime loader ;) I've added a few paragraphs about that. –  goldilocks Nov 10 '14 at 16:02
 
Compiles - thanks! –  user2290362 Nov 10 '14 at 16:50
原文:http://raspberrypi.stackexchange.com/questions/24394/raspicam-c-api-mmal-linking



1 0
原创粉丝点击