123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #include <opencv2/opencv.hpp>
- #include <opencv2/xfeatures2d.hpp>
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <filesystem>
- using namespace cv;
- using namespace cv::xfeatures2d;
- using namespace std;
- using namespace std::filesystem;
- typedef void process(const path& input, const path& output);
- const auto detector1 = SIFT::create();
- const auto detector2 = SURF::create();
- /// <summary>
- /// 提取图片特征描述符
- /// </summary>
- /// <param name="image">待提取图片路径</param>
- /// <param name="output">保存描述符路径</param>
- void process_image1(const path& image, const path& output) {
- ofstream fs(output.string().c_str());
- if (!fs.is_open()) {
- cout << "create file error!";
- }
- fs.close();
- Mat input_image = imread(image.string());
- vector<KeyPoint> keypoints_cat;
- Mat descriptor_cat;
- detector1->detectAndCompute(input_image, Mat(), keypoints_cat, descriptor_cat);
- cout << "before: " << descriptor_cat.type() << " - " << descriptor_cat.channels() << endl;
- //for (int i = 0; i < descriptor_cat.rows; i++) {
- // for (int j = 0; j < descriptor_cat.cols; j++) {
- // auto element = descriptor_cat.at<float>(i, j);
- // cout << element << endl;
- // }
- //}
- Mat convert;
- if (descriptor_cat.type() != CV_8U)
- {
- descriptor_cat.convertTo(convert, CV_8U, 1.0, 0);
- }
- else
- {
- convert = descriptor_cat;
- }
- cout << "after: " << convert.type() << " - " << convert.channels() << endl;
- imwrite(output.string(), convert);
- }
- void process_image2(const path& image, const path& output) {
- ofstream fs(output.string().c_str());
- if (!fs.is_open()) {
- cout << "create file error!";
- }
- fs.close();
- Mat input_image = imread(image.string());
- vector<KeyPoint> keypoints_cat;
- Mat descriptor_cat;
- detector2->detectAndCompute(input_image, Mat(), keypoints_cat, descriptor_cat);
- cout << "before: " << descriptor_cat.type() << " - " << descriptor_cat.channels() << endl;
- //for (int i = 0; i < descriptor_cat.rows; i++) {
- // for (int j = 0; j < descriptor_cat.cols; j++) {
- // auto element = descriptor_cat.at<float>(i, j);
- // cout << element << endl;
- // }
- //}
- Mat convert;
- if (descriptor_cat.type() != CV_8U)
- {
- descriptor_cat.convertTo(convert, CV_8U, 255.0, 0);
- }
- else
- {
- convert = descriptor_cat;
- }
- cout << "after: " << convert.type() << " - " << convert.channels() << endl;
- imwrite(output.string(), convert);
- }
- void process_image3(const path& image, const path& output) {
- ofstream fs(output.string().c_str());
- if (!fs.is_open()) {
- cout << "create file error!";
- }
- fs.close();
- Mat input_image = imread(image.string());
- vector<KeyPoint> keypoints_cat;
- Mat descriptor_cat;
- detector1->detectAndCompute(input_image, Mat(), keypoints_cat, descriptor_cat);
- cout << "before: " << descriptor_cat.type() << " - " << descriptor_cat.channels() << endl;
- //for (int i = 0; i < descriptor_cat.rows; i++) {
- // for (int j = 0; j < descriptor_cat.cols; j++) {
- // auto element = descriptor_cat.at<float>(i, j);
- // cout << element << endl;
- // }
- //}
- cout << "after: " << descriptor_cat.type() << " - " << descriptor_cat.channels() << endl;
- FileStorage file_storage(output.string(), FileStorage::WRITE);
- file_storage << "card-des" << descriptor_cat;
- file_storage.release();
- }
- void process_image4(const path& image, const path& output) {
- ofstream fs(output.string().c_str());
- if (!fs.is_open()) {
- cout << "create file error!";
- }
- fs.close();
- Mat input_image = imread(image.string());
- vector<KeyPoint> keypoints_cat;
- Mat descriptor_cat;
- detector2->detectAndCompute(input_image, Mat(), keypoints_cat, descriptor_cat);
- cout << "before: " << descriptor_cat.type() << " - " << descriptor_cat.channels() << endl;
- //for (int i = 0; i < descriptor_cat.rows; i++) {
- // for (int j = 0; j < descriptor_cat.cols; j++) {
- // auto element = descriptor_cat.at<float>(i, j);
- // cout << element << endl;
- // }
- //}
- FileStorage file_storage(output.string(), FileStorage::WRITE);
- file_storage << "card-des" << descriptor_cat;
- file_storage.release();
- }
- void deep_copy(const path& src, const path& des, const string& suffix, process* process) {
- if (is_regular_file(src)) {
- auto d = des.string().substr(0, des.string().size() - 3) + suffix + "\0";
- process(src, path(d));
- return;
- }
- if (!exists(des)) {
- create_directories(des);
- }
- for (auto& it : directory_iterator(src)) {
- string current = it.path().lexically_relative(it.path().parent_path()).string();
- path des_next(des);
- deep_copy(it.path(), des_next /= current, suffix, process);
- }
- }
- void process_mat(string input_dir, string output_dir, const string& suffix, process* process) {
- path input_dir_path(input_dir.c_str());
- path output_dir_path(output_dir.c_str());
- if (!exists(input_dir_path)) {
- cout << "请设置正确的卡片存放路径" << endl;
- }
- if (exists(output_dir_path)) {
- remove_all(output_dir_path);
- }
- else {
- create_directories(output_dir_path);
- }
- directory_entry input_entry(input_dir_path);
- assert(input_entry.status().type() == file_type::directory);
- copy(input_dir_path, output_dir_path);
- deep_copy(input_dir_path, output_dir_path, suffix, process);
- }
- int main(int argc, char** argv) {
- string input_dir = "D:\\Work\\Cards";
- //string output_dir = "D:\\Work\\Outputs";
- string output_dir1 = "D:\\Work\\SIFT";
- string output_dir2 = "D:\\Work\\SURF";
- string output_dir3 = "D:\\Work\\SIFT-XML";
- string output_dir4 = "D:\\Work\\SURF-XML";
- process_mat(input_dir, output_dir1, "png", process_image1);
- process_mat(input_dir, output_dir2, "png", process_image2);
- process_mat(input_dir, output_dir3, "xml", process_image3);
- process_mat(input_dir, output_dir4, "xml", process_image4);
- return 0;
- }
|