-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_norm_histogram.m
332 lines (257 loc) · 10.7 KB
/
compute_norm_histogram.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
function [img,final_grad] = compute_norm_histogram(img_name)
% Implementation of the basic idea of the Arbelaez's algorithm to compute
% the oriented gradient of histogram.
%
%%%%%%%%%%%% ALGORITHM FOR EFFICIENT COMPUTATION %%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 1.Rotate the intensity image of a certain angle
% 2.Approximate the circle with a rectangle
% 3.Consider 2 different rectangles (upper part/lower part)
% 4.Compute the integral image of the rotated image
% 5.Compute the histogram of each halves of the rectangle using the sum of
% over the region with the integral image, as stated in the paper:
% J(P)+J(S)-J(Q)-J(R)
% 6.Compute the difference between the histograms of the upper/lower part
% of the rectangle
% 7.Rotate the image back
% 8.Repeat the previous points for all the bins
% 9.Compute Chi squared distance between the two histograms
%
% Input: image
% Output: image and computed gradient
%
% Authors: Ali Alessio Salman, Marìa Silos
%conditional variables
DEBUG=0;
ROTATE=0;
%Loading the image
%in this way is independent from the OS and the current file system
%pwd specify the current directory and filesep is '\' on windows and '/' on Linux
%has to be run inside ADIP directory
root_path = pwd;
path_im=[root_path filesep 'Images' filesep];
image = img_name;
im=imread([path_im image]);
img=im;
%INIT
num_bins=8;
neighbors=5; % Window size
border_rows=5;
border_cols=5;
%these variables are only for testing purposes on the histogram values
%less pixels ==> faster computation
test_rows=250;
test_cols=250;
%width of the rectangle on which the histogram will
%be computed for each pixel, area: 10x10
width1=4;
width2=5;
rows=size(im,1);
cols=size(im,2);
gradient_dens_x=zeros(rows,cols); % black image
gradient_dens_y=zeros(rows,cols);
r_gradient_dens_x=zeros(rows,cols);
r_gradient_dens_y=zeros(rows,cols);
%HISTOGRAM VARIABLES
%normal
up_hist=[]; % Basic Matrix assignment of Upper Histogram
down_hist=[]; % Basic Matrix assignment of Lower Histogram
left_hist=[];
right_hist=[];
%rotated
r_up_hist=[]; % Basic Matrix assignment of Upper Histogram
r_down_hist=[]; % Basic Matrix assignment of Lower Histogram
r_left_hist=[];
r_right_hist=[];
% Convert RGB image to grayscale
if size(im,3)==3
im=rgb2gray(im);
end
%median filter, not sure if it's improving or not
%im=medfilt2(im,[3 3]);
figure(100);
imshow(im);
%%%%%%%%%%%% ALGORITHM FOR EFFICIENT COMPUTATION %%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 1.Rotate the intensity image of a certain angle
% 2.Approximate the circle with a rectangle
% 3.Consider 2 different rectangles (upper part/lower part)
% 4.Compute the integral image of the rotated image
% 5.Compute the histogram of each halves of the rectangle using the sum of
% over the region with the integral image, as stated in the paper:
% J(P)+J(S)-J(Q)-J(R)
% 6.Compute the difference between the histograms of the upper/lower part
% of the rectangle
% 7.Rotate the image back
% 8.Repeat the previous points for all the bins
% 9.Compute Chi squared distance between the two histograms
rows=size(im,1);
cols=size(im,2);
%we process each histogram bin separately
I_b_cell=compute_histogram_bins(im,num_bins);
%I_b_cell=compute_hist_bins_with_imhist(im,num_bins);
%initialize the cell that will contain all the integral images
J_cell=cell(1,num_bins);
[J_cell{:}] = deal(zeros(rows,cols));
for n=1:num_bins
J_cell{1,n} = integralImage(I_b_cell{1,n});
end
toc
%%%%%%%%%%%% ROTATION INIT %%%%%%%%%%%%%%%%%%%%%%%%%%%
if ROTATE==1
im_r=imrotate(im,-45);
r_rows=size(im_r,1);
r_cols=size(im_r,2);
I_b_cell=compute_histogram_bins(im_r,num_bins);
%initialize the cell that will contain all the integral images
J_cell_rot=cell(1,num_bins);
[J_cell_rot{:}] = deal(zeros(rows,cols));
for n=1:num_bins
J_cell_rot{1,n} = integralImage(I_b_cell{1,n});
end
end
% %****** TESTING VARIABLES ******%
% %variable for my histogram
% if DEBUG == 1
% TEST_HIST=cell(test_rows - border_rows , test_cols - border_cols);
% [TEST_HIST{:}] = deal(zeros(1,num_bins));
%
% %variable for matlab histogram function
% TEST_HIST2=cell(test_rows - border_rows , test_cols - border_cols);
% [TEST_HIST2{:}] = deal(zeros(1,num_bins));
%
%
% rows=test_rows;
% cols=test_cols;
% end
%
%save('var.mat');
%%
%load('var.mat');
% Histograms of the central part (without taking into account the 5 pixels
% borders)
%
for r=width2:rows-width2
for c=width2:cols-width2
% Oriented rotated histogram
if DEBUG==1
tmp_hist=TEST_HIST{r,c}; %1 x num_bins matrix
end
%%%%%%%%%%%%%%%%%%%%%%%% NORMAL ORIENTATION %%%%%%%%%%%%%%%%%%%%%%
%computing the histogram using the integral image
for n=1:num_bins
%retrieving the proper integral image according to the bin we
%are processing
J=J_cell{1,n};
%the integralimage based sum region takes just 10^-5 sec
%Define rectangular region as [startingRow, startingColumn, endingRow, endingColumn].
%******* X-AXIS *******%
%UPPER PART
[sR sC eR eC] = deal(r-width1,c-width1,r,c+width2);
regionSum = J(eR+1,eC+1) - J(eR+1,sC) - J(sR,eC+1) + J(sR,sC);
up_hist(n)=regionSum;
%saving to a variable for further comparison later
tmp_hist(n)=up_hist(n);
%LOWER PART
[sR sC eR eC] = deal(r+1,c-width1,r+width2,c+width2);
regionSum = J(eR+1,eC+1) - J(eR+1,sC) - J(sR,eC+1) + J(sR,sC);
down_hist(n)=regionSum;
%******* Y-AXIS ******%
%LEFT PART
[sR sC eR eC] = deal(r-width1,c-width1,r+width2,c);
regionSum = J(eR+1,eC+1) - J(eR+1,sC) - J(sR,eC+1) + J(sR,sC);
left_hist(n)=regionSum;
%RIGHT PART
[sR sC eR eC] = deal(r-width1,c+1,r+width2,c+width2);
regionSum = J(eR+1,eC+1) - J(eR+1,sC) - J(sR,eC+1) + J(sR,sC);
right_hist(n)=regionSum;
end
%***+ CROSS CHECKING WITH MATLAB FUNCTIONS *****
%upper part
%new_im_up=im(r-width1:r,c-width1:c+width2); % cut the image in order to have the upper part
%[counts_x,~]=histcounts(new_im_up,num_bins);
%fprintf('counts(n)= %d\n',counts_x(n));
%fprintf('r_up_hist(n)= %d\n\n\n', r_up_hist(n));
if DEBUG == 1
%saving for further comparison later
TEST_HIST{r,c}=tmp_hist;
TEST_HIST2{r,c}=counts_x;
end
%setting 0 values to 1s to compute correctly the distance between
%the histogram. Crucial to have accurate results
up_hist(up_hist==0)=1;
down_hist(down_hist==0)=1;
left_hist(left_hist==0)=1;
right_hist(right_hist==0)=1;
%computing the gradient from the histogram
sum_val_x=sum((up_hist-down_hist).^2./(up_hist+down_hist));
gradient_magnitude_x=0.5*sum_val_x;
sum_val_y= sum((left_hist-right_hist).^2./(left_hist+right_hist));
gradient_magnitude_y=0.5*sum_val_y;
% Max val of both
gradient_dens_x(r,c)=gradient_magnitude_x; %10^-5
gradient_dens_y(r,c)=gradient_magnitude_y;
gradient_dens_max(r,c)=max(gradient_magnitude_x, gradient_magnitude_y);
end
end
if ROTATE == 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ROTATED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for r=width2:r_rows-width2
for c=width2:r_cols-width2
% Oriented rotated histogram
%computing the histogram using the integral image
for n=1:num_bins
%retrieving the proper integral image according to the bin we
%are processing
J=J_cell_rot{1,n};
%the integralimage based sum region takes just 10^-5 sec
%Define rectangular region as [startingRow, startingColumn, endingRow, endingColumn].
%******* X-AXIS *******%
%UPPER PART
[sR sC eR eC] = deal(r-width1,c-width1,r,c+width2);
regionSum = J(eR+1,eC+1) - J(eR+1,sC) - J(sR,eC+1) + J(sR,sC);
r_up_hist(n)=regionSum;
%saving to a variable for further comparison later
tmp_hist(n)=r_up_hist(n);
%LOWER PART
[sR sC eR eC] = deal(r+1,c-width1,r+width2,c+width2);
regionSum = J(eR+1,eC+1) - J(eR+1,sC) - J(sR,eC+1) + J(sR,sC);
r_down_hist(n)=regionSum;
%******* Y-AXIS ******%
%LEFT PART
[sR sC eR eC] = deal(r-width1,c-width1,r+width2,c);
regionSum = J(eR+1,eC+1) - J(eR+1,sC) - J(sR,eC+1) + J(sR,sC);
r_left_hist(n)=regionSum;
%RIGHT PART
[sR sC eR eC] = deal(r-width1,c+1,r+width2,c+width2);
regionSum = J(eR+1,eC+1) - J(eR+1,sC) - J(sR,eC+1) + J(sR,sC);
r_right_hist(n)=regionSum;
end
r_up_hist(r_up_hist==0)=1;
r_down_hist(r-down_hist==0)=1;
r_left_hist(r_left_hist==0)=1;
r_right_hist(r_right_hist==0)=1;
r_sum_val_x=sum((r_up_hist-r_down_hist).^2./(r_up_hist+r_down_hist));
r_gradient_magnitude_x=0.5*r_sum_val_x;
r_sum_val_y= sum((r_left_hist-r_right_hist).^2./(r_left_hist+r_right_hist));
r_gradient_magnitude_y=0.5*r_sum_val_y;
% Max val of both
r_gradient_dens_x(r,c)=r_gradient_magnitude_x; %10^-5
r_gradient_dens_y(r,c)=r_gradient_magnitude_y;
r_gradient_dens_max(r,c)=max(r_gradient_magnitude_x,r_gradient_magnitude_y);
%gradient_dens_max(r,c)=max(gradient_magnitude_x, gradient_magnitude_y,r_gradient_dens_x,r_gradient_dens_y);
%gradient_dens_max(r,c)=gradient_dens_x(r,c);
end
end
%rotate the image gradient BACK
r_gradient_dens_x=imrotate(r_gradient_dens_x,45);
r_gradient_dens_y=imrotate(r_gradient_dens_y,45);
r_gradient_dens_max=imrotate(r_gradient_dens_max,45);
r_gradient_dens_x=cropMargins(r_gradient_dens_x);
r_gradient_dens_y=cropMargins(r_gradient_dens_y);
r_gradient_dens_max=cropMargins(r_gradient_dens_max);
end
% gmax=gradient_dens_max;
% save('xy.mat','gmax');
%final_max_grad=max(gradient_dens_max,r_gradient_dens_max);
final_grad = gradient_dens_max;