[opencv]GeneralProcessing_Template_Function

 

//
// Created by leoxae on 2019-05-08.
//

#ifndef OPENCVDEMO_UTILS_H
#define OPENCVDEMO_UTILS_H

#include <iostream>
#include <map>
#include <sstream>
#include <vector>
#include <algorithm>
#include <chrono>
#include "../globals.h"
#include "opencv2/opencv.hpp"

#define Max(a, b) (a > b ? a : b)
#define Min(a, b) (a < b ? a : b)


using namespace std ;
using namespace cv ;

class TempHelper {

public:

    static long getTickCount()
    {
        auto now = std::chrono::steady_clock::now();
        std::chrono::milliseconds ms(0);
        ms=std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());

        return ms.count();
    }

    /**
     * 批量删除vector中的元素
     * @tparam T 泛型
     * @param src 数据源
     * @param rms 待删除的数据
     * @return
     */
    template<typename T> static auto removeAll(std::vector<T> &src, std::vector<T> &rms);


    /**
     * 删除vector中单个元素
     * @tparam T
     * @param src
     * @param a
     * @return
     */
    template<typename T> static auto removeObj(std::vector<T> &src, T &a){
        typename std::vector<T>::iterator d = remove(src.begin(), src.end(), a);
        src.erase(d, src.end());
        return src;
    }

    /**
     * 直接把对象tostring
     * @tparam T
     * @param t
     * @return
     */
    template<typename T> static auto toString(const T& t)
    {
        std::ostringstream oss;      //创建一个格式化输出流
        oss<<t;                //把值传递到流中
        return oss.str();
    }

    /**
     * 批量添加元素
     * @tparam T
     * @param src
     * @param need
     * @return
     */
    template<typename T> static auto addAll(std::vector<T> &src, std::vector<T> &need){
        for (auto it = need.begin(); it != need.end(); ++it) {
            src.emplace_back(*it);
        }
        return src;
    }

    /**
     * 取特定个数元素
     * @tparam T
     * @param src
     * @param size
     * @return
     */
    template<typename T> static auto subVector(std::vector<T> &src, const int &size){
        std::vector<T> vt;
        int idx = 0;
        for(auto itx = src.begin(); itx != src.end(); ++itx){
            if (idx == size) {
                break;
            }
            vt.emplace_back(*itx);
            idx ++;
        }
        return vt;
    }

    template<typename T> static auto split_and_toString(std::vector<T> src, std::string split){
        std::string data ;
        for (auto itx = src.begin(); itx != src.end(); ++itx) {
            data += (*itx) + split;
        }
        return data.substr(0, data.length() - 1);
    }

    /****
     * 排序
     * @tparam A
     * @tparam B
     * @param p
     * @return
     */
    template<typename A, typename B> static std::pair<B,A> flip_pair(const std::pair<A,B> &p)
    {
        return std::pair<B,A>(p.second, p.first);
    }

    template<typename A, typename B> static std::multimap<B,A> flip_map(const std::map<A,B> &src)
    {
        std::multimap<B,A> dst;
        std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()),
                       flip_pair<A,B>);
        return dst;
    }

    /**
     * 数组长度
     * @tparam T
     * @param ary
     * @return
     */
    template<class T> static int getAryLen(T &ary) {
        return sizeof(ary) / sizeof(ary[0]);
    }

};

template<typename T>
auto TempHelper::removeAll(std::vector<T> &src, std::vector<T> &rms) {
    for (auto it = rms.begin(); it != rms.end(); ++it) {
        typename std::vector<T>::iterator del = remove(src.begin(), src.end(), (*it));
        src.erase(del, src.end());
    }
    return src;
}


////////////////////////
template<class T>
int length(T& arr)
{
    //cout << sizeof(arr[0]) << endl;
    //cout << sizeof(arr) << endl;
    return sizeof(arr) / sizeof(arr[0]);
}


    /**
     * 对数组取平均
     *
     * @param ary
     * @return
     */
        static double avg(double ary[]) {
        int size = length(ary);
        double total = 0;
        for (int i = 0; i < size; i++) {
            total += ary[i];
        }
        return total / size;
    }

//char getCardName(char content) {
//    char regex = "([\u4e00-\u9fa5]+)";
//    Pattern p = Pattern.compile(regex);
//    Matcher m = p.matcher(content);
//    while (m.find()) {
//        return m.group(0);
//    }
//    return "未找到卡片名称";
//}

// std::vector<cv::DMatch> DmatchtoArray() {
//    int num = int total();
//    DMatch a[] = new DMatch[num];
//    if (num == 0) {
//        return a;
//    } else {
//        float buff[] = new float[num * 4];
//        this.get(0, 0, buff);
//
//        for(int i = 0; i < num; ++i) {
//            a[i] = new DMatch((int)buff[4 * i + 0], (int)buff[4 * i + 1], (int)buff[4 * i + 2], buff[4 * i + 3]);
//        }
//
//        return a;
//    }
//}


#endif //OPENCVDEMO_UTILS_H

 

posted @ 2019-10-18 17:47  Xu_Lin  阅读(113)  评论(0编辑  收藏  举报