-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblurMetric.m
102 lines (81 loc) · 3.12 KB
/
blurMetric.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
function blur = blurMetric(original)
% original : entry image
% The idea is from "The Blur Effect: Perception and Estimation with a New No-Reference Perceptual Blur Metric"
% Crét?Roffet F., Dolmiere T., Ladret P., Nicolas M. - GRENOBLE - 2007
% In SPIE proceedings - SPIE Electronic Imaging Symposium Conf Human Vision and Electronic Imaging, États-Unis d'Amérique (2007)
% Written by DO Quoc Bao, PhD Student in L2TI Laboratory, Paris 13 University, France
% Email: [email protected], [email protected]
% Last changed: 21-09-2008
%%%%%%%%%%%%%%%%%%Note: the output (blur) is in [0,1]; 0 means sharp, 1 means blur%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Please cite the author when you use this code. All remarks are welcome. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I = double(original);
[x, y] = size(I);
Hv = [1 1 1 1 1 1 1 1 1]/9;
Hh = Hv';
B_Ver = imfilter(I,Hv);%blur the input image in vertical direction
B_Hor = imfilter(I,Hh);%blur the input image in horizontal direction
wSize = 9;
vRange = zeros(y, 2);
for i = 1: y
ind = find(I(:, i), 1);
if ~isempty(ind)
vRange(i, 1) = ind + wSize;
end
ind = find(I(:, i), 1, 'last');
if ~isempty(ind)
vRange(i, 2) = ind - wSize;
end
end
hRange = zeros(x, 2);
for i = 1: x
ind = find(I(i, :), 1);
if ~isempty(ind)
hRange(i, 1) = ind + wSize;
end
ind = find(I(i, :), 1, 'last');
if ~isempty(ind)
hRange(i, 2) = ind - wSize;
end
end
function D = vDiff(I)
D = zeros(size(I));
for iCol = 1: y
if vRange(iCol, 1) ~= 0 || vRange(iCol, 2) ~= 0
ind1 = vRange(iCol, 1);
ind2 = vRange(iCol, 2);
D(:, ind1: ind2-1) = abs(I(:, ind1: (ind2 - 1)) - I(:, (ind1 + 1): ind2));
end
end
end
function D = hDiff(I)
D = zeros(size(I));
for iRow = 1: x
if hRange(iRow, 1) ~= 0 || hRange(iRow, 2) ~= 0
ind1 = hRange(iRow, 1);
ind2 = hRange(iRow, 2);
D(ind1: ind2-1, :) = abs(I(ind1: (ind2 - 1), :) - I((ind1 + 1): ind2, :));
end
end
end
D_F_Ver = vDiff(I);
D_F_Hor = hDiff(I);
% D_F_Ver = abs(I(:,1:x-1) - I(:,2:x));%variation of the input image (vertical direction)
% D_F_Hor = abs(I(1:y-1,:) - I(2:y,:));%variation of the input image (horizontal direction)
D_B_Ver = vDiff(B_Ver);
D_B_Hor = hDiff(B_Hor);
% D_B_Ver = abs(B_Ver(:,1:x-1)-B_Ver(:,2:x));%variation of the blured image (vertical direction)
% D_B_Hor = abs(B_Hor(1:y-1,:)-B_Hor(2:y,:));%variation of the blured image (horizontal direction)
T_Ver = D_F_Ver - D_B_Ver;%difference between two vertical variations of 2 image (input and blured)
T_Hor = D_F_Hor - D_B_Hor;%difference between two horizontal variations of 2 image (input and blured)
V_Ver = max(0,T_Ver);
V_Hor = max(0,T_Hor);
S_D_Ver = sum(sum(D_F_Ver(2:y-1,2:x-1)));
S_D_Hor = sum(sum(D_F_Hor(2:y-1,2:x-1)));
S_V_Ver = sum(sum(V_Ver(2:y-1,2:x-1)));
S_V_Hor = sum(sum(V_Hor(2:y-1,2:x-1)));
blur_F_Ver = (S_D_Ver-S_V_Ver)/S_D_Ver;
blur_F_Hor = (S_D_Hor-S_V_Hor)/S_D_Hor;
blur = max(blur_F_Ver,blur_F_Hor);
end