美文网首页
opencv--常用库调用

opencv--常用库调用

作者: 有事没事扯扯淡 | 来源:发表于2020-07-20 10:55 被阅读0次

    时不时用到Opencv里面的各种库,每次都是现查,各种版本都有,实在受不了自己写个文档记录一下。毫无逻辑、毫无美感,纯为了查阅方便。(●ˇ∀ˇ●)

    Opencv + SVM

    cv::Mat train_data, train_label;
    
    train_data.convertTo(train_data, CV_32FC1);
    train_label.convertTo(train_label, CV_32FC1);
    
    Ptr<SVM> svm = SVM::create();
    svm->setType(SVM::C_SVC);
    svm->setKernel(SVM::RBF);
    
    svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 1000, 1e-5));
    svm->train(train_data, ROW_SAMPLE, train_label);
    svm->save("svm.xml");
    

    Opencv + ANN

    Mat layers_size = (Mat_<int>(1, 4) << 500,200,10,1);
    
    ////模型创建和参数设置
    Ptr<ml::ANN_MLP> ann = ml::ANN_MLP::create();//创建一个空模型
    ann->setLayerSizes(layers_size);
    ann->setTrainMethod(ml::ANN_MLP::BACKPROP, 0.1, 0.1);
    ann->setActivationFunction(ml::ANN_MLP::SIGMOID_SYM);
    ann->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 1000, 1e-4));
    
    ////设置训练数据并训练分类器
    Ptr<ml::TrainData> tData = ml::TrainData::create(train_data, ml::ROW_SAMPLE, train_label);
    
    ann->train(tData);
    ann->save("model_ann.xml");
    

    Opencv + json(第三方库)

    https://github.com/nlohmann/json/tree/develop/single_include/nlohmann/json.hpp
    (include/lib啥的自己配置属性表)

    #include "nlohmann/json.hpp" 
    #include <fstream>              // ifstream, ofstream
    
    string json_name;
    
    std::ifstream fin(json_name);   // 注意此处是相对路径
    json j;
    fin >> j;
    fin.close();
    
    std::cout << j["shapes"][0]["label"] << std::endl;  // 根据自己的json文件改
    string image_name = j["imagePath"];
    

    Opencv + PCA

    Mat input_data;
    //-> save pca file
    PCA pca(input_data, Mat(), PCA::DATA_AS_ROW,500);
    FileStorage fs("pca.xml", FileStorage::WRITE);
    pca.write(fs);
    fs.release();
    
    //->load pca file
    FileStorage fs("pca.xml", FileStorage::READ);
    cv::PCA pca;
    pca.read(fs.root());
    fs.release();
    
    //->project the input vector
    input_data = pca.project(input_data);
    

    C++ 指定字符分割字符串

    vector<string> _split(const string& s, const string& seperator) 
    {
    vector<string> result;
    typedef string::size_type string_size;
    string_size i = 0;
    
    while (i != s.size()) {
    //找到字符串中首个不等于分隔符的字母;
    int flag = 0;
    while (i != s.size() && flag == 0) {
          flag = 1;
          for (string_size x = 0; x < seperator.size(); ++x)
               if (s[i] == seperator[x]) {
                  ++i;
                  flag = 0;
                  break;
               }
         }
    
    //找到又一个分隔符,将两个分隔符之间的字符串取出;
    flag = 0;
    string_size j = i;
    while (j != s.size() && flag == 0) {
        for (string_size x = 0; x < seperator.size(); ++x)
                 if (s[j] == seperator[x]) {
                      flag = 1;
                      break;
                 }
                if (flag == 0)
                      ++j;
            }
           if (i != j) {
               result.push_back(s.substr(i, j - i));
               i = j;
           }
       }
       return result;
    }
    
    

    C++ 获取系统时间转为字符串

    string getCurTime()
    {
        time_t now = time(0);
        char dt[50];
        ctime_s(dt,50,&now);
        string ans;
        ans= dt;
        return ans;
    }
    

    相关文章

      网友评论

          本文标题:opencv--常用库调用

          本文链接:https://www.haomeiwen.com/subject/hpdekktx.html