Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <opencv2/highgui.hpp>
- #include <opencv2/xfeatures2d.hpp>
- #include <opencv2/core.hpp>
- #include <opencv2/features2d.hpp>
- #include <opencv2/imgproc.hpp>
- #include "dlib/image_processing.h"
- #include "dlib/string.h"
- #include "dlib/opencv.h"
- std::vector<std::string> labels{ "dolphin", "ibis" };
- std::string filename;
- std::vector<cv::KeyPoint> kpts;
- cv::Mat desc;
- cv::Mat feats_unclustered;
- cv::Ptr<cv::Feature2D> sift_detector = cv::xfeatures2d::SIFT::create();
- cv::Mat image;
- // Get the keypoints and compute descriptors for all the images in the dataset.
- for (int class_id = 0; class_id < labels.size(); class_id++) {
- for (int image_id = 1; image_id < 51; image_id++) {
- filename = "./data/" + labels[class_id] + "/image_" + dlib::pad_int_with_zeros(image_id, 4) + ".jpg";
- image = cv::imread(filename, cv::IMREAD_GRAYSCALE);
- if (image.empty()) {
- std::cout << filename << " cannot read\n";
- }
- sift_detector->detectAndCompute(image, cv::Mat(), kpts, desc);
- feats_unclustered.push_back(desc);
- }
- }
- // Build the vocabulary by clustering the obtained descriptors and obtain 200 words.
- int dict_size = 200;
- cv::TermCriteria tc(cv::TermCriteria::MAX_ITER, 100, 0.01);
- int retires = 1;
- int flag = cv::KMEANS_PP_CENTERS;
- cv::BOWKMeansTrainer bow_trainer(dict_size, tc, retires, flag);
- cv::Mat dictionary = bow_trainer.cluster(feats_unclustered);
- // Save dictionary to YML file
- cv::FileStorage fs("dictionary.yml", cv::FileStorage::WRITE);
- fs << "vocabulary" << dictionary;
- fs.release();
- //// write to CSV file
- //std::ofstream ofs;
- //ofs.open("dictionary_trained.csv");
- //ofs << cv::format(dict_trained, cv::Formatter::FMT_CSV) << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement