1 #include "opencv2/core/core.hpp"
2 #include "opencv2/core/internal.hpp"
5 #include "imagestorage.h"
13 bool CvCascadeImageReader::create( const string _posFilename, const string _negFilename, Size _winSize )
15 return posReader.create(_posFilename) && negReader.create(_negFilename, _winSize);
18 CvCascadeImageReader::NegReader::NegReader()
20 src.create( 0, 0 , CV_8UC1 );
21 img.create( 0, 0, CV_8UC1 );
22 point = offset = Point( 0, 0 );
24 scaleFactor = 1.4142135623730950488016887242097F;
28 bool CvCascadeImageReader::NegReader::create( const string _filename, Size _winSize )
31 std::ifstream file(_filename.c_str());
32 if ( !file.is_open() )
35 size_t pos = _filename.rfind('\\');
37 if (pos == string::npos)
39 pos = _filename.rfind('/');
42 dirname = pos == string::npos ? "" : _filename.substr(0, pos) + dlmrt;
45 std::getline(file, str);
46 if (str.empty()) break;
47 if (str.at(0) == '#' ) continue; /* comment */
48 imgFilenames.push_back(dirname + str);
57 bool CvCascadeImageReader::NegReader::nextImg()
59 Point _offset = Point(0,0);
60 size_t count = imgFilenames.size();
61 for( size_t i = 0; i < count; i++ )
63 src = imread( imgFilenames[last++], 0 );
66 round += last / count;
67 round = round % (winSize.width * winSize.height);
70 _offset.x = std::min( (int)round % winSize.width, src.cols - winSize.width );
71 _offset.y = std::min( (int)round / winSize.width, src.rows - winSize.height );
72 if( !src.empty() && src.type() == CV_8UC1
73 && offset.x >= 0 && offset.y >= 0 )
78 return false; // no appropriate image
79 point = offset = _offset;
80 scale = max( ((float)winSize.width + point.x) / ((float)src.cols),
81 ((float)winSize.height + point.y) / ((float)src.rows) );
83 Size sz( (int)(scale*src.cols + 0.5F), (int)(scale*src.rows + 0.5F) );
84 resize( src, img, sz );
88 bool CvCascadeImageReader::NegReader::get( Mat& _img )
90 CV_Assert( !_img.empty() );
91 CV_Assert( _img.type() == CV_8UC1 );
92 CV_Assert( _img.cols == winSize.width );
93 CV_Assert( _img.rows == winSize.height );
99 Mat mat( winSize.height, winSize.width, CV_8UC1,
100 (void*)(img.data + point.y * img.step + point.x * img.elemSize()), img.step );
103 if( (int)( point.x + (1.0F + stepFactor ) * winSize.width ) < img.cols )
104 point.x += (int)(stepFactor * winSize.width);
108 if( (int)( point.y + (1.0F + stepFactor ) * winSize.height ) < img.rows )
109 point.y += (int)(stepFactor * winSize.height);
113 scale *= scaleFactor;
115 resize( src, img, Size( (int)(scale*src.cols), (int)(scale*src.rows) ) );
126 CvCascadeImageReader::PosReader::PosReader()
132 bool CvCascadeImageReader::PosReader::create( const string _filename )
136 file = fopen( _filename.c_str(), "rb" );
141 if( fread( &count, sizeof( count ), 1, file ) != 1 ||
142 fread( &vecSize, sizeof( vecSize ), 1, file ) != 1 ||
143 fread( &tmp, sizeof( tmp ), 1, file ) != 1 ||
144 fread( &tmp, sizeof( tmp ), 1, file ) != 1 )
145 CV_Error_( CV_StsParseError, ("wrong file format for %s\n", _filename.c_str()) );
146 base = sizeof( count ) + sizeof( vecSize ) + 2*sizeof( tmp );
150 vec = (short*) cvAlloc( sizeof( *vec ) * vecSize );
155 bool CvCascadeImageReader::PosReader::get( Mat &_img )
157 CV_Assert( _img.rows * _img.cols == vecSize );
159 size_t elements_read = fread( &tmp, sizeof( tmp ), 1, file );
160 if( elements_read != 1 )
161 CV_Error( CV_StsBadArg, "Can not get new positive sample. The most possible reason is "
162 "insufficient count of samples in given vec-file.\n");
163 elements_read = fread( vec, sizeof( vec[0] ), vecSize, file );
164 if( elements_read != (size_t)(vecSize) )
165 CV_Error( CV_StsBadArg, "Can not get new positive sample. Seems that vec-file has incorrect structure.\n");
167 if( feof( file ) || last++ >= count )
168 CV_Error( CV_StsBadArg, "Can not get new positive sample. vec-file is over.\n");
170 for( int r = 0; r < _img.rows; r++ )
172 for( int c = 0; c < _img.cols; c++ )
173 _img.ptr(r)[c] = (uchar)vec[r * _img.cols + c];
178 void CvCascadeImageReader::PosReader::restart()
182 fseek( file, base, SEEK_SET );
185 CvCascadeImageReader::PosReader::~PosReader()