After some busy times, I finally have a weekend for working on this project


One of the ancillary parts of this project, is to make some blob analysis on the images captured by RaspiCam to detect laser beams. So I need install the OpenCV libraries and try to get images from RaspiCam inside a C program.

The idea is that an external application will command the autopilot subsystem (by means of MAVLink messages) and will detect reflected laser beams to start some special effect


So here are the steps to install OpenCV

 

1. Install RaspiCam. Installation procedure is very well described on Raspberry official site http://www.raspberrypi.org/archives/3890 Once installed, test it with this command:

raspistill -t 10000

2. Download the MMAL library aspivid/Raspistill source code from https://github.com/raspberrypi/userland .

3. Unzip the file and copy the directory userlan-master in /opt/vc. I also renamed the directory as "userland"

4.  Go to /opt/vc/userland and type

     sed -i ‘s/if (DEFINED CMAKE_TOOLCHAIN_FILE)/if (NOT DEFINED CMAKE_TOOLCHAIN_FILE)/g’ makefiles/cmake/arm-linux.cmake

5. create a build directory...

     sudo mkdir build

     cd build

6. ... and compile

     sudo cmake -DCMAKE_BUILD_TYPE=Release ..
     sudo
make
    
sudo make install

7. go to /opt/vc/bin and test one file typing : ./raspistill -t 3000



OK... now I'm ready to create a new project

 

1. create a new folder (/home/pi/vc) in your home directory

     mkdir cv
     cd cv

2. copy all raspicam apps source code

     cp /opt/vc/userland/host_applications/linux/apps/raspicam/*  .

     mv RaspiStill.c camcv.c

     sudo chmod 777 camcv.c

3. remove then content of CMakeLists.txt and replace with :
     cmake_minimum_required(VERSION 2.8)
     project( camcv )
     SET(COMPILE_DEFINITIONS -Werror)
     include_directories(/opt/vc/userland/host_applications/linux/libs/bcm_host/include)
     include_directories(/opt/vc/userland/interface/vcos)
     include_directories(/opt/vc/userland)
     include_directories(/opt/vc/userland/interface/vcos/pthreads)
     include_directories(/opt/vc/userland/interface/vmcs_host/linux)
     include_directories(/opt/vc/userland/interface/khronos/include)

     include_directories(/opt/vc/userland/interface/khronos/common)

     add_executable(camcv RaspiCamControl.c RaspiCLI.c RaspiPreview.c camcv.c RaspiText.c RaspiTexUtil.c gl_scenes/teapot.c gl_scenes/models.c gl_scenes/square.c gl_scenes/mirror.c gl_scenes/yuv.c gl_scenes/sobel.c tga.c)
     target_link_libraries(camcv /opt/vc/lib/libmmal_core.so /opt/vc/lib/libmmal_util.so /opt/vc/lib/libmmal_vc_client.so /opt/vc/lib/libvcos.so /opt/vc/lib/libbcm_host.so /opt/vc/lib/libGLESv2.so /opt/vc/lib/libEGL.so)

4. delete CMakeFiles directory if it exits
5. compile & test
     cmake .
     make
     ./camcv -t 1000


Great!!!
We are now ready to install OpenCV


1. install both dev lib and python lib. Even if I'm going to use pure C for development, Python is still useful for small scripts, so I recommend to install it.

     sudo apt-get update

     sudo apt-get install libopencv-dev
     sudo apt-get install python-opencv

2. to test if OpenCv library is well installed, write this test software. It just displays a picture using imread and imshow functions. You will need to provide a sample .jpg file. To compile using OpenCv lib, create a CMakeLists.txt file with the following lines

     cmake_minimum_required(VERSION 2.8)
     project( displayimage )
     find_package( OpenCV REQUIRED )
     add_executable( displayimage display_image.cpp )
     target_link_libraries( displayimage ${OpenCV_LIBS} )

3. compile and execute

     cmake .
     make
     ./displayimage

4. Modify your CMakeFiles.txt to include OpenCV library

     project( camcv )

     SET(COMPILE_DEFINITIONS -Werror)

     #OPENCV
     find_package( OpenCV REQUIRED )

     include_directories(/opt/vc/userland/host_applications/linux/libs/bcm_host/include)

     include_directories(/opt/vc/userland/interface/vcos)
     include_directories(/opt/vc/userland)
     include_directories(/opt/vc/userland/interface/vcos/pthreads)
     include_directories(/opt/vc/userland/interface/vmcs_host/linux)
     include_directories(/opt/vc/userland/interface/khronos/include)

     include_directories(/opt/vc/userland/interface/khronos/common)

     add_executable(camcv RaspiCamControl.c RaspiCLI.c RaspiPreview.c camcv.c RaspiText.c RaspiTexUtil.c gl_scenes/teapot.c gl_scenes/models.c gl_scenes/square.c gl_scenes/mirror.c gl_scenes/yuv.c gl_scenes/sobel.c tga.c)
     target_link_libraries(camcv /opt/vc/lib/libmmal_core.so /opt/vc/lib/libmmal_util.so /opt/vc/lib/libmmal_vc_client.so /opt/vc/lib/libvcos.so /opt/vc/lib/libbcm_host.so /opt/vc/lib/libGLESv2.so /opt/vc/lib/libEGL.so ${OpenCV_LIBS})

5. Recompile. It should be ok, because we didn't change anything in the source code

     make
     ./camcv

 

In next post I will make a basic application that can capture an image from RaspiCam and make some video processing using the power of the OpenCV library