void TestObjectRecognition::addTrainingSet(cv::Mat &trainingData, cv::Mat &labels, int elemCount, int label,
                                           int offset) {
	for (int i = offset; i < elemCount + offset; i++) {
		trainingData.push_back(featureDetector->detectImageFeatures(i + 1, label));
		labels.push_back(label);
	}
}
Exemplo n.º 2
0
//codification of the images with the BOW vocabulary
void bow_encode(const fs::path& basepath, cv::Mat& descriptors, cv::Mat& labels)
{
	int no_files = 0;
	std::string class_name = basepath.string();
	class_name.erase(class_name.begin(),class_name.end()-2);

	for(fs::directory_iterator iter(basepath), end; iter != end; ++iter)
	{
		fs::directory_entry entry = *iter;
		if(fs::is_directory(entry.path()))
		{
			std::cout << "Processing directory: " << entry.path().string() << std::endl;
			bow_encode(entry.path(),descriptors,labels);
		}
		else
		{
			fs::path entryPath = entry.path();
			if(entryPath.extension()==".jpg")
			{
			//	std::cout << "Processing file: " << entry.path().string() << std::endl;
				no_files++;
				cv::Mat img = cv::imread(entryPath.string(),CV_LOAD_IMAGE_COLOR);
				if(!img.empty())
				{
					std::vector<cv::KeyPoint> feature_keypoints;
					feature_detector.detect(img,feature_keypoints);
					if(feature_keypoints.empty())
					{
						std::cerr << "Could not find points in image: " << entryPath.string();
						std::cerr << std::endl;
					}
					else
					{

						cv::Mat bowDescriptor;
						bowDE.compute(img,feature_keypoints,bowDescriptor);
						descriptors.push_back(bowDescriptor);
						//std::cout << class_name.c_str() << std::endl;
						labels.push_back( float( atoi(class_name.c_str()) ) );
						feature_keypoints.clear();
						//std::cout << local_descriptors.rows << std::endl;	
						~bowDescriptor;

					}
				}
				else
				{
					std::cerr << "Could not read image: " << entryPath.string() << std::endl;
				}
												
			}												
		}							
		
	}		
	std::cout << "No files: " << no_files << std::endl;

}
Exemplo n.º 3
0
    // TextureFeature::Classifier
    virtual int predict(const cv::Mat &testFeature, cv::Mat &results) const
    {
        int best = -1;
        double mind=DBL_MAX;
        nearest(testFeature, features, best, mind, *this);

        int found = best>-1 ? labels.at<int>(best) : -1;
        results.push_back(float(found));
        results.push_back(float(mind));
        results.push_back(float(best));
        return 3;
    }
  void ODFeatureDetector2D::findSiftGPUDescriptors(char const *image_name, cv::Mat &descriptors, vector<cv::KeyPoint> &keypoints)
  {
    sift_gpu_->RunSIFT(image_name);

    int nFeat = sift_gpu_->GetFeatureNum();//get feature count
    //allocate memory for readback
    vector<SiftGPU::SiftKeypoint> keys(nFeat);
    //read back keypoints and normalized descritpros
    //specify NULL if you don’t need keypoints or descriptors
    vector<float> imageDescriptors(128 * nFeat);
    sift_gpu_->GetFeatureVector(&keys[0], &imageDescriptors[0]);

    sift_gpu_->SaveSIFT("1.sift");

    //to opencv format
    keypoints.clear();
    descriptors.create(0, 128, CV_32FC1);
    for(int i = 0; i < nFeat; ++i) {
      cv::KeyPoint key(keys[i].x, keys[i].y, keys[i].s, keys[i].o);
      keypoints.push_back(key);
      cv::Mat descriptor(1, 128, CV_32FC1);
      for(int x = 0; x < 128; x++) descriptor.at<float>(x) = floor(0.5 + 512.0f * imageDescriptors[(i << 7) + x]);
      descriptors.push_back(descriptor);
    }
    cv::Mat image = cv::imread(image_name);
  }
