#include #include #include #include #include #include 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(); /// /// 提取图片特征描述符 /// /// 待提取图片路径 /// 保存描述符路径 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 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(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 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(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 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(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 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(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; }