Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

uniform LBP - mapping using lookup table

Hello, I have basic implemented Local Binary Pattern (LBP), without interpolation, using OpenCV and C++. Following is the code:

#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace cv;
using namespace std;

cv::Mat LBP(string src_image)   
{   
  cv::Mat temp_image = imread(src_image.c_str(), 1); 
  cv::Mat Image(temp_image.rows, temp_image.cols, CV_8UC1);
  cv::Mat lbp(temp_image.rows, temp_image.cols, CV_8UC1);

  if (temp_image.channels() == 3)   
    cvtColor(temp_image, Image, CV_BGR2GRAY);     

  imshow("src_image", Image); 

  int center = 0;   
  int center_lbp = 0;   

  for (int row = 1; row < Image.rows; row++)   
  {
    for (int col = 1; col < Image.cols; col++)   
    {   
      center = Image.at<int>(row, col);
      center_lbp = 0;   

      if ( center <= Image.at<int>(row-1, col-1) )   
    center_lbp += 1;   

      if ( center <= Image.at<int>(row-1, col)   )    
        center_lbp += 2;   

      if ( center <= Image.at<int>(row-1, col+1) )   
        center_lbp += 4;   

      if ( center <= Image.at<int>(row, col-1)   )   
        center_lbp += 8;   

      if ( center <= Image.at<int>(row, col+1)   )   
        center_lbp += 16;   

      if ( center <= Image.at<int>(row+1, col-1) )   
        center_lbp += 32;   

      if ( center <= Image.at<int>(row+1, col)   )   
        center_lbp += 64;

      if ( center <= Image.at<int>(row+1, col+1) )   
        center_lbp += 128;   

      cout << "center lbp value: " << center_lbp << endl;
      lbp.at<int>(row, col) = center_lbp;
    }
  }

    imshow("lbp_image", lbp);
    waitKey(0);   
    destroyAllWindows();

    return lbp;
} 

void histogram(cv::Mat image)
{
  int histSize = 256;
  float range[] = { 0, 256 } ;
  const float* histRange = { range }; 

  bool uniform = true; bool accumulate = false;

  Mat hist;

  calcHist( &image, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );

  int hist_w = 512; int hist_h = 400;
  int bin_w = cvRound( (double) hist_w/histSize );

  Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );

  /// Normalize the result to [ 0, histImage.rows ]
  normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );

  /// Draw for each channel
  for( int i = 1; i < histSize; i++ )
  {
      line( histImage, Point( bin_w*(i-1), hist_h - cvRound(hist.at<float>(i-1)) ) ,
                       Point( bin_w*(i), hist_h - cvRound(hist.at<float>(i)) ),
                       Scalar( 255, 0, 0), 2, 8, 0  );
  }

  imshow("LBP image Histogram", histImage );
  waitKey(0);
}

//--------------------------------------------------------   
int main()   
{
  cv::Mat LBPimage = LBP("a.ppm");
  histogram(LBPimage);

  return 0;   
}

I now stuck at implementing uniform LBP. Can't figure out how to proceed. How to map using lookup table....can anybody please help?

thanks!

uniform LBP - mapping using lookup table

Hello, I have basic implemented Local Binary Pattern (LBP), without interpolation, using OpenCV and C++. Following is the code:

#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace cv;
using namespace std;

cv::Mat LBP(string src_image)   
{   
  cv::Mat temp_image = imread(src_image.c_str(), 1); 
  cv::Mat Image(temp_image.rows, temp_image.cols, CV_8UC1);
  cv::Mat lbp(temp_image.rows, temp_image.cols, CV_8UC1);

  if (temp_image.channels() == 3)   
    cvtColor(temp_image, Image, CV_BGR2GRAY);     

  imshow("src_image", Image); 

  int center = 0;   
  int center_lbp = 0;   

  for (int row = 1; row < Image.rows; row++)   
  {
    for (int col = 1; col < Image.cols; col++)   
    {   
      center = Image.at<int>(row, col);
      center_lbp = 0;   

      if ( center <= Image.at<int>(row-1, col-1) )   
    center_lbp += 1;   

      if ( center <= Image.at<int>(row-1, col)   )    
        center_lbp += 2;   

      if ( center <= Image.at<int>(row-1, col+1) )   
        center_lbp += 4;   

      if ( center <= Image.at<int>(row, col-1)   )   
        center_lbp += 8;   

      if ( center <= Image.at<int>(row, col+1)   )   
        center_lbp += 16;   

      if ( center <= Image.at<int>(row+1, col-1) )   
        center_lbp += 32;   

      if ( center <= Image.at<int>(row+1, col)   )   
        center_lbp += 64;

      if ( center <= Image.at<int>(row+1, col+1) )   
        center_lbp += 128;   

      cout << "center lbp value: " << center_lbp << endl;
      lbp.at<int>(row, col) = center_lbp;
    }
  }

    imshow("lbp_image", lbp);
    waitKey(0);   
    destroyAllWindows();

    return lbp;
} 

void histogram(cv::Mat image)
{
  int histSize = 256;
  float range[] = { 0, 256 } ;
  const float* histRange = { range }; 

  bool uniform = true; bool accumulate = false;

  Mat hist;

  calcHist( &image, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );

  int hist_w = 512; int hist_h = 400;
  int bin_w = cvRound( (double) hist_w/histSize );

  Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );

  /// Normalize the result to [ 0, histImage.rows ]
  normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );

  /// Draw for each channel
   for( int i = 1; i < histSize; i++ )
  {
      line( histImage, Point( bin_w*(i-1), hist_h - cvRound(hist.at<float>(i-1)) ) ,
                       Point( bin_w*(i), hist_h - cvRound(hist.at<float>(i)) ),
                       Scalar( 255, 0, 0), 2, 8, 0  );
  }

  imshow("LBP image Histogram", histImage );
  waitKey(0);
}

//--------------------------------------------------------   
int main()   
{
  cv::Mat LBPimage = LBP("a.ppm");
  histogram(LBPimage);

  return 0;   
}

I now stuck at implementing uniform LBP. Can't figure out how to proceed. How to map using lookup table....can anybody please help?

thanks!