-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainPSO.m
More file actions
133 lines (100 loc) · 5.15 KB
/
mainPSO.m
File metadata and controls
133 lines (100 loc) · 5.15 KB
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
close all
clear
clc
fprintf("Inizio codice...\n");
fprintf("Caricamento immagini...\n");
fixedImageStruct = nii_tool('load', 'Task02_Heart/imagesTr/la_019.nii.gz');
movingImageStruct = nii_tool('load', 'Task02_Heart/labelsTr/la_019.nii.gz');
fixedImage = double(fixedImageStruct.img);
movingImage = double(movingImageStruct.img);
fixedImage = imgaussfilt3(fixedImage, 1);
movingImage = imgaussfilt3(movingImage, 1);
fprintf("Dimensioni immagine fissa: [%d %d %d]\n", size(fixedImage));
fprintf("Dimensioni immagine mobile: [%d %d %d]\n", size(movingImage));
if any(size(fixedImage) ~= size(movingImage))
error('Le dimensioni delle immagini fissa e mobile non corrispondono!');
end
cb_ref = [floor(size(fixedImage,1)/5), floor(size(fixedImage,2)/5), floor(size(fixedImage,3)/5);
floor(size(fixedImage,1)/5), floor(size(fixedImage,2)/5*4), floor(size(fixedImage,3)/5*4);
floor(size(fixedImage,1)/5*4), floor(size(fixedImage,2)/5), floor(size(fixedImage,3)/5);
floor(size(fixedImage,1)/5*4), floor(size(fixedImage,2)/5*4), floor(size(fixedImage,3)/5*4)];
lb = [-5, -5, -5, -pi/2, -pi/2, -pi/2, 0.9];
ub = [5, 5, 5, pi/2, pi/2, pi/2, 1.1];
alpha = 0.2;
objective = @(params) objective_function(params, fixedImage, movingImage, alpha, cb_ref);
particles = 200;
sub_interval = 300;
dt = 10;
options = optimoptions("particleswarm", ...
"Display","iter", SwarmSize=200, MaxIterations=300, MaxStallTime=3,SocialAdjustmentWeight=2.05, SelfAdjustmentWeight=3.05);
monte_carlo_run = 10;
fprintf("Avvio ottimizzazione PSO...\n");
optimal_params_all = zeros(monte_carlo_run, 7);
optimal_values_all = zeros(monte_carlo_run, 1);
mi_values_all = zeros(monte_carlo_run, 1);
rmse_values_all = zeros(monte_carlo_run, 1);
for i = 1:monte_carlo_run
[optimal_params, optimal_value] = particleswarm(objective, 7, lb, ub, options);
optimal_params_all(i, :) = optimal_params;
optimal_values_all(i) = optimal_value;
fprintf('\n\nIterazione %d:', i);
fprintf('Parametri ottimali trovati:');
fprintf('tx = %.4f, ty = %.4f, tz = %.4f, theta_x = %.4f, theta_y = %.4f, theta_z = %.4f, scale = %.4f\n', optimal_params);
fprintf('Valore finale di Mutual Information: %.7g\n', optimal_value);
fprintf('\nOttimizzazione PSO completata.\n');
T_final = create_transformation_matrix(optimal_params(1), optimal_params(2), optimal_params(3), optimal_params(4), optimal_params(5), optimal_params(6), optimal_params(7));
fprintf('Determinante della matrice di rotazione: %.7g\n', det(T_final(1:3,1:3)));
tform = affine3d(T_final);
movingRegistered = imwarp(movingImage, tform, 'OutputView', imref3d(size(fixedImage)));
% figure; sliceViewer(fixedImage); title('Immagine Fissa');
% figure; sliceViewer(movingImage); title('Immagine Mobile');
% figure; sliceViewer(movingRegistered); title('Immagine Registrata');
mi_value = mutual_information(fixedImage, movingRegistered);
rmse_value = rmse_control_points(size(fixedImage), optimal_params, cb_ref);
mi_values_all(i) = mi_value;
rmse_values_all(i) = rmse_value;
fprintf('Mutual Information tra l immagine fissa e quella registrata: %.7g\n', mi_value);
fprintf('RMSE tra l immagine fissa e quella registrata: %.7g\n', rmse_value);
end
data_struct = struct('lb', lb, ...
'ub', ub, ...
'optimal_params_all', optimal_params_all, 'optimal_values_all', optimal_values_all, ...
'mi_values_all', mi_values_all, 'rmse_values_all', rmse_values_all);
json_text = jsonencode(data_struct);
fid = fopen('monte_carlo_results_pso.json', 'w');
if fid == -1
error('Impossibile aprire il file per la scrittura.');
end
fwrite(fid, json_text, 'char');
fclose(fid);
fprintf('\nFine codice\n\n');
function score = objective_function(params, fixedImage, movingImage, alpha, cb_ref)
rmse_score = rmse_control_points(size(fixedImage), params, cb_ref);
mi_val = mutual_information(fixedImage, movingImage);
score = alpha * mi_val + (1 - alpha) * rmse_score;
end
function T_final = create_transformation_matrix(tx, ty, tz, theta_x, theta_y, theta_z, scale)
Rz = [cos(theta_z), -sin(theta_z), 0; sin(theta_z), cos(theta_z), 0; 0, 0, 1];
Ry = [cos(theta_y), 0, sin(theta_y); 0, 1, 0; -sin(theta_y), 0, cos(theta_y)];
Rx = [1, 0, 0; 0, cos(theta_x), -sin(theta_x); 0, sin(theta_x), cos(theta_x)];
R = Rz * Ry * Rx;
S = scale * eye(3);
T_final = eye(4);
T_final(1:3,1:3) = R * S / norm(R * S, 'inf');
end
function mi_val = mutual_information(img1, img2)
img1 = img1(:); img2 = img2(:);
jointHist = histcounts2(img1, img2, 256, 'Normalization', 'probability');
px = sum(jointHist, 2); py = sum(jointHist, 1);
entropyX = entropy(px);
entropyY = entropy(py);
jointEntropy = -sum(jointHist(jointHist > 0) .* log2(jointHist(jointHist > 0)));
mi_val = entropyX + entropyY - jointEntropy;
end
function e = rmse_control_points(fixedSize, params, cb_ref)
T_final = create_transformation_matrix(params(1), params(2), params(3), params(4), params(5), params(6), params(7));
tform = affine3d(T_final);
cp_moving = transformPointsForward(tform, cb_ref);
diff = cp_moving - cb_ref;
e = sqrt(mean(sum(diff.^2, 2)));
end