-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfit_sphere_thin.m
38 lines (27 loc) · 1.24 KB
/
fit_sphere_thin.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
function [x_centre, y_centre, radius, psf_sigma, height, residual] = fit_sphere_thin(x_centre, y_centre, radius, psf_sigma, height, actual_image)
image_width = size(actual_image, 2);
image_height = size(actual_image, 1);
image_centre_x = image_width / 2;
image_centre_y = image_height / 2;
x = (1:image_width) - image_centre_x;
y = -(1:image_height) + image_centre_y;
[x, y] = meshgrid(x, y);
X = [x(:) y(:)];
% Background subtraction
background = median(actual_image(actual_image < mean(actual_image(:))));
actual_image = double(actual_image - background);
f = @(initial_params, X) cross_section_sphere_thin(initial_params(1), initial_params(2), initial_params(3), initial_params(4), initial_params(5), X);
assignin('base','ActImage', actual_image);
assignin('base','XX', X);
assignin('base','ff', f);
initial_params = [x_centre, y_centre, radius, psf_sigma, height];
fit_model = fitnlm(X, actual_image(:), f, initial_params);
fit_params = fit_model.Coefficients.Estimate;
x_centre = fit_params(1);
y_centre = fit_params(2);
radius = fit_params(3);
psf_sigma = fit_params(4);
height = fit_params(5);
fit_image = image_sphere_thin(x_centre, y_centre, radius, psf_sigma, height, actual_image);
residual = sum((actual_image(:) - fit_image(:)).^2);
end