コード例 #1
0
ファイル: Main.cpp プロジェクト: XinChenBug/CatsDogsDetection
void createTrainingDescriptors(vector<string> trainImages) {

	//Mat img;
	//string filepath;
	cout << "create training descriptors" << endl;
	Ptr<FeatureDetector> detector = FeatureDetector::create(detector_type);
	Ptr<DescriptorExtractor> extractor = DescriptorExtractor::create(extractor_type);
	if (detector_type == "SURF") {
		detector->set("hessianThreshold", minHessian);
		extractor->set("hessianThreshold", minHessian);
	}
	//Ptr<DescriptorExtractor> extractor = DescriptorExtractor::create(extractor_type);
	
	
	Mat *training_descriptors = new Mat();
	/* Extracting features from training set thar contains all classes */
	for (int i = 0; i < trainImages.size(); i++) {
		string filepath = dataset_dir + trainImages[i];
		if (!fileExists(filepath)) {
			cout << "Error opening file! " << endl;
			cout << "Error in: " << filepath << endl;
			continue;
		}

		vector<KeyPoint> keypoints;
		Mat img = imread(filepath, CV_LOAD_IMAGE_COLOR);
		detector->detect(img, keypoints);

		Mat descriptors;
		extractor->compute(img, keypoints, descriptors);
		training_descriptors->push_back(descriptors);

		if (i % 500 == 0)
			cout << "Iteration: " << i << " of " << trainImages.size() << endl;
	}

	cout << "Finished training descriptors" << endl;

	// Creates vocabulary
	cout << "Going to do vocabulary" << endl;
	/* Creating the vocabulary (bag of words) */
	BOWKMeansTrainer *bowtrainer = new BOWKMeansTrainer(numClusters);

	
	bowtrainer->add(*training_descriptors);

	delete training_descriptors;
	
	vocabulary = bowtrainer->cluster();

	delete bowtrainer;

	FileStorage fs("vocabulary.yml", FileStorage::WRITE);
	fs << "vocabulary" << vocabulary;
	fs.release();
	

	cout << "Finished vocabulary creation (1)" << endl;

}
コード例 #2
0
void BOW::train(const char *imagepath, const char *imagelists) {
    BOWKMeansTrainer trainer = BOWKMeansTrainer(200);
    Ptr<DescriptorExtractor> desext = SIFT::create();
    vector<KeyPoint> kps;
    Mat desp;
    Mat img;
    static char fullpath[100], filename[100];
    printf("Computing descriptors..\n");
    FILE* fin = fopen(imagelists, "r");
    int imgnum;
    fscanf(fin, "%d", &imgnum);
    int skipimg = 10;
    double kpratio = 0.3;
    for (int i = 0; i < imgnum; ++i)
    {
        fscanf(fin, "%s", filename);
        printf("%s, %d of %d..", filename, i+1, imgnum);
        if (i % skipimg)
        {
            printf("skipped\n");
            continue;
        }
        sprintf(fullpath, "%s%s", imagepath, filename);
        img = imread(fullpath);
//        if (img.rows > 100 && img.cols > 100)
//            img = Mat(img, Range(img.rows/5, img.rows*4/5), Range(img.cols/5, img.cols*4/5));
        if (img.cols > 300 || img.rows > 300)
        {
            double fr = min(300.0/img.rows, 300.0/img.cols);
            resize(img, img, Size(), fr, fr, INTER_AREA);
        }
        //GaussianBlur(img, img, Size(5, 5), 1.5, 1.5, BORDER_REPLICATE);
        ImgBGReplace(img, img);
        m_fd->detect(img, kps);
//        Mat show;
//        drawKeypoints(img, kps, show);
//        imshow("", show);
//        waitKey();
        desext->compute(img, kps, desp);
        static int idx[5000];
        for (int j = 0; j < desp.rows; ++j) idx[j] = j;
        int selectedkp = (int)(desp.rows * kpratio);
        for (int j = 0; j < selectedkp; ++j)
        {
            int r = rand() % (desp.rows - j);
            swap(idx[j], idx[r]);
            trainer.add(desp.row(idx[j]));
        }
        printf("Complete\n");
    }
    fclose(fin);
    printf("Complete\n");
    printf("%d descriptors extracted.\n", trainer.descriptorsCount());

    printf("Clustering...");
    desp = trainer.cluster();
    m_ext->setVocabulary(desp);
    printf("Complete\n");
}
コード例 #3
0
void gatherLocalFeatures()
{
	for (unsigned int i = 0; i < featuresVector.size(); i++)
	{
		for (unsigned int j = 0; j < featuresVector[i].size(); j++)
			bowTrainer.add(featuresVector[i][j]);
	}
}
コード例 #4
0
ファイル: meraocr.cpp プロジェクト: hishehim/Vision_OCR
/*
	processFeatures function process images and derives the features of each image which in our case is a character.
	These features/descriptors of the characters are then added to BOWKMeansTrainer which is a Bag of Features
	This function takes as input parameters a vector of images, this allows us to process all images in one place.
	We pass by reference our BOWKMeansTrainer (bag of features) since this is a variable we want available in main scope
	A pointer for DescriptorExtractor and SurfFeatureDetector is also passed from main since we define them there already.
	The function process each image by using SurfFeatureDetector to derive keypoints of the image/character.
	Using the pointer DescriptorExtractor extractor we are able to compute features for our character based on the character's keypoints.
	Then, we add the features computed to our bowTrainer (bag of features)
	Although the function is void, we are accessing bowTrainer from main scope so results will be stored there
	Overall, the main purpose of this is to allow us to build a vocabulary of features.
	However, we wouldn't be able to determine the corresponding character to features
*/
void processFeatures(vector<Mat> images, BOWKMeansTrainer &bowTrainer, Ptr<DescriptorExtractor> extractor, SiftFeatureDetector detector) {
    for (int j=0; j<images.size(); j++) {
        Mat image, src;

        resize(images.at(j), src, Size(0,0), 10,10);

        copyMakeBorder(src, image, 10,10,10,10,BORDER_CONSTANT, Scalar(255));


        vector<KeyPoint> keypoints;
        detector.detect(image, keypoints);
        Mat features;
        extractor->compute(image, keypoints, features);
        bowTrainer.add(features);

    }
}
コード例 #5
0
void clusterFeatures()
{
	Mat dictionary = bowTrainer.cluster();
	bowExtractor.setVocabulary(dictionary);
}
コード例 #6
0
void collectclasscentroids(SurfFeatureDetector &detector, Ptr<DescriptorExtractor> &extractor, BOWKMeansTrainer &bowTrainer, string trainingDir, bool runInBackground, bool writelog) {

    IplImage *img;
    vector<string> files = vector<string>();
    Helper helper;
    string event;
    char ch[30];

    // should put error correction here to check if directory exists

    helper.GetFileList(trainingDir, files);

    for (unsigned int iz = 0; iz < files.size(); iz++) {
        int isImage = helper.instr(files[iz], "jpg", 0, true);
        if (isImage > 0) {


            string sFileName = trainingDir;
            string sFeaturesDir = "/usr/local/share/archive-vision/build/features/";
            string sOutputImageFilename = "/usr/local/share/archive-vision/build/feature_point_images/";
            sFileName.append(files[iz]);
            sOutputImageFilename.append(files[iz]);
            sFeaturesDir.append(files[iz]);
            sFeaturesDir.append(".txt");
            const char * imageName = sFileName.c_str ();

            img = cvLoadImage(imageName,0);
            if (img) {
                string workingFile = files[iz];
                vector<KeyPoint> keypoint;
                detector.detect(img, keypoint);
                if (keypoint.size()) {
                    Mat features;
                    extractor->compute(img, keypoint, features);

                    event = "Processing " + workingFile;
                    helper.logEvent(event, 2, runInBackground, writelog);


                    //try to write out an image with the features highlighted
                    // Add results to image and save.
                    //				Mat output;
                    //				drawKeypoints(img, keypoint, output, Scalar(0, 128, 0), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
                    //				imwrite(sOutputImageFilename, output);




                    // try writing out all the feature, each to its own YML file and see what
                    // they look like
                    //				helper.WriteToFile(sFeaturesDir, features, "features");

                    bowTrainer.add(features);
                } else {
                    event = workingFile + "contains no keypoints.";
                    helper.logEvent(event, 1, runInBackground, writelog);
                }
            }


        }
    }
    return;
}