1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| #include "HOG-SVM.hpp" #include "Macro.hpp"
using namespace cv; using namespace ml;
HOG_SVM::HOG_SVM() { svm = SVM::create(); }
HOG_SVM::HOG_SVM(std::string model_path) { svm = Algorithm::load<SVM>(model_path); }
bool HOG_SVM::LoadModel(std::string model_path) { bool flag = true; try{ svm = Algorithm::load<SVM>(model_path); } catch (std::exception e){ MESSAGE_COUT("ERROR", e.what()) flag = false; } return flag; }
Mat HOG_SVM::ExtractFeature(Mat Img, Size mrs) { * @brief HOG_SVM::ExtractFeature The story behind 1764 For example window size is 64x64, block size is 16x16 and block setp is 8x8£¬cell size is 8x8, the block number window contained is (£¨64-16£©/8+1)*((64-16)/8+1) = 7*7 = 49, the cell number each block contained is (16/8)*(16/8) = 4 every cell can project 9 bin, and each bin related to 9 vector so feature_dim = B x C x N, and caulated result is 1764 (B is each window's blocks number, C is every block's cell number, n is bin number) */ resize(Img, Img, mrs); HOGDescriptor *hog = new HOGDescriptor(cvSize(64, 64), cvSize(16, 16), cvSize(8, 8), cvSize(8, 8), 9); std::vector<float> descriptors; hog->compute(Img, descriptors, Size(1, 1), Size(0, 0)); return Mat(descriptors).t(); }
Mat HOG_SVM::GetDataSet(std::vector<std::string> data_path) { int nImgNum = data_path.size(); int success = 0; Mat data_mat, src; for (int i = 0; i < nImgNum; i++){ src = imread(data_path[i]); if (src.cols && src.rows){ MESSAGE_COUT("PROCESS", FileOperation::findFileName(data_path[i]) << "\t" << success++) Mat post = ExtractFeature(src, Size(64, 64)); data_mat.push_back(post); } } return data_mat; }
Mat HOG_SVM::GetDataSet(std::vector<std::string> data_path, std::vector<GroundTruth>& gt, int c) { int nImgNum = data_path.size(); int success = 0; Mat data_mat; Mat src; std::string imgname; for (int i = 0; i < nImgNum; i++){ src = imread(data_path[i]); if (src.cols && src.rows){ imgname = FileOperation::findFileName(data_path[i]); MESSAGE_COUT("PROCESS", imgname << "\t" << success++) Mat post = ExtractFeature(src, Size(64, 64)); data_mat.push_back(post); gt.push_back(GroundTruth(c, imgname)); } } return data_mat; }
int HOG_SVM::SetSvmParameter(int sv_num, int c_r_type, int kernel, double gamma) { TermCriteria criteria = TermCriteria(CV_TERMCRIT_EPS, sv_num, FLT_EPSILON); svm->setType(c_r_type); svm->setKernel(kernel); if (kernel == SVM::RBF) svm->setGamma(gamma); svm->setTermCriteria(criteria); return 1; }
int HOG_SVM::Training(Mat& trainSet, Mat& label, bool save,std::string dir) { SetSvmParameter(200, SVM::C_SVC, SVM::LINEAR, 0); Ptr<TrainData> traindata = ml::TrainData::create(trainSet, ROW_SAMPLE, label); svm->train(traindata); if (save){ svm->save(dir+"HOG-SVM-MODEL.xml"); } return 1; }
int HOG_SVM::Testing(Mat& testSet, float gt) { int error = 0; int postnum = testSet.rows; Mat res = Mat::zeros(postnum, 1, CV_32FC1); svm->predict(testSet, res); for (int i = 0; i < postnum; i++) if (res.at<float>(i, 0) != gt) error++; std::cout << error << "/" << postnum << std::endl; return error; }
int HOG_SVM::Testing(Mat& testSet, std::vector<GroundTruth> gt) { int error = 0; int postnum = testSet.rows; Mat res = Mat::zeros(postnum, 1, CV_32FC1); svm->predict(testSet, res); for (int i = 0; i < postnum; i++) if (res.at<float>(i, 0) != gt[i].label){ MESSAGE_COUT("ERROR", gt[i].imgname << "\t" << gt[i].label) error++; } MESSAGE_COUT("RESULT", error << "/" << postnum) return error; }
float HOG_SVM::Predict(Mat& image) { if (!image.rows) return -1; Mat gray; cvtColor(image, gray, CV_BGR2GRAY); Mat post = ExtractFeature(gray, Size(64, 64)); gray.release(); return svm->predict(post); }
float HOG_SVM::EndToEnd(std::string data_path) { std::string trainPath = data_path + "train\\"; std::vector<std::string> trainPathPositive = this->getFilepath(trainPath + "1\\"); std::vector<std::string> trainPathNegative = this->getFilepath(trainPath + "-1\\"); std::string testPath = data_path + "test\\"; std::vector<std::string> testPathPositive = FileOperation::getCurrentDir(testPath + "1\\"); std::vector<std::string> testPathNegative = FileOperation::getCurrentDir(testPath + "-1\\"); Mat trainSet, label; Mat trainSetP = GetDataSet(trainPathPositive); Mat labelP = Mat::ones(trainSetP.rows, 1, CV_32SC1); Mat trainSetN = GetDataSet(trainPathNegative); Mat labelN = Mat::ones(trainSetN.rows, 1, CV_32SC1)*(-1); trainSet.push_back(trainSetP); trainSet.push_back(trainSetN); label.push_back(labelP); label.push_back(labelN); Training(trainSet, label, true, data_path); std::vector<GroundTruth> gtP, gtN; Mat testSetP = GetDataSet(testPathPositive, gtP, 1); Mat testSetN = GetDataSet(testPathNegative, gtN, -1); int error = Testing(testSetP, gtP) + Testing(testSetN, gtN); return 1.0f*error / (testSetP.rows + testSetN.rows); }
|