-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathehd20.m
135 lines (112 loc) · 4.23 KB
/
ehd20.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
function H = ehd20(Img, Threshold)
% Img = imread (Image);
[oldx oldy z] = size(Img);
x = oldx/4;
y = oldy/4;
if mod(x, 2) ~= 0
x = x + 1;
end
if mod(y, 2) ~= 0
y = y + 1;
end
Img2 = uint8(zeros([x*4 y*4 z]));
Img2(1:oldx, 1:oldy, 1:z) = Img(1:oldx, 1:oldy, 1:z);
Img = Img2;
%figure; imshow (Img);
% Create the image Intensity
Image = rgb2gray(im2double(Img));
% Initialize the edge detection masks
H = [];
HorizontalMask = [1 1 1;0 0 0;-1 -1 -1];
VerticalMask = [1 0 -1;1 0 -1;1 0 -1];
DiagonalMask = [0 1 1;-1 0 1;-1 -1 0];
AntiDiagMask = [1 1 0;1 0 -1;0 -1 -1];
%NonDirectionalMask = [2, -2;-2, 2];
% Perform the filtering using the masks to create the edge images
HImage = imfilter(Image, HorizontalMask,'replicate');
VImage = imfilter(Image, VerticalMask,'replicate');
DImage = imfilter(Image, DiagonalMask,'replicate');
AImage = imfilter(Image, AntiDiagMask,'replicate');
%NImage = imfilter(Image, NonDirectionalMask,'replicate');
% Determine the size of each subimage dynamically
m = floor(size(Image, 1)/2); % Number of rows per subimage.
n = floor(size(Image, 2)/2); % Number of columns per subimage.
column = 1;row = 1;counter = 1; % Variable initialization
for i = 1:4 % Loop through every subimage
if counter > 2
column = 1;
row = row + m;
counter = 1;
end
subImgH(i).img = subim(HImage, m, n, row, column); % Get subimage for H
subImgV(i).img = subim(VImage, m, n, row, column); % Get subimage for V
subImgD(i).img = subim(DImage, m, n, row, column); % Get subimage for D
subImgA(i).img = subim(AImage, m, n, row, column); % Get subimage for A
% subImgN(i).img = subim(NImage, m, n, row, column); % Get subimage for A
column = column + n;
counter = counter + 1;
end
% To verify subimages can use the following code:
% figure;
% for i=1:16
% hold on
% subplot(4,4,i)
% imshow(subImgA(i).img,[])
% end
% Compute the blocks and histograms
try
for i = 1:4 % All subimages
HLocal(i,1:5) = 0;
row = 1; column = 1;
for j = 1:ceil(size(subImgH(i).img,1)*size(subImgH(i).img,2)/4) % All 2x2 blocks
if column > size(subImgH(i).img,2)
column = 1;
row = row + 2;
end
% Determine the max edge of the averages in a 2x2 area
[M, I] = max([sum(sum(abs(subImgH(i).img(row:row+1,column:column+1))))/4 ...
sum(sum(abs(subImgV(i).img(row:row+1,column:column+1))))/4 ...
sum(sum(abs(subImgD(i).img(row:row+1,column:column+1))))/4 ...
sum(sum(abs(subImgA(i).img(row:row+1,column:column+1))))/4 ...
Threshold]);
%sum(sum(abs(subImgN(i).img(row:row+1,column:column+1))))/4 ...
IndexedSub(i).img((row + 1)/2,(column + 1)/2) = I; %
% if I ~= 5
HLocal(i,I) = HLocal(i,I) + 1;
% IndexedSub(i).img((row + 1)/2,(column + 1)/2) = I; % Used for displaying edges
% end
column = column + 2;
end
HLocal(i,:) = HLocal(i,:)./(ceil(size(subImgH(i).img,1)*size(subImgH(i).img,2)/4));
end
catch
error('Issue with subimages having odd rows and columns, block error.')
end
%Uncomment to save or display the edge images used in the report.
% IS = [IndexedSub(1).img IndexedSub(2).img IndexedSub(3).img IndexedSub(4).img; ...
% IndexedSub(5).img IndexedSub(6).img IndexedSub(7).img IndexedSub(8).img; ...
% IndexedSub(9).img IndexedSub(10).img IndexedSub(11).img IndexedSub(12).img; ...
% IndexedSub(13).img IndexedSub(14).img IndexedSub(15).img IndexedSub(16).img];
% mymap = [1 0 0; 0 1 0; 0 0 1; 1 1 0; 1 1 1];
% figure; imshow(IS, mymap);
%imwrite(ind2rgb(IS,mymap),strcat('Threshold',num2str(Threshold),'\', name));
% Create the main edge histogram to return.
for i=1:4
H = cat(2,H,HLocal(i,:));
end
function s = subim(f, m, n, rx, cy)
%SUBIM Extracts a subimage, s, from a given image, f.
% The subimage is of size m-by-n, and the coordinates of its top, left
% corner are (rx, cy).
s = zeros(m, n);
rowhigh = rx + m - 1;
colhigh = cy + n - 1;
xcount = 0;
for r = rx:rowhigh
xcount = xcount + 1;
ycount = 0;
for c = cy:colhigh
ycount = ycount + 1;
s(xcount, ycount) = f(r, c);
end
end