Convert Gray image into LBP image

asked 2015-04-19 00:49:32 -0500

Vishal Chhatwani gravatar image

updated 2015-04-20 20:52:17 -0500

Hello everyone,

I want to detect the eye blink in android OpenCV. I have been suggested to use LBP and SVM Classification for better results. I have been looking at different places including this and this but having trouble in understanding the code. I want the code in Java and I am having problems in converting the C++ code into Java as there are some function which are different in both of them. Can someone kindly give me a sample code to convert gray image to LBP image so that I can apply SVM on it. Need it badly. Please help.. Here is a code in C++. I need this in Java.

Mat LBP(Mat img){
Mat dst = Mat::zeros(img.rows-2, img.cols-2, CV_8UC1);
for(int i=1;i<img.rows-1;i++) {
    for(int j=1;j<img.cols-1;j++) {
        uchar center =<uchar>(i,j);
        unsigned char code = 0;
        code |= ((<uchar>(i-1,j-1)) > center) << 7;
            code |= ((<uchar>(i-1,j)) > center) << 6;
        code |= ((<uchar>(i-1,j+1)) > center) << 5;
        code |= ((<uchar>(i,j+1)) > center) << 4;
        code |= ((<uchar>(i+1,j+1)) > center) << 3;
        code |= ((<uchar>(i+1,j)) > center) << 2;
        code |= ((<uchar>(i+1,j-1)) > center) << 1;
        code |= ((<uchar>(i,j-1)) > center) << 0;<uchar>(i-1,j-1) = code;
return dst;


Thanks in advance.

edit retag flag offensive close merge delete


  • "but could not understand it"

    you probably want to explain, what you did understand, and what not.

(any given code would be useless, without a minimal understanding of what you're doing)

berak gravatar imageberak ( 2015-04-19 05:34:27 -0500 )edit

@berak So far I have detected the eyes and face using haarcascade. Converted them to gray image. So now I have a gray image of that detected region(in this case eyes) on which i want to apply LBP. I know the concept of how LBP works but having trouble in writing the code in java. Can you please help me with that ?

Vishal Chhatwani gravatar imageVishal Chhatwani ( 2015-04-19 14:56:03 -0500 )edit

above would be part1 of the assignment. after that, you'd have to grid the lbp-image into parts, take a histogram of each, and concatenate those histograms to the final (1d feature vector) to be used by svm.

since this involves a lot of per-pixel operations, you'd rather want to keep it c++ (via jni), and only retunr the final feature vect to java

berak gravatar imageberak ( 2015-04-21 01:41:03 -0500 )edit

@berak how would I do that? writing C++ code in android. Can u refer a good link? And why cant I do that in Java? This means C++ is more efficient than Java.

Vishal Chhatwani gravatar imageVishal Chhatwani ( 2015-04-21 05:00:52 -0500 )edit