Ask Your Question
0

Grayscale to Lbp image

asked 2013-09-10 06:17:10 -0600

ussdrk gravatar image

updated 2013-09-10 09:27:30 -0600

Actual output Desired output Hi friends i am trying to implement paper on Rapid texture Based Moving Object Detection So in this the first step is to convert grayscale image to lbp image so i have written the following code but it is not producing the desired output So Please if anybody having proper code provide it or state correction in my code.

int main() {

Mat img = cv::imread("im1.jpg");

if (img.empty())
{
   std::cout << "Cannot open image!" << std::endl;
    return -1;
}
Mat greyMat;

cvtColor(img, greyMat, CV_BGR2GRAY);
Mat lbp=greyMat.clone();
long row=greyMat.rows;
long col=greyMat.cols;
for(int i=1;i<greyMat.rows;i++)
{
 for(int j=1;j<greyMat.cols;j++)
 {
    int cp=(int)greyMat.at<uchar>(i,j);
    int pp=0;
    int po=7;
    if(cp<=(int)greyMat.at<uchar>(i,j+1))
    pp+=pow(2,po);
    po--;
    if(cp<=(int)greyMat.at<uchar>(i+1,j+1))
    pp+=pow(2,po);
    po--;
    if(cp<=(int)greyMat.at<uchar>(i+1,j))
    pp+=pow(2,po);
    po--;
    if(cp<=(int)greyMat.at<uchar>(i+1,j-1))
    pp+=pow(2,po);
    po--;
    if(cp<=(int)greyMat.at<uchar>(i,j-1))
    pp+=pow(2,po);
    po--;


    if(cp<=(int)greyMat.at<uchar>(i-1,j-1))
    pp+=pow(2,po);
    po--;
    if(cp<=(int)greyMat.at<uchar>(i-1,j))
    pp+=pow(2,po);
    po--;

    if(cp<=(int)greyMat.at<uchar>(i-1,j+1))
    pp+=1*pow(2,po);
    po--;


    lbp.at<uchar>(i,j)=pp;

 }
}

return 0;

}

edit retag flag offensive close merge delete

Comments

Please avoid hashtags in your tags, since this creates doubles which is inefficient for the forum search. Also, dont open the same topic 3 times.

Moster gravatar imageMoster ( 2013-09-10 06:24:48 -0600 )edit
  • for(int i=1;i<greyMat.rows - 1 ;i++) // same for the cols
  • ditch that pow(2,n) and use hardcoded numbers(0,1,2,4,8,16,..). it's too expensive, and makes code hard to read.

can you a bit more explicit about what is wrong with the output ?


here's the code from the facerecognizer


are you trying something similar to this ?

berak gravatar imageberak ( 2013-09-10 07:16:32 -0600 )edit

Sorry this is my first time so got confused with tags.

ussdrk gravatar imageussdrk ( 2013-09-10 09:03:15 -0600 )edit

@berak I have sample input and output.But after executing i am getting different output. will u please give ur Email Id so that i can mail u images and code.

ussdrk gravatar imageussdrk ( 2013-09-10 09:11:31 -0600 )edit

you can even attach images here ;) just re-edit your question, there's a button-bar there, the image one is the middle one

berak gravatar imageberak ( 2013-09-10 09:20:19 -0600 )edit

sure, we can start a private ([email protected]) discussion.

but you'll want as much help as possible from people on this site, too, don't you ?

berak gravatar imageberak ( 2013-09-10 09:28:36 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2013-09-11 09:16:41 -0600

Try that:

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 = img.at<uchar>(i,j);
            unsigned char code = 0;
            code |= ((img.at<uchar>(i-1,j-1)) > center) << 7;
            code |= ((img.at<uchar>(i-1,j)) > center) << 6;
            code |= ((img.at<uchar>(i-1,j+1)) > center) << 5;
            code |= ((img.at<uchar>(i,j+1)) > center) << 4;
            code |= ((img.at<uchar>(i+1,j+1)) > center) << 3;
            code |= ((img.at<uchar>(i+1,j)) > center) << 2;
            code |= ((img.at<uchar>(i+1,j-1)) > center) << 1;
            code |= ((img.at<uchar>(i,j-1)) > center) << 0;
            dst.at<uchar>(i-1,j-1) = code;
         }
    }
return dst;
}
edit flag offensive delete link more

Comments

definitely nicer. (though reducing the dst size(skipping the border) might not always be a good idea)

berak gravatar imageberak ( 2013-09-11 09:29:01 -0600 )edit
1

answered 2013-09-10 09:37:38 -0600

berak gravatar image

updated 2013-09-10 09:46:21 -0600

since the actual output is just the inverted desired one, you could achieve that by just changing the sign in your comparison, instead of :

if(cp <= (int)greyMat.at<uchar>(i,j+1))

use :

if(cp > (int)greyMat.at<uchar>(i,j+1))

the one thing will give you a 11011010 neighbourhood, the other a 00100101 one. it's just an inversion.

but honestly, i don't think it matters (imho neither if you start counting at 'north' or 'east', nor if you walk clockwise or not).

as long as you stay consistant whithin your application, everything will be fine.

edit flag offensive delete link more

Comments

I tried this too But when i am doing feature point extraction on Actual Output image Results are different than when i do on The lbp image which is given as sample I mailed u all images and code Please try to solve.

ussdrk gravatar imageussdrk ( 2013-09-10 09:59:51 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2013-09-10 06:17:10 -0600

Seen: 977 times

Last updated: Sep 11 '13