//thread entry function - continuously loads images (producer) void AsyncImageLoader::Run() { ImageSet images(mNumCameras); BinaryImageSet FGImages(mNumCameras); std::vector<std::string> FGfiles(mNumCameras), ImageFiles(mNumCameras); while(mCurrentFrame < mNumFrames && !mFailed) { for(unsigned int i = 0; i < mNumCameras; i++) //generate filenames { FGfiles[i] = mPath + "FG" + str(i + 1) + DIR_SEPARATOR + "image" + str(mCurrentFrame, 4) + ".bmp"; ImageFiles[i] = mPath + "CAM" + str(i + 1) + DIR_SEPARATOR + "image" + str(mCurrentFrame, 4) + ".bmp"; } LoadSet(FGfiles, FGImages, ImageFiles, images); //load the data and convert FG images to binary mDataLock.Lock(); //push images to buffer mImageBuffer.push_back(images); mFGBuffer.push_back(FGImages); mDataLock.Unlock(); mCondEmpty.NotifyOne(); //notify waiting threads of data if(mImageBuffer.size() >= mBufferSize) //if buffer is full { mLock1.Lock(); mCondFull.Wait(); //wait on full condition (wait until a request removes a set of images) mLock1.Unlock(); } mCurrentFrame++; } }
const std::vector<std::string> ImageFiles( const std::string path, const unsigned int level ) { DIR *root = opendir( path.c_str() ); if( root == NULL ) { std::cerr << "Invalid image path" << std::endl; exit( EXIT_FAILURE ); } std::vector<std::string> retVal; dirent *entry = readdir( root ); do { std::string name( entry->d_name ); if( entry->d_type == DT_DIR ) { if( name.compare( "." ) == 0 || name.compare( ".." ) == 0 ) { continue; } else { const std::vector<std::string> child = ImageFiles( path + "/" + name, level + 1 ); retVal.insert( retVal.end(), child.begin(), child.end() ); } } else { if( name.compare( ".DS_Store" ) != 0 ) { retVal.push_back( std::string( path + "/" + name ) ); } } } while( ( entry = readdir( root ) ) ); return retVal; }
int main(int argc, char *argv[]) { if( argc != 6 ) { std::cerr << "Usage: <image_directory> <horizontal_squares> <vertical_squares> <square_size> <stereo>" << std::endl; exit( EXIT_FAILURE ); } const bool stereo = atoi( argv[5] ) == 1; const int nx = atoi( argv[2] ), ny = atoi( argv[3] ); const float size = ( float )atof( argv[4] ); // Find Images const std::vector<std::string> image_paths = ImageFiles( std::string(argv[1]), 0 ); // Load Images std::vector<cv::Mat> images; for( size_t i = 0; i < image_paths.size(); i++ ){ images.push_back( cv::imread( image_paths[i], CV_LOAD_IMAGE_GRAYSCALE ) ); } if( !stereo ){ // Calculate Calibration CalibrateSingle( images, nx, ny, size ); }else{ // Sort Images std::vector<cv::Mat> left_images, right_images; for( size_t i = 0; i < image_paths.size(); i++ ) { if( image_paths[i].find( "left" ) != std::string::npos ) { left_images.push_back( images[i] ); } if( image_paths[i].find( "right" ) != std::string::npos ) { right_images.push_back( images[i] ); } } // Fail if we dont have enough if( left_images.empty() || left_images.size() != right_images.size() ) { std::cerr << "Uneven distribution of left and right images" << std::endl; exit( EXIT_FAILURE ); } // Calculate Calibration CalibrateStereo( left_images, right_images, nx, ny, size ); } return 0; }