使用了两种实现方式,一种是使用vector和Point数据结构实现,另一种是使用基础的数据结构指针实现(uchar、int)。
在i7-6700的机器上第一种方法耗时17s,第二种方法耗时1.3s(若需要做到实时效果,需达到20+fps,仍需优化)。
#include <iostream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <math.h>
#include <vector>
#include <time.h>
#include <Windows.h>
using namespace cv;
using namespace std;
class LBP {
private:
Mat img;
public:
Mat img2lbp(Mat image) {
img = image;
Size shape = img.size();
Mat output = Mat(shape.height - 1, shape.width - 1, CV_8UC1);
for (int i = 1; i < shape.height-1; i++) {
for (int j = 1; j < shape.width - 1; j++) {
output.at<uchar>(Point(j, i)) = point2lbp(Point(j, i));
}
}
return output;
}
vector<Point> getRoundPosition(Point p) {
vector<Point> points;
points.push_back(Point(p.x - 1, p.y - 1));
points.push_back(Point(p.x, p.y - 1));
points.push_back(Point(p.x + 1, p.y - 1));
points.push_back(Point(p.x - 1, p.y));
points.push_back(Point(p.x + 1, p.y));
points.push_back(Point(p.x - 1, p.y + 1));
points.push_back(Point(p.x, p.y + 1));
points.push_back(Point(p.x + 1, p.y - 1));
points.push_back(Point(p.x - 1, p.y + 1));
return points;
}
int s(cv::Point p_position, int gray_value) {
if (img.at<uchar>(p_position) > gray_value) {
return 1;
}
else {
return 0;
}
}
int point2lbp(Point p_position) {
int lbp_value = 0;
vector<Point> points = getRoundPosition(p_position);
for (int i = 0; i < 8; i++) {
lbp_value += (pow(2, i)*s(points.at(i), img.at<uchar>(p_position)));
}
return lbp_value;
}
/*
----使用指针重构LBP二值化----
*/
// 图像lbp转换
Mat img2lbp_p(Mat image) {
Size shape = image.size();
uchar* data = image.data;
// 为转换结果申请内存空间
int new_width = shape.height - 1;
int new_height = shape.height - 1;
uchar* data_new = (uchar*)malloc(sizeof(uchar)*new_width*new_height);
for (int i = 1; i < new_height; i++) {
for (int j = 1; j < new_width; j++) {
data_new[i*new_width + j] = point2lbp_p(j, i, shape.width, shape.height, data);
}
}
// 将转换结果转化为Mat类型
Mat output = Mat(new_width, new_height, CV_8UC1);
output.data = data_new;
return output;
}
// 计算某点的lbp值
int point2lbp_p(int x, int y,int w, int h,uchar* data) {
int lbp_value = 0;
int* points = getRoundPosition_p(x, y);
for (int i = 0; i < 8; i++) {
lbp_value += (pow(2, i)*s_p(data, points[i * 2], points[i * 2 + 1], w, data[y*w + x]));
}
free(points);
return lbp_value;
}
// 获取某点周围8个点的位置
int* getRoundPosition_p(int x, int y) {
int * points = (int*)malloc(sizeof(int) * 9 * 2);
points[0] = x - 1; points[1] = y - 1;
points[2] = x; points[3] = y - 1;
points[4] = x + 1; points[5] = y - 1;
points[6] = x - 1; points[7] = y;
points[8] = x + 1; points[9] = y;
points[10] = x - 1; points[11] = y + 1;
points[12] = x; points[13] = y + 1;
points[14] = x + 1; points[15] = y + 1;
return points;
}
// 判断图像中该点是否大于中间像素
int s_p(uchar* img, int x, int y, int w, int gray_value) {
if (img[y*w + x] > gray_value) {
return 1;
}
else {
return 0;
}
}
};
int main()
{
Mat image=imread("C:/Users/dell/Pictures/语义分割.png");
Mat gray;
cvtColor(image, gray, CV_RGB2GRAY);
LBP lbp = LBP();
imshow("lbp image", gray);
waitKey(10000);
destroyAllWindows();
time_t start = time(0);
DWORD t_start, t_end;
t_start = GetTickCount();
gray = lbp.img2lbp_p(gray);
t_end = GetTickCount();
std::cout << t_end - t_start << std::endl;
imshow("lbp image", gray);
waitKey(10000);
destroyAllWindows();
imwrite("lbp_output.png", gray);
}