146 lines
5.8 KiB
C++

/**********************************************************
Software developed by AVA ( Ava Group of the University of Cordoba, ava at uco dot es)
Main author Rafael Munoz Salinas (rmsalinas at uco dot es)
This software is released under BSD license as expressed below
-------------------------------------------------------------------
Copyright (c) 2013, AVA ( Ava Group University of Cordoba, ava at uco dot es)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by the Ava group of the University of Cordoba.
4. Neither the name of the University nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY AVA ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL AVA BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
****************************************************************/
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include "raspicam_cv.h"
using namespace std;
bool doTestSpeedOnly=false;
//parse command line
//returns the index of a command line param in argv. If not found, return -1
int findParam ( string param,int argc,char **argv ) {
int idx=-1;
for ( int i=0; i<argc && idx==-1; i++ )
if ( string ( argv[i] ) ==param ) idx=i;
return idx;
}
//parse command line
//returns the value of a command line param. If not found, defvalue is returned
float getParamVal ( string param,int argc,char **argv,float defvalue=-1 ) {
int idx=-1;
for ( int i=0; i<argc && idx==-1; i++ )
if ( string ( argv[i] ) ==param ) idx=i;
if ( idx==-1 ) return defvalue;
else return atof ( argv[ idx+1] );
}
void processCommandLine ( int argc,char **argv,raspicam::RaspiCam_Cv &Camera ) {
Camera.set ( cv::CAP_PROP_FRAME_WIDTH, getParamVal ( "-w",argc,argv,1280 ) );
Camera.set ( cv::CAP_PROP_FRAME_HEIGHT, getParamVal ( "-h",argc,argv,960 ) );
Camera.set ( cv::CAP_PROP_BRIGHTNESS,getParamVal ( "-br",argc,argv,50 ) );
Camera.set ( cv::CAP_PROP_CONTRAST ,getParamVal ( "-co",argc,argv,50 ) );
Camera.set ( cv::CAP_PROP_SATURATION, getParamVal ( "-sa",argc,argv,50 ) );
Camera.set ( cv::CAP_PROP_GAIN, getParamVal ( "-g",argc,argv ,50 ) );
Camera.set ( cv::CAP_PROP_FPS, getParamVal ( "-fps",argc,argv, 0 ) );
if ( findParam ( "-gr",argc,argv ) !=-1 )
Camera.set ( cv::CAP_PROP_FORMAT, CV_8UC1 );
if ( findParam ( "-test_speed",argc,argv ) !=-1 )
doTestSpeedOnly=true;
if ( findParam ( "-ss",argc,argv ) !=-1 )
Camera.set ( cv::CAP_PROP_EXPOSURE, getParamVal ( "-ss",argc,argv ) );
// Camera.setSharpness ( getParamVal ( "-sh",argc,argv,0 ) );
// if ( findParam ( "-vs",argc,argv ) !=-1 )
// Camera.setVideoStabilization ( true );
// Camera.setExposureCompensation ( getParamVal ( "-ev",argc,argv ,0 ) );
}
void showUsage() {
cout<<"Usage: "<<endl;
cout<<"[-gr set gray color capture]\n";
cout<<"[-test_speed use for test speed and no images will be saved]\n";
cout<<"[-w width] [-h height] \n[-br brightness_val(0,100)]\n";
cout<<"[-co contrast_val (0 to 100)]\n[-sa saturation_val (0 to 100)]";
cout<<"[-g gain_val (0 to 100)]\n";
cout<<"[-ss shutter_speed (0 to 100) 0 auto]\n";
cout<<"[-fps frame_rate (0 to 120) 0 auto]\n";
cout<<"[-nframes val: number of frames captured (100 default). 0 == Infinite lopp]\n";
cout<<endl;
}
int main ( int argc,char **argv ) {
if ( argc==1 ) {
cerr<<"Usage (-help for help)"<<endl;
}
if ( findParam ( "-help",argc,argv ) !=-1 ) {
showUsage();
return -1;
}
raspicam::RaspiCam_Cv Camera;
processCommandLine ( argc,argv,Camera );
cout<<"Connecting to camera"<<endl;
if ( !Camera.open() ) {
cerr<<"Error opening camera"<<endl;
return -1;
}
cout<<"Connected to camera ="<<Camera.getId() <<endl;
cv::Mat image;
int nCount=getParamVal ( "-nframes",argc,argv, 100 );
cout<<"Capturing"<<endl;
double time_=cv::getTickCount();
for ( int i=0; i<nCount || nCount==0; i++ ) {
Camera.grab();
Camera.retrieve ( image );
if ( !doTestSpeedOnly ) {
if ( i%5==0 ) cout<<"\r capturing ..."<<i<<"/"<<nCount<<std::flush;
if ( i%30==0 && i!=0 )
cv::imwrite ("image"+std::to_string(i)+".jpg",image );
}
}
if ( !doTestSpeedOnly ) cout<<endl<<"Images saved in imagexx.jpg"<<endl;
double secondsElapsed= double ( cv::getTickCount()-time_ ) /double ( cv::getTickFrequency() ); //time in second
cout<< secondsElapsed<<" seconds for "<< nCount<<" frames : FPS = "<< ( float ) ( ( float ) ( nCount ) /secondsElapsed ) <<endl;
Camera.release();
}