|
@@ -10,13 +10,16 @@ using namespace cv::xfeatures2d;
|
|
|
using namespace std;
|
|
|
using namespace std::filesystem;
|
|
|
|
|
|
-const auto detector = SIFT::create();
|
|
|
+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_image(const path& image, const path& output) {
|
|
|
+void process_image1(const path& image, const path& output) {
|
|
|
ofstream fs(output.string().c_str());
|
|
|
if (!fs.is_open()) {
|
|
|
cout << "create file error!";
|
|
@@ -27,28 +30,129 @@ void process_image(const path& image, const path& output) {
|
|
|
|
|
|
vector<KeyPoint> keypoints_cat;
|
|
|
|
|
|
- Mat descriptor_cat, descriptor_smallCat;
|
|
|
- detector->detectAndCompute(input_image, Mat(), keypoints_cat, descriptor_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_32F)
|
|
|
+ if (descriptor_cat.type() != CV_8U)
|
|
|
{
|
|
|
- descriptor_cat.convertTo(convert, CV_32F, 1.0 / 255, 0);
|
|
|
+ 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 deep_copy(const path& src, const path& des) {
|
|
|
+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) + "png\0";
|
|
|
+ auto d = des.string().substr(0, des.string().size() - 3) + suffix + "\0";
|
|
|
|
|
|
- process_image(src, path(d));
|
|
|
+ process(src, path(d));
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -59,15 +163,11 @@ void deep_copy(const path& src, const path& 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);
|
|
|
+ deep_copy(it.path(), des_next /= current, suffix, process);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-int main(int argc, char** argv) {
|
|
|
- string input_dir = "D:\\Work\\Cards";
|
|
|
- //string output_dir = "D:\\Work\\Outputs";
|
|
|
- string output_dir = "D:\\Work\\SIFT";
|
|
|
-
|
|
|
+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());
|
|
|
|
|
@@ -88,7 +188,21 @@ int main(int argc, char** argv) {
|
|
|
|
|
|
copy(input_dir_path, output_dir_path);
|
|
|
|
|
|
- deep_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;
|
|
|
}
|