Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to train Local Binary Patterns to SVM

 bool affiche = true;
            //Mat pic1 is the source image.
    cv::Mat Image(pic1.rows, pic1.cols, CV_8UC1); 
    cv::Mat lbp(pic1.rows, pic1.cols, CV_8UC1);

    //converts the pic to grayscale
    if (pic1.channels() == 3)
        cvtColor(pic1, Image, CV_BGR2GRAY);

    unsigned center = 0;
    unsigned center_lbp = 0;

    Mat H(1, 256, CV_8UC1, Scalar::all(0));//creating the histogram

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

            if (center <= Image.at<uchar>(row - 1, col - 1))
                center_lbp += 1;
            H.at<uchar>(center_lbp) += 1;
            if (center <= Image.at<uchar>(row - 1, col))
                center_lbp += 2;
            H.at<uchar>(center_lbp) += 1;
            if (center <= Image.at<uchar>(row - 1, col + 1))
                center_lbp += 4;
            H.at<uchar>(center_lbp) += 1;
            if (center <= Image.at<uchar>(row, col - 1))
                center_lbp += 8;
            H.at<uchar>(center_lbp) += 1;
            if (center <= Image.at<uchar>(row, col + 1))
                center_lbp += 16;
            H.at<uchar>(center_lbp) += 1;
            if (center <= Image.at<uchar>(row + 1, col - 1))
                center_lbp += 32;
            H.at<uchar>(center_lbp) += 1;
            if (center <= Image.at<uchar>(row + 1, col))
                center_lbp += 64;
            H.at<uchar>(center_lbp) += 1;
            if (center <= Image.at<uchar>(row + 1, col + 1))
                center_lbp += 128;
            H.at<uchar>(center_lbp) += 1;

            lbp.at<uchar>(row, col) = center_lbp;
        }
    }
    //normalize the histogram
    normalize(H, H, 0, 1, NORM_MINMAX, -1, Mat());
    imwrite("LBP/LBPImage.jpg", lbp);

I am a beginner in openCV. The code above is inside a function that should return the finished training data. My problem is that I don't know how to get the numbers inside H and how to create the training data for the SVM. Can somebody help me with this?

How to train Local Binary Patterns to SVM

 public: cv::Mat LBP(Mat pic1){
        bool affiche = true;
            //Mat pic1 is the source image.
    cv::Mat Mat Image(pic1.rows, pic1.cols, CV_8UC1); 
    cv::Mat CV_8UC1), lbp(pic1.rows, pic1.cols, CV_8UC1);

    //converts the pic to grayscale
    if (pic1.channels() == 3)
        cvtColor(pic1, Image, CV_BGR2GRAY);
      unsigned center = 0;
    unsigned 0, center_lbp = 0;
      Mat H(1, 256, CV_8UC1, Scalar::all(0));//creating the histogram

CV_32FC1, Scalar::all(0));
        for (int row = 1; row < Image.rows - 1; row++)
     {
         for (int col = 1; col < Image.cols - 1; col++)
         {
             center = Image.at<uchar>(row, Image.at<float>(row, col);
             center_lbp = 0;
              if (center <= Image.at<uchar>(row Image.at<float>(row - 1, col - 1))
                 center_lbp += 1;
            H.at<uchar>(center_lbp) += 1;
            if (center <= Image.at<uchar>(row Image.at<float>(row - 1, col))
                 center_lbp += 2;
            H.at<uchar>(center_lbp) += 1;
            if (center <= Image.at<uchar>(row Image.at<float>(row - 1, col + 1))
                 center_lbp += 4;
            H.at<uchar>(center_lbp) += 1;
            if (center <= Image.at<uchar>(row, Image.at<float>(row, col - 1))
                 center_lbp += 8;
            H.at<uchar>(center_lbp) += 1;
            if (center <= Image.at<uchar>(row, Image.at<float>(row, col + 1))
                 center_lbp += 16;
            H.at<uchar>(center_lbp) += 1;
            if (center <= Image.at<uchar>(row Image.at<float>(row + 1, col - 1))
                 center_lbp += 32;
            H.at<uchar>(center_lbp) += 1;
            if (center <= Image.at<uchar>(row Image.at<float>(row + 1, col))
                 center_lbp += 64;
            H.at<uchar>(center_lbp) += 1;
            if (center <= Image.at<uchar>(row Image.at<float>(row + 1, col + 1))
                 center_lbp += 128;
            H.at<uchar>(center_lbp) += 1;

            lbp.at<uchar>(row,     lbp.at<float>(row, col) = center_lbp;
                H.at<float>(center_lbp) += 1;
            }
     }
    //normalize the histogram
    normalize(H, H, 0, 1, NORM_MINMAX, -1, Mat());
    imwrite("LBP/LBPImage.jpg", lbp);
    textureData.push_back(H);
        return H;
    }

I am a beginner in openCV. The code above is inside a function that should return the finished training data. My problem is that I don't know how to get the numbers inside H and how to create the training data for the SVM. Can somebody help me with this?