Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

raspicam_cv_test.cpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**********************************************************
  2. Software developed by AVA ( Ava Group of the University of Cordoba, ava at uco dot es)
  3. Main author Rafael Munoz Salinas (rmsalinas at uco dot es)
  4. This software is released under BSD license as expressed below
  5. -------------------------------------------------------------------
  6. Copyright (c) 2013, AVA ( Ava Group University of Cordoba, ava at uco dot es)
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions
  10. are met:
  11. 1. Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. 3. All advertising materials mentioning features or use of this software
  17. must display the following acknowledgement:
  18. This product includes software developed by the Ava group of the University of Cordoba.
  19. 4. Neither the name of the University nor the names of its contributors
  20. may be used to endorse or promote products derived from this software
  21. without specific prior written permission.
  22. THIS SOFTWARE IS PROVIDED BY AVA ''AS IS'' AND ANY
  23. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. DISCLAIMED. IN NO EVENT SHALL AVA BE LIABLE FOR ANY
  26. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ****************************************************************/
  33. #include <iostream>
  34. #include <ctime>
  35. #include <cstdlib>
  36. #include <fstream>
  37. #include <sstream>
  38. #include "raspicam_cv.h"
  39. using namespace std;
  40. bool doTestSpeedOnly=false;
  41. //parse command line
  42. //returns the index of a command line param in argv. If not found, return -1
  43. int findParam ( string param,int argc,char **argv ) {
  44. int idx=-1;
  45. for ( int i=0; i<argc && idx==-1; i++ )
  46. if ( string ( argv[i] ) ==param ) idx=i;
  47. return idx;
  48. }
  49. //parse command line
  50. //returns the value of a command line param. If not found, defvalue is returned
  51. float getParamVal ( string param,int argc,char **argv,float defvalue=-1 ) {
  52. int idx=-1;
  53. for ( int i=0; i<argc && idx==-1; i++ )
  54. if ( string ( argv[i] ) ==param ) idx=i;
  55. if ( idx==-1 ) return defvalue;
  56. else return atof ( argv[ idx+1] );
  57. }
  58. void processCommandLine ( int argc,char **argv,raspicam::RaspiCam_Cv &Camera ) {
  59. Camera.set ( cv::CAP_PROP_FRAME_WIDTH, getParamVal ( "-w",argc,argv,1280 ) );
  60. Camera.set ( cv::CAP_PROP_FRAME_HEIGHT, getParamVal ( "-h",argc,argv,960 ) );
  61. Camera.set ( cv::CAP_PROP_BRIGHTNESS,getParamVal ( "-br",argc,argv,50 ) );
  62. Camera.set ( cv::CAP_PROP_CONTRAST ,getParamVal ( "-co",argc,argv,50 ) );
  63. Camera.set ( cv::CAP_PROP_SATURATION, getParamVal ( "-sa",argc,argv,50 ) );
  64. Camera.set ( cv::CAP_PROP_GAIN, getParamVal ( "-g",argc,argv ,50 ) );
  65. Camera.set ( cv::CAP_PROP_FPS, getParamVal ( "-fps",argc,argv, 0 ) );
  66. if ( findParam ( "-gr",argc,argv ) !=-1 )
  67. Camera.set ( cv::CAP_PROP_FORMAT, CV_8UC1 );
  68. if ( findParam ( "-test_speed",argc,argv ) !=-1 )
  69. doTestSpeedOnly=true;
  70. if ( findParam ( "-ss",argc,argv ) !=-1 )
  71. Camera.set ( cv::CAP_PROP_EXPOSURE, getParamVal ( "-ss",argc,argv ) );
  72. // Camera.setSharpness ( getParamVal ( "-sh",argc,argv,0 ) );
  73. // if ( findParam ( "-vs",argc,argv ) !=-1 )
  74. // Camera.setVideoStabilization ( true );
  75. // Camera.setExposureCompensation ( getParamVal ( "-ev",argc,argv ,0 ) );
  76. }
  77. void showUsage() {
  78. cout<<"Usage: "<<endl;
  79. cout<<"[-gr set gray color capture]\n";
  80. cout<<"[-test_speed use for test speed and no images will be saved]\n";
  81. cout<<"[-w width] [-h height] \n[-br brightness_val(0,100)]\n";
  82. cout<<"[-co contrast_val (0 to 100)]\n[-sa saturation_val (0 to 100)]";
  83. cout<<"[-g gain_val (0 to 100)]\n";
  84. cout<<"[-ss shutter_speed (0 to 100) 0 auto]\n";
  85. cout<<"[-fps frame_rate (0 to 120) 0 auto]\n";
  86. cout<<"[-nframes val: number of frames captured (100 default). 0 == Infinite lopp]\n";
  87. cout<<endl;
  88. }
  89. int main ( int argc,char **argv ) {
  90. if ( argc==1 ) {
  91. cerr<<"Usage (-help for help)"<<endl;
  92. }
  93. if ( findParam ( "-help",argc,argv ) !=-1 ) {
  94. showUsage();
  95. return -1;
  96. }
  97. raspicam::RaspiCam_Cv Camera;
  98. processCommandLine ( argc,argv,Camera );
  99. cout<<"Connecting to camera"<<endl;
  100. if ( !Camera.open() ) {
  101. cerr<<"Error opening camera"<<endl;
  102. return -1;
  103. }
  104. cout<<"Connected to camera ="<<Camera.getId() <<endl;
  105. cv::Mat image;
  106. int nCount=getParamVal ( "-nframes",argc,argv, 100 );
  107. cout<<"Capturing"<<endl;
  108. double time_=cv::getTickCount();
  109. for ( int i=0; i<nCount || nCount==0; i++ ) {
  110. Camera.grab();
  111. Camera.retrieve ( image );
  112. if ( !doTestSpeedOnly ) {
  113. if ( i%5==0 ) cout<<"\r capturing ..."<<i<<"/"<<nCount<<std::flush;
  114. if ( i%30==0 && i!=0 )
  115. cv::imwrite ("image"+std::to_string(i)+".jpg",image );
  116. }
  117. }
  118. if ( !doTestSpeedOnly ) cout<<endl<<"Images saved in imagexx.jpg"<<endl;
  119. double secondsElapsed= double ( cv::getTickCount()-time_ ) /double ( cv::getTickFrequency() ); //time in second
  120. cout<< secondsElapsed<<" seconds for "<< nCount<<" frames : FPS = "<< ( float ) ( ( float ) ( nCount ) /secondsElapsed ) <<endl;
  121. Camera.release();
  122. }