Ask Your Question
0

does any one has a uniform lbp c++ code with it's spatial histogram

asked 2014-05-23 23:07:41 -0600

Hm gravatar image

updated 2014-05-24 01:01:12 -0600

berak gravatar image

i have a code but with error

void LBPFeatures::computeuniformlbp(Mat image,Mat &dst)
{
    uchar *ptr=image.data;
    image.copyTo(dst);
    uchar *optr=dst.data;
    int width=image.cols;
    int height=image.rows;

for(int i=1;i<height-1;i++)
{
    for(int j=1;j<width-1;j++)
    {
        int center=(int)ptr[j+i*width];
        unsigned char code=0;

        //for(int k=7;k>=0;k++)

        code|=((int)ptr[(j-1)+(i-1)*width] >=center)<<7;
        code|=((int)ptr[j+(i-1)*width] >=center)<<6 ;
        code|=((int)ptr[(j+1)+(i-1)*width] >=center)<<5 ;
        code|=((int)ptr[(j+1)+(i)*width] >=center)<<4 ;
        code|=((int)ptr[(j+1)+(i+1)*width] >=center)<<3 ;
        code|=((int)ptr[j+(i+1)*width] >=center)<<2 ;
        code|=((int)ptr[j-1+(i+1)*width] >=center)<<1 ;
        code|=((int)ptr[j-1+(i)*width] >=center)<<0 ;

       // int check=0;
        //check=code;
        //heck if the code is uniform code
        //encode only if it is a uniform code else
        //assign it a number 255
        optr[j+i*width]=lookup[code];

    }
}
initUniform();
}

can you find my error

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2014-05-24 01:56:36 -0600

berak gravatar image

updated 2014-05-24 02:16:08 -0600

the idea behind the 'uniform' lbph is to compress the histogram from 256 items to 59 (faster/less memory) and at the same time achieving a bit more robustness against noise.

//
// Mat_<uchar> used here for convenient () operator indexing
//
uchar lbp(const Mat_<uchar> & img, int x, int y)
{
    // this is pretty much the same what you already got..
    uchar v = 0;
    uchar c = img(y,x);
    v += (img(y-1,x  ) > c) << 0;
    v += (img(y-1,x+1) > c) << 1;
    v += (img(y  ,x+1) > c) << 2;
    v += (img(y+1,x+1) > c) << 3;
    v += (img(y+1,x  ) > c) << 4;
    v += (img(y+1,x-1) > c) << 5;
    v += (img(y  ,x-1) > c) << 6;
    v += (img(y-1,x-1) > c) << 7;
    return v;
}

your code above tries to build an lbp image first, i'm going to skip that, and calculate the histogram directly from the (uniform) lbp values.

//
// you would usually apply this to small patches of an image and concatenate the histograms
//
void uniformLbpHist(const Mat & img_gray, Mat & hist)
{
    static uchar uniform[256] = { // hardcoded 8-neighbour case
        0,1,2,3,4,58,5,6,7,58,58,58,8,58,9,10,11,58,58,58,58,58,58,58,12,58,58,58,13,58,
        14,15,16,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,17,58,58,58,58,58,58,58,18,
        58,58,58,19,58,20,21,22,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,
        58,58,58,58,58,58,58,58,58,58,58,58,23,58,58,58,58,58,58,58,58,58,58,58,58,58,
        58,58,24,58,58,58,58,58,58,58,25,58,58,58,26,58,27,28,29,30,58,31,58,58,58,32,58,
        58,58,58,58,58,58,33,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,34,58,58,58,58,
        58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,
        58,35,36,37,58,38,58,58,58,39,58,58,58,58,58,58,58,40,58,58,58,58,58,58,58,58,58,
        58,58,58,58,58,58,41,42,43,58,44,58,58,58,45,58,58,58,58,58,58,58,46,47,48,58,49,
        58,58,58,50,51,52,58,53,54,55,56,57
    };
    // 59 bins, bin 58 is the noise/non-uniform slot:
    hist = Mat::zeros(59,1,CV_32F);

    Mat_<uchar>img(img_gray);

    for(int i=1;i<height-1;i++)
    {
        for(int j=1;j<width-1;j++)
        {
            uchar uv = lbp(img, j,i);
            hist[ uniform[uv] ] ++; // incr. the resp. histogram bin
        }
    }
}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-05-23 23:07:41 -0600

Seen: 2,295 times

Last updated: May 24 '14