-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDCT_Corr_Eng.m
225 lines (178 loc) · 7.31 KB
/
DCT_Corr_Eng.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
% C)Mostafa Amin-Naji, Babol Noshirvani University of Technology,
% My Official Website: www.Amin-Naji.com
% My Email: [email protected]
% PLEASE CITE THE BELOW PAPER IF YOU USE THIS CODE
%M. Amin-Naji and A. Aghagolzadeh, “Multi-Focus Image Fusion in DCT Domain using
%Variance and Energy of Laplacian and Correlation Coefficient for Visual Sensor
%Networks,” Journal of AI and Data Miningm vol. 6, no. 2, 2018, pp. 233-250.
% DOI: http://dx.doi.org/10.22044/jadm.2017.5169.1624
clc
clear
close all
%Select First Image
disp('Please Select First Image:')
[filename, pathname]= uigetfile({'*.jpg;*.png;*.tif'},'Select First Image');
path=fullfile(pathname, filename);
im1=imread(path);
disp('Great! First Image is selected')
%Select Second Image
disp('Please Select Second Image:')
[filename, pathname]= uigetfile({'*.jpg;*.png;*.tif'},'Select Second Image');
path=fullfile(pathname, filename);
im2=imread(path);
disp('Great! Second Image is selected')
if size(im1,3) == 3 % Check if the images are grayscale
im1 = rgb2gray(im1);
end
if size(im2,3) == 3
im2 = rgb2gray(im2);
end
if size(im1) ~= size(im2) % Check if the input images are of the same size
error('Size of the source images must be the same!')
end
disp('congratulations! Fusion Process in Running')
% Get input image size
[m,n] = size(im1);
FusedDCT = zeros(m,n);
FusedDCT_CV = zeros(m,n);
Map = zeros(floor(m/8),floor(n/8));
% Level shifting
im1 = double(im1)-128;
im2 = double(im2)-128;
x=0.0751;
y=0.1238;
z=0.2042;
C=dctmtx(8);
t=[
y x 0 0 0 0 0 0
x y x 0 0 0 0 0
0 x y x 0 0 0 0
0 0 x y x 0 0 0
0 0 0 x y x 0 0
0 0 0 0 x y x 0
0 0 0 0 0 x y x
0 0 0 0 0 0 x y];
s=[
z y 0 0 0 0 0 0
y z y 0 0 0 0 0
0 y z y 0 0 0 0
0 0 y z y 0 0 0
0 0 0 y z y 0 0
0 0 0 0 y z y 0
0 0 0 0 0 y z y
0 0 0 0 0 0 y z];
u=[
x+2*y 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 x+2*y ];
v=[
0 x 0 0 0 0 0 0
x y x 0 0 0 0 0
0 x y x 0 0 0 0
0 0 x y x 0 0 0
0 0 0 x y x 0 0
0 0 0 0 x y x 0
0 0 0 0 0 x y x
0 0 0 0 0 0 x 0];
lu =[
0 1 0 0 0 0 0 0
1 0 1 0 0 0 0 0
0 1 0 1 0 0 0 0
0 0 1 0 1 0 0 0
0 0 0 1 0 1 0 0
0 0 0 0 1 0 1 0
0 0 0 0 0 1 0 1
0 0 0 0 0 0 1 0];
q =[
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1];
LU=C*lu*C';
T=C*t*C';
S=C*s*C';
U=C*u*C';
V=C*v*C';
Q=C*q*C';
tic
threshold1=0;
% Divide source images into 8*8 blocks and perform the fusion process
for i = 1:floor(m/8)
for j = 1:floor(n/8)
im1_Block = im1(8*i-7:8*i,8*j-7:8*j);
im2_Block = im2(8*i-7:8*i,8*j-7:8*j);
% Compute the 2-D DCT of 8*8 blocks
im1_Block_DCT = C*im1_Block*C';
im2_Block_DCT = C*im2_Block*C';
im1SubDct_LOW=(LU*im1_Block_DCT*T)+(im1_Block_DCT*S)+((Q*im1_Block_DCT*U)+(V*im1_Block_DCT*Q)+(Q*im1_Block_DCT*V));
im2SubDct_LOW=(LU*im2_Block_DCT*T)+(im2_Block_DCT*S)+((Q*im2_Block_DCT*U)+(V*im2_Block_DCT*Q)+(Q*im2_Block_DCT*V));
PimA=im1_Block_DCT-mean2(im1_Block_DCT);
PimB=im2_Block_DCT-mean2(im2_Block_DCT);
PimA_Low=im1SubDct_LOW-mean2(im1SubDct_LOW);
PimB_Low=im2SubDct_LOW-mean2(im2SubDct_LOW);
cor1= sum(sum(PimA.*PimA_Low))/sqrt(sum(sum(PimA.*PimA))*sum(sum(PimA_Low.*PimA_Low)));
cor2= sum(sum(PimB.*PimB_Low))/sqrt(sum(sum(PimB.*PimB))*sum(sum(PimB_Low.*PimB_Low)));
energy_A=sum(sum(im1_Block_DCT.^2));
energy_B=sum(sum(im2_Block_DCT.^2));
energy_A_Low=sum(sum(im1SubDct_LOW.^2));
energy_B_Low=sum(sum(im2SubDct_LOW.^2));
corr_eng_1=energy_A*(1-cor1)*energy_A_Low;
corr_eng_2=energy_B*(1-cor2)*energy_B_Low;
z=corr_eng_1;
zz=corr_eng_2;
if z>=zz
dctBlock = im1_Block_DCT;
Map(i,j) = -1; % Consistency verification index
end
if z<zz
dctBlock = im2_Block_DCT;
Map(i,j) = +1; % Consistency verification index
end
if z<zz+threshold1 && z>zz-threshold1
dctBlock = (im2_Block_DCT+im2_Block_DCT)./2;
Map(i,j) =0 ;
end
% Compute the 2-D inverse DCT of 8*8 blocks and construct fused image
FusedDCT(8*i-7:8*i,8*j-7:8*j) = C'*dctBlock*C; % DCT+Corr_Eng method
end
end
toc
% Concistency verification (CV) with Majority Filter (3x3 Averaging Filter)
fi=fspecial('average',3);
Map_Filtered = imfilter(Map, fi,'symmetric'); % Filtered index map
threshold2=0.00;
for i = 1:m/8
for j = 1:n/8
% DCT+Variance+CV method
if Map_Filtered(i,j) <= -threshold2
FusedDCT_CV(8*i-7:8*i,8*j-7:8*j) = im1(8*i-7:8*i,8*j-7:8*j);
end
if Map_Filtered(i,j) > threshold2
FusedDCT_CV(8*i-7:8*i,8*j-7:8*j) = im2(8*i-7:8*i,8*j-7:8*j);
end
if Map_Filtered(i,j) > -threshold2 && Map_Filtered(i,j) < threshold2
FusedDCT_CV(8*i-7:8*i,8*j-7:8*j) = (im1(8*i-7:8*i,8*j-7:8*j)+im2(8*i-7:8*i,8*j-7:8*j))./2;
end
end
end
% Inverse level shifting
im1 = uint8(double(im1)+128);
im2 = uint8(double(im2)+128);
FusedDCT = uint8(double(FusedDCT)+128);
FusedDCT_CV = uint8(double(FusedDCT_CV)+128);
% Show Images Table
subplot(2,2,1), imshow(im1), title('Source image 1');
subplot(2,2,2), imshow(im2), title('Source image 2');
subplot(2,2,3), imshow(FusedDCT), title('"DCT+Corr_Eng" fusion result');
subplot(2,2,4), imshow(FusedDCT_CV), title('"DCT+Corr_Eng+CV" fusion result');
% Good Luck
% Mostafa Amin-Naji ;)