Ejemplo n.º 1
0
int main(int argc, char** argv) {

	// check arguments
	if (argc != 3 && argc != 4) {
		printf("Usage: PartsBasedDetector model_file image_file [depth_file]\n");
		exit(-1);
	}

	// determine the type of model to read
	boost::scoped_ptr<Model> model;

	string ext = boost::filesystem::path(argv[1]).extension().string();
	if (ext.compare(".xml") == 0 || ext.compare(".yaml") == 0) {
		model.reset(new FileStorageModel);
	} else if (ext.compare(".mat") != 0) {
    model.reset(new MatlabIOModel);
  } else {
		printf("Unsupported model format: %s\n", ext.c_str());
		exit(-2);
  }

  bool ok = model->deserialize(argv[1]);
	if (!ok) {
		printf("Error deserializing file\n");
		exit(-3);
	}

	// create the PartsBasedDetector and distribute the model parameters
	PartsBasedDetector<float> pbd;
	pbd.distributeModel(*model);

	// load the image from file
	Mat_<float> depth;
	Mat im = imread(argv[2]);
  if (im.empty()) {
      printf("Image not found or invalid image format\n");
      exit(-4);
  }

	// detect potential candidates in the image
	double t = (double)getTickCount();

	vector<Candidate> candidates;
	pbd.detect(im, depth, candidates);
	printf("Detection time: %f\n", ((double)getTickCount() - t)/getTickFrequency());
	printf("Number of candidates: %ld\n", candidates.size());

	// display the best candidates
	Visualize visualize(model->name());
	SearchSpacePruning<float> ssp;
  Mat canvas;
	if (candidates.size() > 0) {
	    Candidate::sort(candidates);
	    //Candidate::nonMaximaSuppression(im, candidates, 0.2);
	    visualize.candidates(im, candidates, canvas, true);
      visualize.image(canvas);
	    waitKey();
	}
	return 0;
}
Ejemplo n.º 2
0
int main(int argc, char** argv) {

	/*int padding = 12;
	int interval = 10;

	// check arguments
	if (argc != 3 && argc != 4) {
		printf("Usage: PartsBasedDetector model_file image_file [depth_file]\n");
		exit(-1);
	}

	vector<FFLD::HOGPyramid::Matrix> scores;
	vector<FFLD::Mixture::Indices> argmaxes;
	vector<vector<vector<FFLD::Model::Positions> > > positions;
	const string file("../tmp.jpg");
	cout << file << endl;
	FFLD::JPEGImage image(file);
	FFLD::HOGPyramid pyramidFFLD(image, padding, padding, interval);
	if (!FFLD::Patchwork::Init((pyramidFFLD.levels()[0].rows() - padding + 15) & ~15,
							(pyramidFFLD.levels()[0].cols() - padding + 15) & ~15)) {
		cerr << "\nCould not initialize the Patchwork class" << endl;
		return -1;
	}
		
	cout << "Initialized FFTW in " << stop() << " ms" << endl;
	FFLD::Mixture mixture;
	string modelffld("../model_car_final-1.2.4.txt");
	cout << modelffld << endl;
	ifstream in(modelffld.c_str(), ios::binary);
	
	if (!in.is_open()) {
		//showUsage();
		cerr << "\nInvalid model file " << modelffld << endl;
		return -1;
	}
	in >> mixture;

	//mixture.convolve(pyramidFFLD, scores, argmaxes, &positions);//full ffld run
	cout << "Convolution :p " << endl;
	const int nbModels = mixture.models().size();
	const int nbLevels = pyramidFFLD.levels().size();
	// Convolve with all the models
	vector<vector<FFLD::HOGPyramid::Matrix> > tmp(nbModels);
	//convolve(pyramid, tmp, positions);//going inside
		// Transform the filters if needed
#pragma omp critical
	//if (mixture.filterCache_.empty())
		mixture.cacheFilters();
	
	// Create a patchwork
	const FFLD::Patchwork patchwork(pyramidFFLD);
		// Convolve the patchwork with the filters
	std::vector<FFLD::Patchwork::Filter> filterCache_;
	 filterCache_ = mixture.filterCacheObj();
	vector<vector<FFLD::HOGPyramid::Matrix> > convolutions(filterCache_.size());
	patchwork.convolve(filterCache_, convolutions);///convolve patch with filters, 
	 const int tmpnbFilters = 54, tmpnbPlanes = 12, tmpnbLevels = 41;
	 //nb filters is number of filters (Read from model.txt)
	 // nblevels is number of HOGPyramid levels
	// nbplanes comes from converting pyramid to patchwork, rectangle logic ?
	/* Mat C;
	for (int i = 0; i < tmpnbFilters * tmpnbPlanes; ++i) {
		const int k = i / tmpnbPlanes; // Filter index
		const int l = i % tmpnbPlanes; // Plane index
		for (int j = 0; j < tmpnbLevels; ++j) {
			FFLD::HOGPyramid::Matrix ffldResponse = convolutions[k][j];
			FFLD::HOGPyramid::Matrix tempffldResponse;
			eigen2cv(ffldResponse,C);
			cv2eigen(C,tempffldResponse);
			cout << "ffldResponse dim " << ffldResponse.rows() << " " << ffldResponse.cols() << " C dim " << C.rows << " " << C.cols << " ffld::Eigen dim " << tempffldResponse.rows() << " " << tempffldResponse.cols() <<endl;
		}
	}*/
	
	//cout << " convolution size " << convolutions.size() << "rows " << convolutions[1].rows() << endl;
	// convert the convolusions Eigen matrix to cvMat obj

	//cout << " Mixture model size " << nbModels << endl;
	


	// determine the type of model to read
	cout << " Now starting Bristow " << endl;
	boost::scoped_ptr<Model> model;
	string ext = boost::filesystem::path(argv[1]).extension().string();
	if (ext.compare(".xml") == 0 || ext.compare(".yaml") == 0) {
		model.reset(new FileStorageModel);
	}
#ifdef WITH_MATLABIO
	else if (ext.compare(".mat") == 0) {
		model.reset(new MatlabIOModel);
	}
#endif
	else {
		printf("Unsupported model format: %s\n", ext.c_str());
		exit(-2);
	}
	bool ok = model->deserialize(argv[1]);
	if (!ok) {
		printf("Error deserializing file\n");
		exit(-3);
	}

	cout << " Calling Distribute Model " << endl;
	// create the PartsBasedDetector and distribute the model parameters
	PartsBasedDetector<float> pbd;
	pbd.distributeModel(*model);

	cout << " After Distribute Model " << endl;
	// load the image from file
	Mat_<float> depth;
	Mat im = imread(argv[2]);
        if (im.empty()) {
            printf("Image not found or invalid image format\n");
            exit(-4);
        }
	if (argc == 4) {
		depth = imread(argv[3], CV_LOAD_IMAGE_ANYDEPTH);
		// convert the depth image from mm to m
		depth = depth / 1000.0f;
	}
	
	// detect potential candidates in the image
	double t = (double)getTickCount();
	vector<Candidate> candidates;
	pbd.detect(im, depth, candidates);
	cout << " image dim " << im.rows << " " << im.cols << endl;
	printf("Detection time: %f\n", ((double)getTickCount() - t)/getTickFrequency());
	printf("Number of candidates: %ld\n", candidates.size());

	// display the best candidates
	Visualize visualize(model->name());
	SearchSpacePruning<float> ssp;
        Mat canvas;
	if (candidates.size() > 0) {
	    Candidate::sort(candidates);
	    Candidate::nonMaximaSuppression(im, candidates, 0.2);
	    visualize.candidates(im, candidates, canvas, true);
            visualize.image(canvas);
	    waitKey();
	}
	return 0;
}