Local ternary pattern (LTP)

Dear all,
I’m new Open CV , i had read LBP and LTP but i see only source code LBPH on open CV, how to for LTP , Pls help me.
thanks so much.

i got something like this (please try to read the mentioned paper first !):

//
// Wolf, Hassner, Taigman : "Descriptor Based Methods in the Wild"
// 3.1 Three-Patch LBP Codes
//
struct FeatureTPLbp
{
    int operator () (const Mat &img, Mat &features) const
    {
        Mat_<uchar> I(img);
        Mat_<uchar> fI(I.size(), 0);
        const int R=2;
        for (int r=R; r<I.rows-R; r++)
        {
            for (int c=R; c<I.cols-R; c++)
            {
                uchar v = 0;
                v |= ((I(r,c) - I(r  ,c-2)) > (I(r,c) - I(r-2,c  ))) * 1;
                v |= ((I(r,c) - I(r-1,c-1)) > (I(r,c) - I(r-1,c+1))) * 2;
                v |= ((I(r,c) - I(r-2,c  )) > (I(r,c) - I(r  ,c+2))) * 4;
                v |= ((I(r,c) - I(r-1,c+1)) > (I(r,c) - I(r+1,c+1))) * 8;
                v |= ((I(r,c) - I(r  ,c+2)) > (I(r,c) - I(r+1,c  ))) * 16;
                v |= ((I(r,c) - I(r+1,c+1)) > (I(r,c) - I(r+1,c-1))) * 32;
                v |= ((I(r,c) - I(r+1,c  )) > (I(r,c) - I(r  ,c-2))) * 64;
                v |= ((I(r,c) - I(r+1,c-1)) > (I(r,c) - I(r-1,c-1))) * 128;
                fI(r,c) = v;
            }
        }
        features = fI;
        return 256;
    }
};

Thanks so much, Berak. I will try this .

Dear Mr. Berak
I have see in your project uniform-lbp , this is contain LTP code
"

 struct FeatureLTP
{
    unsigned short lut[8][3];
    int radius;
    float thresholdPos;
    float thresholdNeg;

    FeatureLTP()
        : radius(1)
        , thresholdPos( 0.1f)
        , thresholdNeg(-0.1f)

    {
        unsigned short cnt = 0;
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 3; j++)
                lut[i][j] = cnt++;
            cnt++;  //we skip the 4th number (only three patterns)
        }
    }

    int operator() (const Mat &I, Mat &fI) const
    {
        CV_Assert(I.isContinuous() && (I.channels() == 1));

        Mat_<float>  m(I);
        Mat_<ushort> n(m.size());
        n = 0;

        const float *p = (const float*)m.ptr();
        for (int r=radius; r<m.rows-radius; r++)
        {
            for (int c=radius; c<m.cols-radius; c++)
            {
                const float cval = p[r * m.cols + c];
                static const int off[8][2] = {-1,-1, -1,0, -1,1, 0,1, 1,1, 1,0, 1,-1, 0,-1};
                for (int li=0; li<8; li++)
                {   // walk neighbours:
                    int y = r + off[li][0] * radius;
                    int x = c + off[li][1] * radius;
                    float diff = p[y * m.cols + x] - cval;
                    n(r,c) += (diff > thresholdPos) ? lut[li][0] :
                              (diff < thresholdNeg) ? lut[li][1] : lut[li][2];
                }
            }
        }
        fI = n;
        return 256;
    }
};

"
and in project open-cv contrib have to use code LBPH :slight_smile:
"

void olbp_(InputArray _src, OutputArray _dst) {
    // get matrices
    Mat src = _src.getMat();
    // allocate memory for result
    _dst.create(src.rows-2, src.cols-2, CV_8UC1);
    Mat dst = _dst.getMat();
    // zero the result matrix
    dst.setTo(0);
    // calculate patterns
    for(int i=1;i<src.rows-1;i++) {
        for(int j=1;j<src.cols-1;j++) {
            _Tp center = src.at<_Tp>(i,j);
            unsigned char code = 0;
            code |= (src.at<_Tp>(i-1,j-1) >= center) << 7;
            code |= (src.at<_Tp>(i-1,j) >= center) << 6;
            code |= (src.at<_Tp>(i-1,j+1) >= center) << 5;
            code |= (src.at<_Tp>(i,j+1) >= center) << 4;
            code |= (src.at<_Tp>(i+1,j+1) >= center) << 3;
            code |= (src.at<_Tp>(i+1,j) >= center) << 2;
            code |= (src.at<_Tp>(i+1,j-1) >= center) << 1;
            code |= (src.at<_Tp>(i,j-1) >= center) << 0;
            dst.at<unsigned char>(i-1,j-1) = code;
        }
    }
}

" ,
if we use LTP for face recognition as LBPH , how to change, may you help me, i also new c ++ and open cv, thank you.

it’s unclear, what you need to change (we cannot see your code)
but all 3 algorithms there take a grascale image, and deliver an 8bit “feature” image of the same size.

however, all of this is from around 10 years ago,
the cnns have won that “how to derive nice features for face recognition” race,
nowadays, you probably should use facenet or arcface to generate them, not variants of lbp