-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlbp(MBP).cpp
80 lines (71 loc) · 2.78 KB
/
lbp(MBP).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "lbp.h"
//提取SMBP纹理特征 Sobel+MBP
//Sobel算子
//int Soble[8][9] = { {1,2,1,0,0,0,-1,-2,-1},{2,1,0,1,0,-1,0,-1,-2},{1,0,-1,2,0,-2,1,0,-1},{0,-1,-2,1,0,-1,2,1,0},
// {-1,-2,-1,0,0,0,1,2,1},{-2,-1,0,-1,0,1,0,1,2},{-1,0,1,-2,0,2,-1,0,1},{0,1,2,-1,0,1,-2,-1,0} };
void correlation(uchar* center,uchar* result) ;
//LBP
Mat LBP(Mat img)
{
Mat result;
result.create(img.rows, img.cols, img.type());
result.setTo(0);
/*for (int i = 1; i < img.rows - 1; i++) {
for (int j = 1; j < img.cols - 1; j++) {
uchar center = img.at<uchar>(i, j);
uchar code = 0;
code |= (img.at<uchar>(i - 1, j - 1) >= center) << 7;
code |= (img.at<uchar>(i - 1, j) >= center) << 6;
code |= (img.at<uchar>(i - 1, j + 1) >= center) << 5;
code |= (img.at<uchar>(i, j + 1) >= center) << 4;
code |= (img.at<uchar>(i + 1, j + 1) >= center) << 3;
code |= (img.at<uchar>(i + 1, j) >= center) << 2;
code |= (img.at<uchar>(i + 1, j - 1) >= center) << 1;
code |= (img.at<uchar>(i, j - 1) >= center) << 0;
result.at<uchar>(i - 1, j - 1) = code;
}
}*/
for (int i = 1; i < img.rows - 1; i++) {
for (int j = 1; j < img.cols - 1; j++) {
//uchar center = img.at<uchar>(i, j);
int mid = 0;
uchar sobel[8], temp[8];
sobel[0] = img.at<uchar>(i - 1, j - 1);
sobel[1] = img.at<uchar>(i - 1, j);
sobel[2] = img.at<uchar>(i - 1, j + 1);
sobel[3] = img.at<uchar>(i, j + 1);
sobel[4] = img.at<uchar>(i + 1, j + 1);
sobel[5] = img.at<uchar>(i + 1, j);
sobel[6] = img.at<uchar>(i + 1, j - 1);
sobel[7] = img.at<uchar>(i, j - 1);
for (int k = 0; k < 8; k++)
{
mid = mid + sobel[k];
}
mid = mid / 8;
correlation(sobel, temp);
uchar code = 0;
code |= (temp[0] >= mid) << 7;
code |= (temp[1] >= mid) << 6;
code |= (temp[2] >= mid) << 5;
code |= (temp[3] >= mid) << 4;
code |= (temp[4] >= mid) << 3;
code |= (temp[5] >= mid) << 2;
code |= (temp[6] >= mid) << 1;
code |= (temp[7] >= mid) << 0;
result.at<uchar>(i - 1, j - 1) = code;
}
}
return result;
}
void correlation(uchar* center, uchar* result)
{
result[0] = center[0] + 2 * center[1] + center[2] - center[4] - 2 * center[5] - center[6];
result[1] = center[7] + 2 * center[0] + center[1] - center[3] - 2 * center[4] - center[5];
result[2] = center[0] + 2 * center[7] + center[6] - center[2] - 2 * center[3] - center[4];
result[3] = center[7] + 2 * center[6] + center[5] - center[1] - 2 * center[2] - center[3];
result[4] = center[4] + 2 * center[5] + center[6] - center[0] - 2 * center[1] - center[2];
result[5] = center[3] + 2 * center[4] + center[5] - center[7] - 2 * center[0] - center[1];
result[6] = center[2] + 2 * center[3] + center[4] - center[0] - 2 * center[7] - center[6];
result[7] = center[1] + 2 * center[2] + center[3] - center[7] - 2 * center[6] - center[5];
}