1
votes
void LBP(Mat src, IplImage* dst)
{
    int tmp[8] = { 0 };
    CvScalar s;

    Mat temp = Mat(src.size(), IPL_DEPTH_8U, 1);
    uchar *data = (uchar*)src.data;
    int step = src.step;

    //cout << "step" << step << endl;

    for (int i = 1; i<src.size().height - 1; i++)
        for (int j = 1; j<src.size().width - 1; j++)
        {
            int sum = 0;
            if (data[(i - 1)*step + j - 1]>data[i*step + j])
                tmp[0] = 1;
            else
                tmp[0] = 0;
            if (data[i*step + (j - 1)]>data[i*step + j])
                tmp[1] = 1;
            else
                tmp[1] = 0;
            if (data[(i + 1)*step + (j - 1)]>data[i*step + j])
                tmp[2] = 1;
            else
                tmp[2] = 0;
            if (data[(i + 1)*step + j]>data[i*step + j])
                tmp[3] = 1;
            else
                tmp[3] = 0;
            if (data[(i + 1)*step + (j + 1)]>data[i*step + j])
                tmp[4] = 1;
            else
                tmp[4] = 0;
            if (data[i*step + (j + 1)]>data[i*step + j])
                tmp[5] = 1;
            else
                tmp[5] = 0;
            if (data[(i - 1)*step + (j + 1)]>data[i*step + j])
                tmp[6] = 1;
            else
                tmp[6] = 0;
            if (data[(i - 1)*step + j]>data[i*step + j])
                tmp[7] = 1;
            else
                tmp[7] = 0;

            s.val[0] = (tmp[0] * 1 + tmp[1] * 2 + tmp[2] * 4 + tmp[3] * 8 + tmp[4] * 16 + tmp[5] * 32 + tmp[6] * 64 + tmp[7] * 128);

            cvSet2D(dst, i, j, s);
        }

}

Above is my original code for local binary pattern, src is an input matrix and dst is the output. Now i want to change the IPLimage in void LBP(Mat src, IplImage* dst) to void LBP(Mat src, mat dst), i tried many ways but I always met problems like assertion failed or something else, i thinks it might be the problem of cvSet2D(dst, i, j, s);

this is the definition for the input src:

Mat Gray_face = Mat(image.size(), image.depth(), 1);

this is my definition for output dst:

IplImage* lbp_face = cvCreateImage(Gray_face.size(), IPL_DEPTH_8U, 1);

And i want to change it to mat and make it work for my program.

this is how i call the LBP function:

LBP(Gray_face, lbp_face);

I am quite new to this, can anyone help me? thank you very much!

1

1 Answers

1
votes

Indeed cvSet2D is the old interface. To set a point to a cv::Mat dst you can use at(). Fisrt, you first (re)allocate it as:

dst.create(src.size(),CV_8U);

Then to set a value point in your case:

dst.at<char>(i,j) = (tmp[0] * 1 + tmp[1] * 2 + tmp[2] * 4 + tmp[3] * 8 + tmp[4] * 16 + tmp[5] * 32 + tmp[6] * 64 + tmp[7] * 128);

Last but not least, if you want the result returned, you function definition should be:

 void LBP(Mat src, Mat & dst);

with a reference (&) to the destination.