Bladeren bron

change code

zhaoyadi 2 jaren geleden
bovenliggende
commit
e3b620e114

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+.vs/

BIN
.vs/OpenCVDetect/FileContentIndex/4579e54f-274c-4bb5-9212-c62bb60e85a6.vsidx


BIN
.vs/OpenCVDetect/FileContentIndex/6586f09f-becc-40a8-8897-34966a793ba4.vsidx


BIN
.vs/OpenCVDetect/FileContentIndex/86dc325f-ed1d-4cbd-a25e-1fc0ae7dc5c5.vsidx


BIN
.vs/OpenCVDetect/v17/.suo


BIN
.vs/OpenCVDetect/v17/Browse.VC.db


BIN
.vs/OpenCVDetect/v17/ipch/AutoPCH/8ec6e121cb2885c7/OPENCVDETECT.ipch


+ 130 - 16
OpenCVDetect/OpenCVDetect.cpp

@@ -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;
 }

BIN
OpenCVDetect/x64/Debug/OpenCVDetect.ilk


+ 3 - 1
OpenCVDetect/x64/Debug/OpenCVDetect.log

@@ -1 +1,3 @@
-
+  OpenCVDetect.cpp
+D:\Code\build\install\include\opencv2\xfeatures2d.hpp(1,1): warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
+  OpenCVDetect.vcxproj -> D:\Project\CppPorjects\OpenCVDetect\x64\Debug\OpenCVDetect.exe

BIN
OpenCVDetect/x64/Debug/OpenCVDetect.obj


BIN
OpenCVDetect/x64/Debug/OpenCVDetect.tlog/CL.command.1.tlog


BIN
OpenCVDetect/x64/Debug/OpenCVDetect.tlog/CL.read.1.tlog


BIN
OpenCVDetect/x64/Debug/OpenCVDetect.tlog/CL.write.1.tlog


+ 2 - 0
OpenCVDetect/x64/Debug/OpenCVDetect.tlog/OpenCVDetect.lastbuildstate

@@ -0,0 +1,2 @@
+PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.34.31933:TargetPlatformVersion=10.0.22000.0:
+Debug|x64|D:\Project\CppPorjects\OpenCVDetect\|

BIN
OpenCVDetect/x64/Debug/OpenCVDetect.tlog/link.command.1.tlog


BIN
OpenCVDetect/x64/Debug/OpenCVDetect.tlog/link.read.1.tlog


BIN
OpenCVDetect/x64/Debug/OpenCVDetect.tlog/link.write.1.tlog


BIN
OpenCVDetect/x64/Debug/opencvdetect.obj.enc


BIN
OpenCVDetect/x64/Debug/vc143.idb


BIN
OpenCVDetect/x64/Debug/vc143.pdb


BIN
x64/Debug/OpenCVDetect.exe


BIN
x64/Debug/OpenCVDetect.pdb