Exemplo n.º 5
0
cv::Mat_<int> extractProHOGFeatures( const vector<TrainingSet> &xs,
                    int nbins, const cv::Size &cdims, cv::Mat &trows)
{
    cerr << "Extracting feature vectors from " << xs.size() << " training sets..." << endl;
    cv::Mat_<int> labels(0,0,CV_32SC1);
    trows.create(0,0,CV_32FC1);

    int fvsz = 0;
    int dataCount = 0;
    int lab = -1;
    BOOST_FOREACH( const TrainingSet &ts, xs)
    {
        lab++;  // New dataset label
        BOOST_FOREACH( const cv::Mat &x, ts)
        {
            RFeatures::ProHOG prohog( x, nbins);
            const cv::Mat pfvRaw = prohog( cdims);
            cv::Mat pfv32;
            pfvRaw.convertTo( pfv32, CV_32F);
            const cv::Mat pfv = pfv32.reshape(1,1); // Single row CV_32FC1
            if ( fvsz == 0)
                fvsz = pfv.cols;
            assert( fvsz == pfv.cols);
            trows.push_back(pfv);
            labels.push_back(lab);
            dataCount++;
        }   // end foreach
Exemplo n.º 6
0
void Feature::SURFExtractor_GPU(vector<cv::KeyPoint> &feature_pts,
	vector_eigen_vector3f &feature_pts_3d,
	cv::Mat &feature_descriptors,
	const cv::Mat &imgRGB, const cv::Mat &imgDepth)
{
	cv::Mat grey;
	cv::cvtColor(imgRGB, grey, CV_BGR2GRAY);
	//cv::gpu::printShortCudaDeviceInfo(cv::gpu::getDevice());
	cv::gpu::GpuMat gpuRGB, gpuKeypoints, gpuDescriptors;
	gpuRGB.upload(grey);
	cv::gpu::SURF_GPU surf;

	surf(gpuRGB, cv::gpu::GpuMat(), gpuKeypoints, gpuDescriptors);

	vector<cv::KeyPoint> fpts;
	cv::Mat descriptors;
	surf.downloadKeypoints(gpuKeypoints, fpts);
	gpuDescriptors.download(descriptors);

	for (int i = 0; i < fpts.size(); i++)
	{
		if (imgDepth.at<ushort>(cvRound(fpts[i].pt.y), cvRound(fpts[i].pt.x)) == 0)
			continue;
		feature_pts.push_back(fpts[i]);
		Eigen::Vector3f pt = ConvertPointTo3D(fpts[i].pt.x, fpts[i].pt.y, imgDepth);
		feature_pts_3d.push_back(pt);
		feature_descriptors.push_back(descriptors.row(i));
	}
}
Exemplo n.º 7
0
void Feature::SIFTExtractor(vector<cv::KeyPoint> &feature_pts,
	vector_eigen_vector3f &feature_pts_3d,
	cv::Mat &feature_descriptors,
	const cv::Mat &imgRGB, const cv::Mat &imgDepth)
{
	cv::SIFT sift_detector;
	cv::Mat mask, descriptors;
	vector<cv::KeyPoint> fpts;

	sift_detector(imgRGB, mask, fpts, descriptors);

	for (int i = 0; i < fpts.size(); i++)
	{
		if (imgDepth.at<ushort>(cvRound(fpts[i].pt.y), cvRound(fpts[i].pt.x)) == 0)
			continue;
		
		feature_pts.push_back(fpts[i]);
		Eigen::Vector3f pt = ConvertPointTo3D(fpts[i].pt.x, fpts[i].pt.y, imgDepth);
		feature_pts_3d.push_back(pt);

		double norm = 0.0;
		for (int j = 0; j < descriptors.cols; j++)
		{
			norm += descriptors.at<float>(i, j) * descriptors.at<float>(i, j);
		}
		norm = sqrt(norm);
		for (int j = 0; j < descriptors.cols; j++)
		{
			descriptors.at<float>(i, j) /= norm;
		}
		feature_descriptors.push_back(descriptors.row(i));
	}
}
Exemplo n.º 8
0
void concatChannels(const cv::Mat& src, cv::Mat& dst) {
    std::vector<cv::Mat> channels;
    cv::split(src, channels);
    dst = cv::Mat();
    for (VMit vmit = channels.begin(); vmit != channels.end(); ++vmit) {
        dst.push_back(*vmit);
    }
}
Exemplo n.º 9
0
//analysis operator: action analyse; takes the phase signal and gives the sequence of coefficients (representation realm)
//from phase signal space to zernike representation
void Zernike::analyse(const cv::Mat& sig, cv::Mat& z_coeffs)
{
  z_coeffs.release();
  for(cv::Mat z_j : base_)
  {
    double l2 = cv::norm(z_j, cv::NORM_L2);
    double inner_prod = z_j.dot(sig)/(l2*l2);
    z_coeffs.push_back( inner_prod );
  }
}
Exemplo n.º 10
0
static void randomFat(const vector<cv::Mat> &input, cv::Mat &fat)
{
    cv::Mat_<int> randIdx(1, input.size());
    randomIndex(randIdx);

    for (size_t i=0; i<input.size(); i++)
    {
        int idx = randIdx(i);
        fat.push_back(input[idx].reshape(1,1));
    }
}
Exemplo n.º 11
0
 //
 // concatVectorToMatVertical
 //
 bool concatVectorToMatVertical (const std::vector<float> &vector, cv::Mat &mat)
 {
   if (vector.size () != mat.cols) {
     std::cout << "### Size of vector should be equal to cols of Mat." << std::endl;
     debug (vector.size ());  debug (mat.cols);
     return false;
   }
   cv::Mat temp (vector);
   cv::Mat temp_t = temp.t ();
   mat.push_back (temp_t);
   return true;
 }
void histogram_generator_eyes::compute_histogram(cv::Mat& image, cv::Mat& histogram){

    //Releasing the memory
    histogram.release();

    //Mat aux individual descriptor
    cv::Mat aux_descriptors;
    cv::Mat aux_histogram;
    cv::Mat aux_histogram2;

    //Aux info to compute the boxes
    int aux_cols;
    int aux_rows;
    //Boxes
    std::vector<box> boxes;

    //Cropped aux image
    cv::Mat cropped_image;

    //getting the size of the image
    aux_cols=image.cols;
    aux_rows=image.rows;

    //Getting the boxes

    std::vector<int> binx;
    std::vector<int> biny;
    binx.push_back(1);
    biny.push_back(1);
    boxes=generateSpatialBoxes(aux_rows, aux_cols, binx , biny);


    //Crop the images and extraction features
    for(unsigned int j=0;j<boxes.size();j++){

        cropped_image= image(cv::Range(boxes.at(j).minY, boxes.at(j).maxY), cv::Range(boxes.at(j).minX, boxes.at(j).maxX));
        aux_descriptors=feature_extract->extract_dsift(cropped_image,2,4);
        aux_histogram2=clustering->get_histogram(aux_descriptors);

        transpose(aux_histogram2, aux_histogram2);
        aux_histogram.push_back(aux_histogram2);

    }

    transpose(aux_histogram, aux_histogram);
    histogram.push_back(aux_histogram);
    aux_histogram.release();

}
Exemplo n.º 13
0
void shuffleRows(const cv::Mat &matrix, cv::Mat& shuffleMatrix)
{
  shuffleMatrix.release();
  std::vector <int> seeds;
  for (int cont = 0; cont < matrix.rows; cont++)
  {
    seeds.push_back(cont);
  }
  cv::theRNG() = cv::RNG( cv::getTickCount() );
  cv::randShuffle(seeds);

  for (int cont = 0; cont < matrix.rows; cont++)
  {
    shuffleMatrix.push_back(matrix.row(seeds[cont]));
  }
}
Exemplo n.º 14
0
void Feature::ORBExtractor(vector<cv::KeyPoint> &feature_pts,
	vector_eigen_vector3f &feature_pts_3d,
	cv::Mat &feature_descriptors,
	const cv::Mat &imgRGB, const cv::Mat &imgDepth)
{
	cv::ORB orb_detector;
	cv::Mat mask, descriptors;
	vector<cv::KeyPoint> fpts;

	orb_detector(imgRGB, mask, fpts, descriptors);
	for (int i = 0; i < fpts.size(); i++)
	{
		if (imgDepth.at<ushort>(cvRound(fpts[i].pt.y), cvRound(fpts[i].pt.x)) == 0)
			continue;
		feature_pts.push_back(fpts[i]);
		Eigen::Vector3f pt = ConvertPointTo3D(fpts[i].pt.x, fpts[i].pt.y, imgDepth);
		feature_pts_3d.push_back(pt);
		feature_descriptors.push_back(descriptors.row(i));
	}
}
Exemplo n.º 15
0
void SketchRecognizer::readLabelsMat(const std::string &labels, cv::Mat &labelsMat)
{
    std::ifstream labels_in(labels.c_str());
    uint labels_size = 0;
    labels_in >> labels_size;
    uint class_num = 0;
    labels_in >> class_num;

    _labels.clear();
    char line[1024] = {0};
    labels_in.getline(line, sizeof(line)); //get '\n'

    for(uint i = 0; i < labels_size; i++) {
        labels_in.getline(line, sizeof(line));
        _labels.push_back(line);
        cv::Mat temp = cv::Mat::ones(class_num, 1, CV_32FC1) * i;
        labelsMat.push_back(temp);
    }

    labels_in.close();
}
Exemplo n.º 16
0
void SketchRecognizer::readSketchsMat(const std::string &sketchs, cv::Mat &sketchsMat)
{
    std::ifstream sketchs_in(sketchs.c_str());
    uint sketchsize = 0;
    sketchs_in >> sketchsize;
    uint vocabularySize = 0;
    sketchs_in >> vocabularySize;

    Vec_f32_t sketch(vocabularySize);
    for(uint i = 0; i < sketchsize; i++) {

        cv::Mat temp = cv::Mat::zeros(1, vocabularySize, CV_32FC1);
        for(uint j = 0; j < vocabularySize; j++) {
            sketchs_in >> temp.at<float>(0, j);
        }
        sketchsMat.push_back(temp);
        std::cout << "read sketchs " << i <<"/" << sketchsize << "\r" <<std::flush;
    }
    std::cout << "read sketchs " << sketchsize <<"/" << sketchsize << "\n" <<std::flush;
    sketchs_in.close();
}
  void ODFeatureDetector2D::findSiftGPUDescriptors1(cv::Mat const &image, cv::Mat &descriptors, vector<cv::KeyPoint> &keypoints)
  {
    unsigned char *data = image.data;
    cv::Mat greyimage;
    if(image.type() != CV_8U) {
      cv::cvtColor(image, greyimage, cv::COLOR_BGR2GRAY);
      data = greyimage.data;
    }
    sift_gpu_->RunSIFT(image.cols, image.rows, data, GL_LUMINANCE, GL_UNSIGNED_BYTE);

    int nFeat = sift_gpu_->GetFeatureNum();//get feature count
    //allocate memory for readback
    vector<SiftGPU::SiftKeypoint> keys(nFeat);
    //read back keypoints and normalized descritpros
    //specify NULL if you don’t need keypoints or descriptors
    vector<float> imageDescriptors(128 * nFeat);
    sift_gpu_->GetFeatureVector(&keys[0], &imageDescriptors[0]);

    sift_gpu_->SaveSIFT("2.sift");

    //to opencv format
    keypoints.clear();
    descriptors.create(0, 128, CV_32FC1);
    for(int i = 0; i < nFeat; ++i) {
      cv::KeyPoint key(keys[i].x, keys[i].y, keys[i].s, keys[i].o);
      keypoints.push_back(key);
      cv::Mat descriptor(1, 128, CV_32FC1);

      for(int x = 0; x < 128; x++)
        descriptor.at<float>(x) = floor(0.5 + (512.0f * imageDescriptors[(i * 128) + x]));

      descriptors.push_back(descriptor);
    }


    //viewImage(image, keypoints);

  }