template<typename _Tp> struct RGB2Gray
{
    typedef _Tp channel_type;
    
    RGB2Gray(int _srccn, int blueIdx, const float* _coeffs) : srccn(_srccn)
    {
        static const float coeffs0[] = { 0.299f, 0.587f, 0.114f };  // 三分量系数不同,人眼对绿色最敏感,所以G分量系数较大
        memcpy( coeffs, _coeffs ? _coeffs : coeffs0, 3*sizeof(coeffs[0]) );
        if(blueIdx == 0)
            std::swap(coeffs[0], coeffs[2]);
    }
    
    void operator()(const _Tp* src, _Tp* dst, int n) const  // 运算
    {
        int scn = srccn;
        float cb = coeffs[0], cg = coeffs[1], cr = coeffs[2];
        for(int i = 0; i < n; i++, src += scn)
            dst[i] = saturate_cast<_Tp>(src[0]*cb + src[1]*cg + src[2]*cr);  // 结果
    }
    int srccn;
    float coeffs[3];
};

来源 https://blog.csdn.net/viewcode/article/details/8203728icon-default.png?t=M1L8https://blog.csdn.net/viewcode/article/details/8203728

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