0001 function build_1d_model(spline_type, placement_type, objective_function)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067 frame_counter = 0;
0068 n_images = 10;
0069
0070 n_iterations = 10;
0071
0072 n_sets = 1;
0073
0074
0075
0076 image_width = 50;
0077
0078 spline_type = 'multi_point';
0079
0080
0081 placement_type = 'grid';
0082 objective_function = 'msd_opt_together';
0083 verbose = 'off';
0084
0085 n_points = 5;
0086
0087
0088 do_plot = 1;
0089
0090
0091 white_ctr = 0;
0092
0093 ref_shift = 0.2;
0094
0095 max_shift = 0.2;
0096
0097 step = 0.1;
0098
0099 los = zeros(n_images,floor((max_shift-ref_shift)/step));
0100 his = zeros(n_images,floor((max_shift-ref_shift)/step));
0101
0102 blurred = 0;
0103
0104 spec_iters = 25;
0105
0106 gen_iters = 25;
0107
0108 min_error = 0;
0109 max_error = 1;
0110
0111 error_step = 0.1;
0112 n_error_steps = (max_error-min_error)/error_step;
0113
0114
0115
0116
0117 tic;
0118
0119 for this_set = 1:n_sets
0120
0121 tic;
0122
0123
0124
0125
0126 ['calculating set ', num2str(this_set)]
0127
0128 [imagelist images points his los] = make_1d_images(n_images, image_width, 0.2);
0129
0130
0131
0132
0133
0134
0135 points = -1 + 2*(points-1)/(image_width-1);
0136
0137 keep = 0.999999;
0138
0139 ref_points_vec = points(:,1);
0140 ref_image_vec = images(:,1);
0141
0142 ref_hi = his(1);
0143 ref_lo = los(1);
0144
0145
0146 if(do_plot)
0147 subplot_fig = figure;
0148 H=figure(subplot_fig);
0149 for i=1:n_images,
0150 subplot(n_images,2,(2*i)-1);
0151 plot(images(:,i)),title('Unwarped images');
0152 end
0153 end
0154
0155
0156
0157
0158
0159
0160 warped_images = images;
0161 warped_points = points;
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181 for n=1:n_iterations
0182
0183 disp(['iter ',num2str(n)]);
0184
0185
0186 for i=1:n_images
0187 disp(['Warping image ',num2str(i),' of ',num2str(n_images)]);
0188 image_vec = warped_images(:,i);
0189 points_vec = warped_points(:,i);
0190 if(strcmp(objective_function,'model_opt_together'))
0191 if(do_plot)
0192 frame_counter = frame_counter + 1;
0193 M(frame_counter) = getframe(H);
0194 [param_list, warped_point, warped_image, score] = optimise_all_warps_model([warped_images(:,1:i-1),warped_images(:,i+1:n_images)], [warped_points(:,1:i-1),warped_points(:,i+1:n_images)], image_vec, points_vec,spline_type,placement_type,n_points, verbose, do_plot, subplot_fig,i*2);
0195 else
0196 [param_list, warped_point, warped_image, score] = optimise_all_warps_model([warped_images(:,1:i-1),warped_images(:,i+1:n_images)], [warped_points(:,1:i-1),warped_points(:,i+1:n_images)], image_vec, points_vec,spline_type,placement_type,n_points, verbose, do_plot);
0197 end
0198 elseif(strcmp(objective_function,'model_opt_separate'))
0199 if(do_plot)
0200 frame_counter = frame_counter + 1;
0201 M(frame_counter) = getframe(H);
0202 [param_list, warped_point, warped_image, score] = optimise_warps_model([warped_images(:,1:i-1),warped_images(:,i+1:n_images)], [warped_points(:,1:i-1),warped_points(:,i+1:n_images)], image_vec, points_vec,spline_type,placement_type,n_points, verbose, do_plot, subplot_fig,i*2);
0203 else
0204 [param_list, warped_point, warped_image, score] = optimise_warps_model([warped_images(:,1:i-1),warped_images(:,i+1:n_images)], [warped_points(:,1:i-1),warped_points(:,i+1:n_images)], image_vec, points_vec,spline_type,placement_type,n_points, verbose, do_plot);
0205 end
0206 elseif(strcmp(objective_function,'msd_opt_together'))
0207 if(do_plot)
0208 frame_counter = frame_counter + 1;
0209 M(frame_counter) = getframe(H);
0210 [param_list, warped_point, warped_image, score] = optimise_all_warps_msd(ref_image_vec, ref_points_vec, image_vec, points_vec, spline_type, placement_type, n_points, verbose, do_plot, subplot_fig,i*2);
0211 else
0212 [param_list, warped_point, warped_image, score] = optimise_all_warps_msd(ref_image_vec, ref_points_vec, image_vec, points_vec, spline_type, placement_type, n_points, verbose, do_plot);
0213 end
0214 elseif(strcmp(objective_function,'msd_opt_separate'))
0215 if(do_plot)
0216 [param_list, warped_point, warped_image, score] = optimise_warps_msd(ref_image_vec, ref_points_vec, image_vec, points_vec, spline_type, placement_type, n_points, verbose, do_plot, subplot_fig,i*2);
0217 frame_counter = frame_counter + 1;
0218 M(frame_counter) = getframe(H);
0219 else
0220 [param_list, warped_point, warped_image, score] = optimise_warps_msd(ref_image_vec, ref_points_vec, image_vec, points_vec, spline_type, placement_type, n_points, verbose, do_plot);
0221 end
0222 else
0223 error(['Unknown objective function: ', objective_function]);
0224 end
0225
0226 warped_images(:,i) = warped_image;
0227 warped_points(:,i) = warped_point;
0228
0229
0230
0231 if(n==n_iterations),
0232 final_score(i, this_set) = score;
0233 end
0234 end
0235
0236
0237
0238
0239 end
0240
0241
0242
0243
0244
0245 w_c_model = build_model(warped_images, warped_points, keep,'Optimised warp', 'variance');
0246
0247
0248
0249
0250
0251
0252 fig_title = 'Automatically aligned: ';
0253
0254
0255
0256
0257 w_intensity_total_vars = w_c_model.intensity_model.total_var;
0258 w_shape_total_vars = w_c_model.shape_model.total_var;
0259
0260
0261
0262 model_score(this_set) = measure_model(w_c_model.variances, 20);
0263 msd_score(this_set) = measure_model_msd(warped_images);
0264 shape_modes(this_set) = w_c_model.n_shape_modes;
0265 intensity_modes(this_set) = size(w_c_model.intensity_model.pcs,2);
0266 shape_variance(this_set) = sum(w_c_model.shape_model.variances);
0267 intensity_variance(this_set) = sum(w_c_model.intensity_model.variances);
0268
0269 t = toc;
0270 disp(['Time for set ',num2str(this_set),': ',num2str(t)]);
0271 time(this_set)=t;
0272
0273
0274 end
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288 disp('Mean match score:');
0289 mean(final_score(:))
0290 disp('Std match score:');
0291 std(final_score(:))
0292
0293 disp('Mean model score:');
0294 mean(model_score(:))
0295 disp('Std model score:');
0296 std(model_score(:))
0297
0298 disp('Mean msd score:');
0299 mean(msd_score(:))
0300 disp('Std msd score:');
0301 std(msd_score(:))
0302
0303 disp('Mean shape modes:');
0304 mean(shape_modes(:))
0305 disp('Std shape modes:');
0306 std(shape_modes(:))
0307
0308 disp('Mean intensity modes:');
0309 mean(intensity_modes(:))
0310 disp('Std intensity modes:');
0311 std(intensity_modes(:))
0312
0313 disp('Mean shape variance:');
0314 mean(shape_variance(:))
0315 disp('Std shape variance:');
0316 std(shape_variance(:))
0317
0318 disp('Mean intensity variance:');
0319 mean(intensity_variance(:))
0320 disp('Std intensity variance:');
0321 std(intensity_variance(:))
0322
0323 disp('Mean time: ');
0324 mean(time(:))
0325 disp('Std time: ');
0326 std(time(:))
0327
0328 t = toc;
0329
0330 disp(['Total time: ',num2str(t)]);
0331
0332
0333 movie(M);
0334 movie2avi(M,'set10.avi');
0335
0336
0337
0338