RaspberryP4にraspicam_cvをインストールする

###raspicam_cvをインストールする

    $ mkdir raspicam
    $ cd raspicam
    $ wget https://downloads.sourceforge.net/project/raspicam/raspicam-0.1.8.zip
    $ unzip raspicam-0.1.8.zip
    $ cd raspicam-0.1.8
    $ mkdir build
    $ cd build
    $ cmake ..
    -- The C compiler identification is GNU 8.3.0
    -- The CXX compiler identification is GNU 8.3.0 
    -- Check for working C compiler: /usr/bin/cc
    --Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done 
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Found OpenCV: /usr/local (found version "3.4.10") 
    -- Adding cv library
    -- 
    -- -------------------------------------------------------------------------------
    -- General configuration for raspicam 0.1.8
    -- -------------------------------------------------------------------------------
    -- 
        Built as dynamic libs?:ON
        Compiler:/usr/bin/c++
    -- C++ flags (Release):          -mcpu=cortex-a8 -mfpu=neon -mfloat-abi=hard   -O3 -g0  -DNDEBUG
    -- C++ flags (Debug):            -mcpu=cortex-a8 -mfpu=neon -mfloat-abi=hard   -O0 -g3  -DDEBUG -D_DEBUG -DPRINT_DEBUG_MESSAGES
    -- CMAKE_CXX_FLAGS:         
    -- CMAKE_BINARY_DIR:         /home/pi/raspicam/raspicam-0.1.8/build
    -- 
    -- TARGET_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=pthread
    -- Change a value with: cmake -D<Variable>=<Value>
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/pi/raspicam/raspicam-0.1.8/build
   
    $ make
    $ sudo make install
    $ ls  /usr/local/lib/libraspicam*
    /usr/local/lib/libraspicam_cv.so        /usr/local/lib/libraspicam.so
    /usr/local/lib/libraspicam_cv.so.0.1    /usr/local/lib/libraspicam.so.0.1
    /usr/local/lib/libraspicam_cv.so.0.1.8  /usr/local/lib/libraspicam.so.0.1.8


##テストプログラム
```
/**
simpletest_raspicam.cpp
**/
#include <ctime>
#include <fstream>
#include <iostream>
#include <raspicam/raspicam.h>
#include <unistd.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;
}
```
####CMakeLists.txt
```
    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})
```
####ビルド
    $ mkdir build
    $ cd build
    cmake ..
    make

####実行
    $ ./simpletest_raspicam
![raspicam_image.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/148248/b470d8aa-0767-0295-29e6-156dbdac406f.jpeg)



##OpenCVのテストプログラム
```
/*
* simpletest_raspicam_cv.cpp 
*/
#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;
}
```

####CMakeLists.txt
```
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()
```
####ビルド
    $ mkdir build
    $ cd build
    $ cmake ..
    $ make

####実行
$ ./simpletest_raspicam_cv

![raspicam_cv_image.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/148248/95229036-9eb1-eaa7-ef9e-8d04af5bd025.jpeg)


github https://github.com/misoragod/raspicam

####突然ldがエラーに。
#####ついさっきまで何事もなくビルドできていなのだがなぜに？
    [100%] Linking CXX executable simpletest_raspicam_cv
    /usr/bin/ld: /usr/local/lib/libraspicam_cv.so: invalid string offset 4619 >= 3963 for section `.strtab'
    /usr/bin/ld: /usr/local/lib/libraspicam_cv.so: invalid string offset 4604 >= 3963 for section `.strtab'
    /usr/bin/ld: /usr/local/lib/libraspicam_cv.so: invalid string offset 4592 >= 3963 for section `.strtab'
    /usr/bin/ld: /usr/local/lib/libraspicam_cv.so: invalid string offset 4630 >= 3963 for section `.strtab'
    /usr/bin/ld: /usr/local/lib/libraspicam_cv.so: invalid string offset 4638 >= 3963 for section `.strtab'
    /usr/bin/ld: /usr/local/lib/libraspicam_cv.so: invalid string offset 4585 >= 3963 for section `.strtab'
    /usr/bin/ld: /usr/local/lib/libraspicam_cv.so: invalid string offset 4618 >= 3963 for section `.strtab'
ELFのヘッダー情報がおかしいっていてるが、オブジェクトは出来上がっていて実行もできる。

####ldのバージョン確認
    $ ld --version 
    GNU ld (GNU Binutils for Raspbian) 2.31.1
    Copyright (C) 2018 Free Software Foundation, Inc.
    This program is free software; you may redistribute it under the terms of
    the GNU General Public License version 3 or (at your option) a later version.
    This program has absolutely no warranty.
####g++のバージョン確認
    $ g++ --version
    g++ (Raspbian 8.3.0-6+rpi1) 8.3.0
    Copyright (C) 2018 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO    
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.