Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Preparing LBP Texture Feature for SVM

I have extracted LBP Texture features on an image by splitting the image into small cells and then calculating LBP for each sub-image and then latter concatenating spatial histograms of each sub-image following the Philip Code. I push these spatial histograms to a vector<Mat>. I understand my spatial texture features are now contained in the histograms.

The problem I have is, I want to prepare this texture feature vector for training SVM. I convert the vector<Mat>hists of histograms to a row Mat where each row represent a sample. I use the code below to convert to row Mat.

Mat cv::asRowMatrix(const vector<Mat>& src, int rtype, double alpha, double beta) {
// number of samples
size_t n = src.size();
// return empty matrix if no matrices given
if (n == 0)
    return Mat();
// dimensionality of (reshaped) samples
size_t d = src[0].total();
// create data matrix
Mat data(n, d, rtype);
// now copy data
for (int i = 0; i < n; i++) {
    // make sure data can be reshaped, throw exception if not!
    if (src[i].total() != d) {
        string error_message = format("Wrong number of elements in matrix #%d! Expected %d was %d.", i, d, src[i].total());
        CV_Error(CV_StsBadArg, error_message);
    }
    // get a hold of the current row
    Mat xi = data.row(i);
    // make reshape happy by cloning for non-continuous matrices
    if (src[i].isContinuous()) {
        src[i].reshape(1, 1).convertTo(xi, rtype, alpha, beta);
    }
    else {
        src[i].clone().reshape(1, 1).convertTo(xi, rtype, alpha, beta);
    }
}
return data;}

When I do this as Mat data = asRowMatrix(hists, CV_32F, 1, 0) the matrix data comes with same values in each column, showing that all the images are identical. I get the same results if I use LBP image itself instead of its histogram. I would like to prepare LBP texture for training SVM. I have spent weeks figuring out this but am now stuck. Kindly help. Regards

Preparing LBP Texture Feature for SVM

I have extracted LBP Texture features on an image by splitting the image into small cells and then calculating LBP for each sub-image and then latter concatenating spatial histograms of each sub-image following the Philip Code. I push these spatial histograms to a vector<Mat>. I understand my spatial texture features are now contained in the histograms.

The problem I have is, I want to prepare this texture feature vector for training SVM. I convert the vector<Mat>hists of histograms to a row Mat where each row represent a sample. I use the code below to convert to row Mat.

Mat cv::asRowMatrix(const vector<Mat>& src, int rtype, double alpha, double beta) {
// number of samples
size_t n = src.size();
// return empty matrix if no matrices given
if (n == 0)
    return Mat();
// dimensionality of (reshaped) samples
size_t d = src[0].total();
// create data matrix
Mat data(n, d, rtype);
// now copy data
for (int i = 0; i < n; i++) {
    // make sure data can be reshaped, throw exception if not!
    if (src[i].total() != d) {
        string error_message = format("Wrong number of elements in matrix #%d! Expected %d was %d.", i, d, src[i].total());
        CV_Error(CV_StsBadArg, error_message);
    }
    // get a hold of the current row
    Mat xi = data.row(i);
    // make reshape happy by cloning for non-continuous matrices
    if (src[i].isContinuous()) {
        src[i].reshape(1, 1).convertTo(xi, rtype, alpha, beta);
    }
    else {
        src[i].clone().reshape(1, 1).convertTo(xi, rtype, alpha, beta);
    }
}
return data;}

When I do this as Mat data = asRowMatrix(hists, CV_32F, 1, 0) the matrix data comes with same values in each column, showing that all the images are identical. I get the same results if I use LBP image itself instead of its histogram. I would like to prepare LBP texture for training SVM. I have spent weeks figuring out this but am now stuck. Kindly help. Regards

This is how am filling the vector with the code below

string path = ".\\Images\\*.JPG"; //path to my images
vector<String> names; //hold all URLs to images
cv::glob(path, names, false); //Reading URLs to names
//Loop through names, load image and calculate lbp and hists
vector<Mat> hists;
Mat img;
Mat lbp;
Mat hist;
for(int i = 0; i < names.size(); i++)
{
      img = imread(names[i]);
      cvtColor(img, img, CV_BGR2GRAY);
      lbp::OLBP(img, lbp); //call OLBP function in Phillip code
      lbp::spatial_histogram(lbp, hist, 256, Size(10, 10)); //Call to spatial histograms function in Phillip code
      hists.push_back(hist); //push back histogram of this image to vector of mat
}