-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldp.m
116 lines (105 loc) · 3.39 KB
/
ldp.m
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function histFeature = ldp(img,subregionX,subregionY,bins)
load trees
img = im2double(img);
% img = rgb2gray(img);
% img = imresize(img,[88 88]);
[m,n]=size(img);
width = floor(m/subregionX);
height = floor(n/subregionY);
histFeature = zeros(bins,subregionX*subregionY);
k = 1;
for i = 0:subregionY-1
for j = 0:subregionX-1
queryImage = imcrop(img, map, [((j)*width) ((i)*height) width height]);
histFeature(:,k)= histCounts((LDPFeatureExtract(queryImage)),bins);
k = k + 1;
end
end
histFeature = histFeature';
histFeature = histFeature(:);
end
function I = derivative(img, refX, refY, alpha)
if alpha == 0
I = img(refY, refX) - img(refY, refX+1);
elseif alpha == 45
I = img(refY, refX) - img(refY-1, refX+1);
elseif alpha == 90
I = img(refY, refX) - img(refY-1, refX);
elseif alpha == 135
I = img(refY, refX) - img(refY-1, refX-1);
end
end
function F = comparison(I1, I2)
if I1*I2 > 0
F = 0;
else
F = 1;
end
end
function binary = LDPAlpha(img, refX, refY, alpha)
binary = zeros(8,1);
binary(1) = comparison(derivative(img,refX,refY,alpha),derivative(img,refX-1,refY-1,alpha));
binary(2) = comparison(derivative(img,refX,refY,alpha),derivative(img,refX,refY-1,alpha));
binary(3) = comparison(derivative(img,refX,refY,alpha),derivative(img,refX+1,refY-1,alpha));
binary(4) = comparison(derivative(img,refX,refY,alpha),derivative(img,refX+1,refY,alpha));
binary(5) = comparison(derivative(img,refX,refY,alpha),derivative(img,refX+1,refY+1,alpha));
binary(6) = comparison(derivative(img,refX,refY,alpha),derivative(img,refX,refY+1,alpha));
binary(7) = comparison(derivative(img,refX,refY,alpha),derivative(img,refX-1,refY+1,alpha));
binary(8) = comparison(derivative(img,refX,refY,alpha),derivative(img,refX-1,refY,alpha));
end
function a = LDP(img, refX, refY, alpha)
ldp = zeros(8,1);
a = 0;
ldp(1:8) = LDPAlpha(img, refX, refY, alpha);
% ldp(9:16) = LDPAlpha(img, refX, refY, 45);
% ldp(17:24) = LDPAlpha(img, refX, refY, 90);
% ldp(25:32) = LDPAlpha(img, refX, refY, 135);
%for i=0:3
i=0;
for t=0:7
a = a + ((2^(t))*ldp((8-t)+(i*8))); %decimal to binary conversion
end
%end
end
function hldp = HLDP(img)
feature = zeros(256,1);
img = img(:);
for i = 1:size(img,1)
feature(img(i)+1,1) = feature(img(i)+1,1) + 1;
end
hldp = feature;
end
function ldpimg = LDPImg(img,alpha)
[m,n] = size(img);
ldpimg = zeros(m,n);
for i=3:m-2
for j=3:n-2
ldpimg(i,j) = LDP(img,j,i,alpha);
end
end
ldpimg = uint8(ldpimg);
end
function feature = LDPFeatureExtract(img)
feature = zeros(256*4,1);
ldpimg1 = LDPImg(img,0);
ldpimg2 = LDPImg(img,45);
ldpimg3 = LDPImg(img,90);
ldpimg4 = LDPImg(img,135);
feature1 = HLDP(ldpimg1);
feature2 = HLDP(ldpimg2);
feature3 = HLDP(ldpimg3);
feature4 = HLDP(ldpimg4);
feature(1:256) = feature1;
feature(257:512) = feature2;
feature(513:768) = feature3;
feature(769:1024) = feature4;
end
function hist = histCounts(histogram, bins)
hist = zeros(bins,1);
width = size(histogram,1)/bins;
for i = 0:bins-1
for j = (width*i)+1:(width*(i+1))
hist(i+1,1)=hist(i+1,1)+histogram(j,1);
end
end
end