Пример #1
0
/**  Saves index on disk by using single or multiple files, having 
	proper extensions. */
int save_index (void *index, char *filename) {
	char *basename = filename;
	twcsa *wcsa=(twcsa *) index;

	fprintf(stderr,"\nSaving structures to disk: %s.*\n",basename);			
	fflush(stderr);

	// Saves the vocabulary of words
	saveVocabulary (index, basename);

	// Saves some configuration constants: maxNumOccs, sourceTextSize */
	saveIndexConstants (index, filename);
	
	#ifdef FREQ_VECTOR_AVAILABLE	
	// saves freqVector do disk
	saveFreqVector (wcsa->freqs,   wcsa->nwords, basename);	
	#endif
				
	// Saving the Compressed Structure of posting lists (il) 	
	int error = save_il(wcsa->ils, basename);
	IFERRORIL(error);
	fprintf(stderr,"\n \t**saved il_list");
	fflush(stderr);

	// Saving the Representation of the source text
	save_representation(wcsa->ct, filename);

	fprintf(stderr,"\n \t**saved compressed representation of the source text");
	fflush(stderr);

	return 0;
}
Пример #2
0
bool BowVocabulary::computeVocabulary(Mat& vocabularyOut, const string& vocabularyImgsList, bool outputAnalyzedImages, bool useOnlyTargetRegions) {
	if (loadVocabulary(vocabularyOut)) {
		return true;
	}	

	_bowTrainer->clear();

	ifstream imgsList(vocabularyImgsList);
	if (imgsList.is_open()) {		
		vector<string> fileNames;
		string filename;
		while (getline(imgsList, filename)) {									
			fileNames.push_back(filename);
		}
		int numberOfFiles = fileNames.size();


		cout << "    -> Building vocabulary with " << numberOfFiles << " images..." << endl;
		PerformanceTimer performanceTimer;
		performanceTimer.start();

		int descriptorsOriginalMatrixType = CV_32FC1;

		//#pragma omp parallel for schedule(dynamic)
		for (int i = 0; i < numberOfFiles; ++i) {
			Mat imagePreprocessed;
			string imageFilename = IMGS_DIRECTORY + fileNames[i] + IMAGE_TOKEN;
			if (_imagePreprocessor->loadAndPreprocessImage(imageFilename, imagePreprocessed, CV_LOAD_IMAGE_GRAYSCALE, false)) {
				Mat outputImage;
				if (outputAnalyzedImages) {
					outputImage = imagePreprocessed.clone();
				}

				if (useOnlyTargetRegions) {
					vector<Mat> masks;
					ImageUtils::retriveTargetsMasks(IMGS_DIRECTORY + fileNames[i], masks);
					for (size_t maskIndex = 0; maskIndex < masks.size(); ++maskIndex) {
						vector<KeyPoint> keypoints;
						Mat targetMask = masks[maskIndex];
						_featureDetector->detect(imagePreprocessed, keypoints, targetMask);						
						//_featureDetector->detect(imagePreprocessed, keypoints, masks[maskIndex]);

						if (keypoints.size() > 3) {
							Mat descriptors;
							_descriptorExtractor->compute(imagePreprocessed, keypoints, descriptors);
							descriptorsOriginalMatrixType = descriptors.type();
							descriptors.convertTo(descriptors, CV_32FC1);

							if (descriptors.rows > 0) {
								//#pragma omp critical
								_bowTrainer->add(descriptors);
							}

							if (outputAnalyzedImages) {
								cv::drawKeypoints(outputImage, keypoints, outputImage);
							}
						}
					}
				} else {
					vector<KeyPoint> keypoints;
					_featureDetector->detect(imagePreprocessed, keypoints);

					if (keypoints.size() > 3) {
						Mat descriptors;
						_descriptorExtractor->compute(imagePreprocessed, keypoints, descriptors);
						descriptorsOriginalMatrixType = descriptors.type();
						descriptors.convertTo(descriptors, CV_32FC1);

						if (descriptors.rows > 0) {
							//#pragma omp critical
							_bowTrainer->add(descriptors);
						}

						if (outputAnalyzedImages) {
							cv::drawKeypoints(outputImage, keypoints, outputImage);
						}
					}					
				}
				
				if (outputAnalyzedImages) {
					stringstream imageOutputFilename;
					imageOutputFilename << VOCABULARY_BUILD_OUTPUT_DIRECTORY << fileNames[i] << FILENAME_SEPARATOR << _vocabularyFilename << IMAGE_OUTPUT_EXTENSION;
					imwrite(imageOutputFilename.str(), outputImage);
				}				
			}
		}
		vocabularyOut = _bowTrainer->cluster();
		saveVocabulary(vocabularyOut);

		vocabularyOut.convertTo(vocabularyOut, descriptorsOriginalMatrixType);
		_bowImgDescriptorExtractor->setVocabulary(vocabularyOut);		
		cout << "    -> Finished building vocabulary with " << vocabularyOut.rows << " word size in " << performanceTimer.getElapsedTimeFormated() << "\n" << endl;
		

		_bowTrainer->clear();
		return true;
	}
	
	return false;
}