How to use Pi Camera with OpenCV for Raspberry Pi?

pi_withPiCam Raspberry Pi 2 with Pi Camera

Normally OpenCV only supports USB camera for raspberry pi, if you want to use OpenCV with Pi camera, I have a nice guide for you.

Before we set the Pi camera, you have to install OpenCV library firstly, you can follow my previous guide.

I use the RaspiCam library from Rafael Muñoz Salinas (very good job, easy installation and fast speed), it provides C++ API for us, frame per second is 30.

1. Plug Pi camera into Raspberry Pi.

2. Download RaspiCam library into your Pi.

3. Install the library.

tar xvzf raspicam-0.1.3.tgz (uncompress the file)

cd raspicam-0.1.3 (go to the library folder)

mkdir build

cd build

cmake ..

make

sudo make install

4. Testing

picam-testing

The library provides cmake to compile the program, but it’s not convenient for me, so I wrote makefile to compile the program.Here is the makefile:

LIBS = -I/usr/local/include/ 
CFLAGS = -lraspicam -lraspicam_cv -lopencv_core -lopencv_highgui
objects= main.o camera.o

picam: $(objects) 
	g++ $(objects) -o picam $(LIBS)$(CFLAGS)

main.o: main.cpp camera.h
	g++ main.cpp -c

camera.o: camera.cpp camera.h
	g++ camera.cpp -c
	
.PHONY: clean
clean:
	rm picam $(objects)

The library provides some examples to us, I rewrote the sample code, actually we also can use OpenCV C API(I transfer cv::Mat to IplImage in sample code).

Here is sample code(GitHub), download to your raspberry pi, make and run the program, FPS is 30(CPU usage is 22%), amazing!

Advertisements

16 thoughts on “How to use Pi Camera with OpenCV for Raspberry Pi?

  1. Hello,

    thanks for yours great tutorial, but at the last step, I am not sure where I have to put yours files from GitHub. So, I have got some errors with “make” command. Probably, they are caused by wrong final directory.

    g++ main.cpp -c
    main.cpp:4:34: fatal error: raspicam/raspicam_cv.h: No such file or directory
    #include
    ^
    compilation terminated.
    Makefile:9: recipe for target ‘main.o’ failed
    make: *** [main.o] Error 1

    Please, can you help me with this problem?

    Dominik
    Best regards.

    Like

  2. Thank you for your reply, but finally, I solved this problem few days ago (it was my mistake of course). I copied folder contained “camera.cpp”, “camera.h”, “main.cpp”, and “Makefile” in main directory instead build one. Then I collected all necessary libraries in one folder and link them all in each .cpp and .h file, because I had problem with adressing correct folders of libraries. But unfortunately, now, after compile command I have some errors.
    http://s23.postimg.org/vsei243m3/error.png – error list in Geany, which I am using
    http://s11.postimg.org/f86majhqr/build_commands.png – setting of build commands (I am not sure what I need to edit here)
    I would appreciate it, if you help me with this problem. I now that, I have got something wrong, but now, I am confused.

    Like

  3. Ok, so I tried steps again. I got one of this notice in terminal although it finished without error.

    CMake Warning at CMakeLists.txt:51 (FIND_PACKAGE):
    By not providing “FindOpenCV.cmake” in CMAKE_MODULE_PATH this project has
    asked CMake to find a package configuration file provided by “OpenCV”, but
    CMake did not find one.

    Could not find a package configuration file provided by “OpenCV” with any
    of the following names:

    OpenCVConfig.cmake
    opencv-config.cmake

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

    I tried to copy all cmake files from Opencv/cmake folder to raspicam or raspicam/build folder but it is still showing same notice. :/

    Like

      1. Finally, I resolved it by re-installing OpenCV and everything is working. Thank you very much and I wish you happy and successful new year. 🙂

        Dominik

        Like

  4. Hello, I’m trying to run make in step 3 and I get the result below:

    pi@raspberrypi:~/raspicam-0.1.6/build $ make

    [ 5%] Building CXX object src/CMakeFiles/raspicam_cv.dir/raspicam_cv.cpp.o
    [ 11%] Building CXX object src/CMakeFiles/raspicam_cv.dir/raspicam_still_cv.cpp.o
    make[2]: *** No rule to make target ‘/home/pi/opencv/release/lib/libopencv_core.so.3.2.0’, needed by ‘src/libraspicam_cv.so’. Stop.
    CMakeFiles/Makefile2:119: recipe for target ‘src/CMakeFiles/raspicam_cv.dir/all’ failed
    make[1]: *** [src/CMakeFiles/raspicam_cv.dir/all] Error 2
    Makefile:127: recipe for target ‘all’ failed
    make: *** [all] Error 2
    pi@raspberrypi:~/raspicam-0.1.6/build $

    Can you help me with this problem?

    br
    zeb

    Like

    1. After cmake .. you see something like this?
      — 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=

      — Configuring done
      — Generating done
      — Build files have been written to: /home/pi/raspicam/trunk/build

      if your result is exactly like above, it should be ok.
      Make sure your OpenCV is installed correctly. By the way, I am not sure OpenCV 3.2.0 is compatible with raspicam, but I guess it’s ok.

      Like

      1. Thanks for the reply.

        I get this after cmake ..

        pi@raspberrypi:~/raspicam-0.1.6/build $ cmake ..
        — ——————————————————————————-
        — GNU COMPILER
        — ——————————————————————————-
        — Adding cv library

        — ——————————————————————————-
        — General configuration for raspicam 0.1.6
        — ——————————————————————————-

        Built as dynamic libs?:ON
        Compiler:/usr/bin/c++
        — C++ flags (Release): -std=c++0x -Wl,–no-as-needed -Wall -ffunction-sections -fomit-frame-pointer -O2 -ffast-math -DNDEBUG -lpthread
        — C++ flags (Debug): -std=c++0x -Wl,–no-as-needed -Wall -ffunction-sections -g3 -O0 -DDEBUG -D_DEBUG -W -Wextra -Wno-return-type -lpthread
        — CMAKE_CXX_FLAGS: -std=c++0x -Wl,–no-as-needed -Wall -ffunction-sections
        — CMAKE_BINARY_DIR: /home/pi/raspicam-0.1.6/build

        — CMAKE_SYSTEM_PROCESSOR = armv7l
        — BUILD_SHARED_LIBS = ON
        — BUILD_UTILS = ON
        — CMAKE_INSTALL_PREFIX = /usr/local
        — CMAKE_BUILD_TYPE = Release
        — CMAKE_MODULE_PATH = /usr/local/lib/cmake/;/usr/lib/cmake

        — 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=

        — Configuring done
        — Generating done
        — Build files have been written to: /home/pi/raspicam-0.1.6/build

        It seems to be ok. Strange that I fail to run make.

        Can you recommend a version of opencv and raspicam that works together?

        br
        Zeb

        Like

      2. the log looks ok.
        I tried 2.4.9 and 3.0, both are ok. You should know reinstall opencv need some time. use “sudo make” instead “make”, try again.

        Like

  5. Sir i am getting 13 fps with 30 fps camera raspberry pi .i am using raspberry pi zero w .is that all the fps i will get ?
    what are the fps of your camera ? you are getting almost 30 fps

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s