基于bow的支持向量机分类

  • q2_431020
    了解作者
  • 39.1KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-23 03:38
    上传日期
这是一个图像分类的程序,可用于多种图像分类
85375565SVM-image-classification--OpenCV--.rar
内容介绍
#include <iostream> #include <fstream> #include <string> #include <iomanip> #include <opencv2/opencv.hpp> #include <opencv2/nonfree/features2d.hpp> #include <opencv2/legacy/legacy.hpp> #ifdef _DEBUG #pragma comment(lib, "opencv_core245d") #pragma comment(lib, "opencv_highgui245d") #pragma comment(lib, "opencv_features2d245d") #pragma comment(lib, "opencv_ml245d") #pragma comment(lib, "opencv_nonfree245d") #pragma comment(lib, "opencv_legacy245d") #pragma comment(lib, "opencv_flann245d") #pragma comment(lib, "opencv_imgproc245d") #else #pragma comment(lib, "opencv_core245") #pragma comment(lib, "opencv_highgui245") #pragma comment(lib, "opencv_features2d245") #pragma comment(lib, "opencv_ml245") #pragma comment(lib, "opencv_nonfree245") #pragma comment(lib, "opencv_legacy245") #pragma comment(lib, "opencv_flann245") #pragma comment(lib, "opencv_imgproc245") #endif const std::string image_folder_path("C:\\Users\\Hongze Zhao\\Downloads\\MLKD-Final-Project-Release\\ic-data\\train\\"); bool ReadImageNamesAndLabels(std::vector<std::string>& image_file_names, std::vector<std::string>& image_labels, std::string folder, std::string list_file_name) { using namespace std; ifstream label_list_file(folder + list_file_name); if (!label_list_file) return false; string fname; string label; while (!label_list_file.eof()) { label_list_file >> fname >> label; if (fname.length() == 0 || label.length() == 0) continue; image_file_names.push_back(folder + fname + ".jpg"); image_labels.push_back(label); } label_list_file.close(); return true; } // resize to 256x256 void UnifyImageSize(cv::Mat& image) { using namespace cv; Mat unified_image; int s = cv::min(image.rows, image.cols); //float scale = 128.0 / s; //Size size(image.cols * scale, image.rows * scale); Size size(128, 128); cv::resize(image, unified_image, size); //cv::GaussianBlur(unified_image, unified_image, Size(3, 3), 3.0); //cv::filter2D(unified_image, unified_image, unified_image.depth(), CV_MEDIAN); cv::medianBlur(unified_image, unified_image, 3); image = unified_image; } void ExtractTrainingSamples(cv::Ptr<cv::FeatureDetector>& detector, cv::BOWImgDescriptorExtractor& bowide, std::map<std::string,cv::Mat>& classes_training_data) { using namespace cv; using namespace std; vector<string> image_file_names; vector<string> image_labels; //ReadImageNamesAndLabels(image_file_names, image_labels, image_folder_path, "all.train.label"); ReadImageNamesAndLabels(image_file_names, image_labels, "C:\\Users\\Hongze Zhao\\Downloads\\MLKD-Final-Project-Release\\ic-data\\extra\\", "extra.label"); ReadImageNamesAndLabels(image_file_names, image_labels, "C:\\Users\\Hongze Zhao\\Downloads\\MLKD-Final-Project-Release\\ic-data\\check\\", "check.label"); cout << "extracting training samples ... "; #pragma omp parallel for for (int i = 0; i < image_file_names.size(); i++) { vector<KeyPoint> keypoints; Mat response_hist; string& class_label = image_labels[i]; Mat image = imread(image_file_names[i]);//, CV_LOAD_IMAGE_GRAYSCALE); UnifyImageSize(image); detector->detect(image, keypoints); bowide.compute(image, keypoints, response_hist); #pragma omp critical { if (classes_training_data.count(class_label) == 0) // not yet created... classes_training_data[class_label].create(0, response_hist.cols, response_hist.type()); classes_training_data[class_label].push_back(response_hist); cout << "\b\b\b\b\b\b\b\b\b"; cout << setfill(' ') << setw(4) << i << "/" << setw(4) << image_file_names.size(); } } cout << endl; cout << "saving to file ..." << endl; FileStorage fs("training_samples.yml", FileStorage::WRITE); for (map<string, Mat>::iterator ite = classes_training_data.begin(); ite != classes_training_data.end(); ++ite) { cout << "save " << ite->first << endl; fs << "class" + ite->first << ite->second; } fs.release(); } void TrainSVM(std::map<std::string,cv::Mat>& classes_training_data, std::string& file_postfix, int response_cols, int response_type) { using namespace cv; using namespace std; vector<string> class_names; for (map<string, Mat>::iterator ite = classes_training_data.begin(); ite != classes_training_data.end(); ++ite) class_names.push_back(ite->first); // one vs. all classifiers #pragma omp parallel for schedule(dynamic) for (int i = 0; i < class_names.size(); i++) { string& class_name = class_names[i]; cout << "training class : " << class_name << " ..." << endl; // copy class samples and label Mat samples(0, response_cols, response_type); Mat labels(0, 1, CV_32FC1); // 0 rows, 1 cols samples.push_back(classes_training_data[class_name]); Mat class_label = Mat::ones(classes_training_data[class_name].rows, 1, CV_32FC1); labels.push_back(class_label); // copy rest samples and label for (map<string, Mat>::iterator ite = classes_training_data.begin(); ite != classes_training_data.end(); ++ite) { string not_class_name = ite->first; if (not_class_name == class_name) continue; Mat& not_class_mat = classes_training_data[not_class_name]; samples.push_back(not_class_mat); class_label = Mat::zeros(not_class_mat.rows, 1, CV_32FC1); labels.push_back(class_label); } // train and save if (samples.rows == 0) continue; Mat sample_32f; samples.convertTo(sample_32f, CV_32F); CvSVMParams svm_param; svm_param.svm_type = CvSVM::C_SVC; svm_param.kernel_type = CvSVM::RBF; //svm_param.nu = 0.5; // in the range 0..1, the larger the value, the smoother the decision boundary svm_param.C = 5; svm_param.gamma = 0.1; //svm_param.degree = 3; svm_param.term_crit.epsilon = 1e-8; svm_param.term_crit.max_iter = 1e9; svm_param.term_crit.type = CV_TERMCRIT_ITER | CV_TERMCRIT_EPS; CvSVM svm_classifier; svm_classifier.train(sample_32f, labels, Mat(), Mat(), svm_param); //svm_classifier.train(sample_32f, labels); //svm_classifier.train_auto(sample_32f, labels, Mat(), Mat(), svm_param); // save classifier string classifier_file_name("SVM_classifier_"); classifier_file_name += file_postfix + "_" + class_name + ".yml"; svm_classifier.save(classifier_file_name.c_str()); cout << classifier_file_name << " saved" << endl; } } int main(int argc, char* argv[]) { using namespace cv; using namespace std; if (argc < 3) { cout << "USAGE: train_bovw <vocabulary_file.yml> <postfix_for_output>"<<endl; return -1; } cout << " ------- Train SVM Classifier -------" << endl; // read vocabulary from file cout << "reading vocabulary form file ..."<<endl; Mat vocabulary; FileStorage fs(argv[1], FileStorage::READ); fs["vocabulary"] >> vocabulary; fs.release(); if (vocabulary.rows == 0) { cerr << "Cannot Load Vocabulary File :" << argv[1] << endl; return -1; } // setup BOWImgDescriptorExtractor with vocabulary Ptr<FeatureDetector> feature_detector(new SurfFeatureDetector(400)); //Ptr<FeatureDetector> feature_detector = Ptr<cv::FeatureDetector>(new PyramidAdaptedFeatureDetector(Ptr<cv::FeatureDetector>(new SurfFeatureDetector(400)))); //Ptr<FeatureDetector> feature_detector = FeatureDetector
评论
    相关推荐
    • 支持向量机
      利用支持向量机实现分类,包括对支持向量机的详细描述及应用。
    • 葡萄酒支持向量机SVM分类
      采用支持向量机SVM分类葡萄酒,完整代码,无错误,下载即可运行。
    • 支持向量机实现的分类源码
      支持向量机实现的分类源码,可用来实现数据分类
    • 支持向量机-libsvm
      libsvm是台湾大学做的开源代码,svm的方法很全,而且提供了参数的选择方法。对于做分类,做识别的同学应该很有用处
    • 支持向量机用于分类
      改代码为机器学习中的支持向量机算法,主要用于分类任务
    • 支持向量机分类识别代码
      支持向量机分类识别代码支持向量机分类识别代码支持向量机分类识别代码支持向量机分类识别代码支持向量机分类识别代码支持向量机分类识别代码支持向量机分类识别代码
    • matlab代码不反应-Classification-SVM:分类支持向量机
      matlab代码不React分类支持向量机 在四个数据集上使用最小二乘回归进行多类分类 选择数据集的过程 在选择用于应用机器学习的数据集时,重要的是对高质量,真实世界(非人为)的数据集进行练习。 幸运的是,UCI机器...
    • 支持向量机
      malab开发的svm程序,有注释,可画出分类
    • 支持向量机
      利用支持向量机对图像进行分类识别,有较好的识别率
    • GaussDB_100_1.0.1-DATABASE-REDHAT-64bit.tar.gz
      guassdb100在redhat上安装包,单机部署的包,安装步骤请看我的文中介绍,经过大量实验搭建总结出来的文档