imagestorage.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "opencv2/core.hpp"
  2. #include "opencv2/core/core_c.h"
  3. #include "opencv2/imgproc.hpp"
  4. #include "opencv2/imgcodecs.hpp"
  5. #include "imagestorage.h"
  6. #include <stdio.h>
  7. #include <iostream>
  8. #include <fstream>
  9. using namespace std;
  10. using namespace cv;
  11. bool CvCascadeImageReader::create( const string _posFilename, const string _negFilename, Size _winSize )
  12. {
  13. return posReader.create(_posFilename) && negReader.create(_negFilename, _winSize);
  14. }
  15. CvCascadeImageReader::NegReader::NegReader()
  16. {
  17. src.create( 0, 0 , CV_8UC1 );
  18. img.create( 0, 0, CV_8UC1 );
  19. point = offset = Point( 0, 0 );
  20. scale = 1.0F;
  21. scaleFactor = 1.4142135623730950488016887242097F;
  22. stepFactor = 0.5F;
  23. }
  24. bool CvCascadeImageReader::NegReader::create( const string _filename, Size _winSize )
  25. {
  26. string str;
  27. std::ifstream file(_filename.c_str());
  28. if ( !file.is_open() )
  29. return false;
  30. while( !file.eof() )
  31. {
  32. std::getline(file, str);
  33. str.erase(str.find_last_not_of(" \n\r\t")+1);
  34. if (str.empty()) break;
  35. if (str.at(0) == '#' ) continue; /* comment */
  36. imgFilenames.push_back(str);
  37. }
  38. file.close();
  39. winSize = _winSize;
  40. last = round = 0;
  41. return true;
  42. }
  43. bool CvCascadeImageReader::NegReader::nextImg()
  44. {
  45. Point _offset = Point(0,0);
  46. size_t count = imgFilenames.size();
  47. for( size_t i = 0; i < count; i++ )
  48. {
  49. src = imread( imgFilenames[last++], 0 );
  50. if( src.empty() ){
  51. last %= count;
  52. continue;
  53. }
  54. round += last / count;
  55. round = round % (winSize.width * winSize.height);
  56. last %= count;
  57. _offset.x = std::min( (int)round % winSize.width, src.cols - winSize.width );
  58. _offset.y = std::min( (int)round / winSize.width, src.rows - winSize.height );
  59. if( !src.empty() && src.type() == CV_8UC1
  60. && _offset.x >= 0 && _offset.y >= 0 )
  61. break;
  62. }
  63. if( src.empty() )
  64. return false; // no appropriate image
  65. point = offset = _offset;
  66. scale = max( ((float)winSize.width + point.x) / ((float)src.cols),
  67. ((float)winSize.height + point.y) / ((float)src.rows) );
  68. Size sz( (int)(scale*src.cols + 0.5F), (int)(scale*src.rows + 0.5F) );
  69. resize( src, img, sz, 0, 0, INTER_LINEAR_EXACT );
  70. return true;
  71. }
  72. bool CvCascadeImageReader::NegReader::get( Mat& _img )
  73. {
  74. CV_Assert( !_img.empty() );
  75. CV_Assert( _img.type() == CV_8UC1 );
  76. CV_Assert( _img.cols == winSize.width );
  77. CV_Assert( _img.rows == winSize.height );
  78. if( img.empty() )
  79. if ( !nextImg() )
  80. return false;
  81. Mat mat( winSize.height, winSize.width, CV_8UC1,
  82. (void*)(img.ptr(point.y) + point.x * img.elemSize()), img.step );
  83. mat.copyTo(_img);
  84. if( (int)( point.x + (1.0F + stepFactor ) * winSize.width ) < img.cols )
  85. point.x += (int)(stepFactor * winSize.width);
  86. else
  87. {
  88. point.x = offset.x;
  89. if( (int)( point.y + (1.0F + stepFactor ) * winSize.height ) < img.rows )
  90. point.y += (int)(stepFactor * winSize.height);
  91. else
  92. {
  93. point.y = offset.y;
  94. scale *= scaleFactor;
  95. if( scale <= 1.0F )
  96. resize( src, img, Size( (int)(scale*src.cols), (int)(scale*src.rows) ), 0, 0, INTER_LINEAR_EXACT );
  97. else
  98. {
  99. if ( !nextImg() )
  100. return false;
  101. }
  102. }
  103. }
  104. return true;
  105. }
  106. CvCascadeImageReader::PosReader::PosReader()
  107. {
  108. file = 0;
  109. vec = 0;
  110. }
  111. bool CvCascadeImageReader::PosReader::create( const string _filename )
  112. {
  113. if ( file )
  114. fclose( file );
  115. file = fopen( _filename.c_str(), "rb" );
  116. if( !file )
  117. return false;
  118. short tmp = 0;
  119. if( fread( &count, sizeof( count ), 1, file ) != 1 ||
  120. fread( &vecSize, sizeof( vecSize ), 1, file ) != 1 ||
  121. fread( &tmp, sizeof( tmp ), 1, file ) != 1 ||
  122. fread( &tmp, sizeof( tmp ), 1, file ) != 1 )
  123. CV_Error_( CV_StsParseError, ("wrong file format for %s\n", _filename.c_str()) );
  124. base = sizeof( count ) + sizeof( vecSize ) + 2*sizeof( tmp );
  125. if( feof( file ) )
  126. return false;
  127. last = 0;
  128. vec = (short*) cvAlloc( sizeof( *vec ) * vecSize );
  129. CV_Assert( vec );
  130. return true;
  131. }
  132. bool CvCascadeImageReader::PosReader::get( Mat &_img )
  133. {
  134. CV_Assert( _img.rows * _img.cols == vecSize );
  135. uchar tmp = 0;
  136. size_t elements_read = fread( &tmp, sizeof( tmp ), 1, file );
  137. if( elements_read != 1 )
  138. CV_Error( CV_StsBadArg, "Can not get new positive sample. The most possible reason is "
  139. "insufficient count of samples in given vec-file.\n");
  140. elements_read = fread( vec, sizeof( vec[0] ), vecSize, file );
  141. if( elements_read != (size_t)(vecSize) )
  142. CV_Error( CV_StsBadArg, "Can not get new positive sample. Seems that vec-file has incorrect structure.\n");
  143. if( feof( file ) || last++ >= count )
  144. CV_Error( CV_StsBadArg, "Can not get new positive sample. vec-file is over.\n");
  145. for( int r = 0; r < _img.rows; r++ )
  146. {
  147. for( int c = 0; c < _img.cols; c++ )
  148. _img.ptr(r)[c] = (uchar)vec[r * _img.cols + c];
  149. }
  150. return true;
  151. }
  152. void CvCascadeImageReader::PosReader::restart()
  153. {
  154. CV_Assert( file );
  155. last = 0;
  156. fseek( file, base, SEEK_SET );
  157. }
  158. CvCascadeImageReader::PosReader::~PosReader()
  159. {
  160. if (file)
  161. fclose( file );
  162. cvFree( &vec );
  163. }