第五步:求取非整数位置的灰度值:
到此就结束了。其他非整数点的灰度值计算类似。
下面给出具体的opencv2的程序,自己可以尝试一下,看看效果。
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
using namespace cv;
using namespace std;
void elbp1(Mat& src, Mat& dst, int radius, int neighbors)
{
for (int n = 0; n < neighbors; n++)
{
float x = static_cast(radius*cos(CV_PI * 2.0 * n /static_cast( neighbors)));
float y = static_cast(-radius*sin(CV_PI*2.0*n/static_cast(neighbors)));
int x1 = static_cast(floor(x));
int y1 = static_cast(floor(y));
int x2 = static_cast(ceil(x));
int y2 = static_cast(ceil(y));
float tx = x - x1;
float ty = y - y1;
float w1 = (1 - tx)*(1 - ty);
float w2 = tx*(1 - ty);
float w3 = (1 - tx)*ty;
float w4 = tx*ty;
for (int i = radius; i < src.rows - radius; i++)
{
for (int j = radius; j < src.cols - radius; j++)
{
uchar center = src.at(i, j);
float t = static_cast(w1*src.at(i + x1, j + y1) + w2*src.at(i + x1, j + y2)\
+ w3*src.at(i + x2, j + y1)+w4*src.at(i + x2, j + y2));
//float t = static_cast(w1*src.at(i + x1, j + y1) + w2*src.at(i +x1 , j + y2) + w3*src.at(i + x2, j + y1) + w4*src.at(i + x2, j + y2));
//dst.at(i - radius, j - radius) += ((t > src.at(i, j)) || (std::abs(t - src.at(i, j)) < std::numeric_limits::epsilon())) << n;
dst.at(i - radius, j - radius) |= (t > center) << (neighbors-n-1);
}
}
}
}
int main()
{
Mat img = cv::imread("E:\\学习资料\\VS2013程序\\LBP\\8.png",0);
namedWindow("Image");
imshow("image",img);
int radius = 1;
int neighbors = 8;
Mat dst1 = Mat(img.rows-2*radius,img.cols-2*radius,CV_8UC1,Scalar(0));
elbp1(img, dst1, 2, 16);
namedWindow("dst1");
imshow("dst1",dst1);
while (1)
cv::waitKey(0);
}