Newer
Older
Nicola Vigano
committed
function [cl, estim_space_bbox_pix, estim_orient_bbox_rod] = gt6DCreateProjDataFromTwinnedGrain(grains_ids, variants, phase_id, varargin)
% FUNCTION cl = gt6DCreateProjDataFromTwinnedGrainFwdProj(parent_id, variants, phase_id, varargin)
% INPUT:
% parent_id: Parent grain ID (it could be the parent grain structure)
% variants: an array of structures defined like the following:
Nicola Vigano
committed
% struct( 'parent_ind', <ind>, 'var_num', <ind> )
Nicola Vigano
committed
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
% OUTPUT:
% samp_ors: is an array of grain structures containing the parent and its
% twins
if (~exist('phase_id', 'var'))
phase_id = 1;
end
conf = struct( ...
'verbose', false, ...
'det_index', 1, ...
'min_eta', 15, ...
'angular_tol', 1, ...
'rspace_oversize', 1, ...
'ospace_oversize', 1, ...
'check_spots', false, ...
'stack_oversize', 1.4, ...
'merge_dist_pixels', 0, ...
'use_raw_images', false, ...
'use_parent_mask', false, ...
'select_raw_shared', false, ...
'include_all', false, ...
'always_select_shared', false, ...
'save', false );
conf = parse_pv_pairs(conf, varargin);
if (~any(conf.use_raw_images) && conf.include_all)
warning('gt6DCreateProjDataFromTwinnedGrainFwdProj:wrong_argument', ...
'"include_all" cannot be enabled if "use_raw_images" is not enabled')
conf.include_all = false;
end
p = gtLoadParameters();
symm = gtCrystGetSymmetryOperators(p.cryst(phase_id).crystal_system);
if (~isstruct(grains_ids))
grs = gtLoadGrain(phase_id, grains_ids);
for ii_g = 1:numel(grs)
grs(ii_g) = gtCalculateGrain(grs(ii_g), p);
end
else
grs = grains_ids;
end
variants_num = numel(variants);
if (variants_num > 0)
Nicola Vigano
committed
fprintf('Loading variants: ')
for ii_var = variants_num:-1:1
num_chars = fprintf('%02d (type: %s)', variants_num-ii_var+1, variants(ii_var).type);
n_p = variants(ii_var).parent_ind;
n_v = variants(ii_var).var_num;
grs(end+1) = twin_fwd_sim_to_gr(grs(n_p), grs(n_p).twin_vars(n_v)); %#ok<AGROW>
fprintf(repmat('\b', [1 num_chars]));
fprintf(repmat(' ', [1 num_chars]));
fprintf(repmat('\b', [1 num_chars]));
end
fprintf('Done.\n')
Nicola Vigano
committed
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
end
grs_list = cat(2, grs(:).id);
num_grains = numel(grs_list);
if (conf.verbose)
fprintf('R_vectors:\n')
cat(1, grs(:).R_vector)
fprintf('Reciprocal disorientations:\n')
for ii_g_1 = 1:num_grains
grs_to_check = (ii_g_1+1):num_grains;
if (numel(grs_to_check) > 0)
fprintf(' - Disorientations from grain: %d\n', grs(ii_g_1).id)
for ii_g_2 = grs_to_check
dis_angle = gtDisorientation(grs(ii_g_1).R_vector', grs(ii_g_2).R_vector', symm);
fprintf(' + Grain %d: %f\n', grs(ii_g_2).id, dis_angle);
end
end
end
end
refgr = grs(1);
refgr_omind = refgr.allblobs.omind;
for ii_g = num_grains:-1:2
grs(ii_g) = gtGrainAllblobsFilterOrder(grs(ii_g), refgr_omind);
end
if (numel(conf.use_raw_images) == 1)
conf.use_raw_images = conf.use_raw_images(ones(num_grains, 1));
end
fprintf('Determining real-space bounding box..')
c = tic();
% Determining real-space bounding box. It is assumed to be contiguos.
[gr_center_pix, bbox_size_pix, estim_space_bbox_pix] = gt6DMergeRealSpaceVolumes(grs, conf.det_index, conf.rspace_oversize);
gr_center_mm = gtGeoSam2Sam(gr_center_pix, p.recgeo, p.samgeo, false, false);
estim_space_bbox_mm = [ ...
gtGeoSam2Sam(estim_space_bbox_pix(1:3), p.recgeo, p.samgeo, false, false), ...
gtGeoSam2Sam(estim_space_bbox_pix(4:6), p.recgeo, p.samgeo, false, false) ];
bbox_size_mm = estim_space_bbox_mm(4:6) - estim_space_bbox_mm(1:3);
fprintf('\b\b: Done in %g seconds.\n', toc(c))
fprintf(' - Estimated extent: [%3g, %3d, %3d] -> [%3d, %3d, %3d]\n', estim_space_bbox_pix);
fprintf(' BBox size: %3d, %3d, %3d (%g, %g, %g mm)\n', bbox_size_pix, bbox_size_mm);
estim_orient_bbox_rod = zeros(num_grains, 6);
bbox_size_rod = zeros(num_grains, 3);
% Determining orientation-space bounding boxes. They are assumed to be
% detached from each others.
for ii_g = 1:num_grains
fprintf('Determining orientation-space region: %d/%d\n', ii_g, num_grains)
sampler = GtOrientationSampling(p, grs(ii_g), ...
'detector_index', conf.det_index, 'verbose', conf.verbose);
r_vecs = sampler.guess_ODF_BB()';
estim_orient_bbox_rod(ii_g, :) = [min(r_vecs, [], 1), max(r_vecs, [], 1)];
bbox_size_rod(ii_g, :) = estim_orient_bbox_rod(ii_g, 4:6) - estim_orient_bbox_rod(ii_g, 1:3);
% oversizing the orienation a bit
delta_bbox_size_rod = bbox_size_rod(ii_g, :) .* (conf.ospace_oversize - 1) / 2;
estim_orient_bbox_rod(ii_g, :) = estim_orient_bbox_rod(ii_g, :) + [-delta_bbox_size_rod, delta_bbox_size_rod];
bbox_size_rod(ii_g, :) = estim_orient_bbox_rod(ii_g, 4:6) - estim_orient_bbox_rod(ii_g, 1:3);
bbox_size_deg = 2 * atand(bbox_size_rod(ii_g, :));
fprintf(' - Estimated extent: [%g, %g, %g] -> [%g, %g, %g]\n', estim_orient_bbox_rod(ii_g, :));
fprintf(' BBox size: %3.3f, %3.3f, %3.3f (deg)\n', bbox_size_deg);
end
samp_ors = cell(num_grains, 1);
% Computing the new grain structures
for ii_g = 1:num_grains
refor = struct( ...
'id', grs(ii_g).id, 'phaseid', grs(ii_g).phaseid, ...
'center', gr_center_mm, 'R_vector', grs(ii_g).R_vector );
refor = gtCalculateGrain(refor, p, 'ref_omind', refgr_omind);
refor.bb_ors = gt6DCalculateFullSpaceCorners(estim_space_bbox_mm, ...
bbox_size_mm, estim_orient_bbox_rod(ii_g, :), ...
bbox_size_rod(ii_g, :), refgr, p);
samp_ors{ii_g} = refor;
end
samp_ors = [samp_ors{:}];
num_images = gtGetTotNumberOfImages(p);
Nicola Vigano
committed
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
% Let's now list all blobs, and the corresponding UVW bounding boxes
% from the orientation-bbs projections (not segmentation of the bls)
all_blobs_list = grs(1).proj(conf.det_index).bl;
for ii_g = 2:num_grains
all_blobs_list = cat(2, all_blobs_list, grs(ii_g).proj(conf.det_index).bl);
end
num_blobs = numel(all_blobs_list);
blobs_uvw_boundaries = zeros(num_blobs, 6);
blobs_ids = zeros(num_blobs, 1);
base_counter = 0;
was_selected = false(num_blobs, 1);
blob_use_raw_images = false(num_blobs, 1);
blobs_etas = zeros(num_blobs, 1);
for ii_g = 1:num_grains
num_bl_grain = numel(grs(ii_g).proj(conf.det_index).bl);
inds_bls = (base_counter+1):(base_counter+num_bl_grain);
inc_bls = grs(ii_g).proj(conf.det_index).ondet(grs(ii_g).proj(conf.det_index).included);
num_bb_ors = numel(samp_ors(ii_g).bb_ors);
if (conf.use_raw_images(ii_g))
uvw_tab = zeros(numel(inc_bls), 3, num_bb_ors);
for ii_ab = 1:num_bb_ors
uvw_tab(:, :, ii_ab) = samp_ors(ii_g).bb_ors(ii_ab).allblobs.detector(conf.det_index).uvw(inc_bls, :);
end
else
bls_u_lims = cat(1, grs(ii_g).proj(conf.det_index).bl(:).mbbu);
bls_v_lims = cat(1, grs(ii_g).proj(conf.det_index).bl(:).mbbv);
bls_w_lims = cat(1, grs(ii_g).proj(conf.det_index).bl(:).mbbw);
uvw_tab = cat(3, ...
[bls_u_lims(:, 1), bls_v_lims(:, 1), bls_w_lims(:, 1)], ...
[bls_u_lims(:, 2), bls_v_lims(:, 2), bls_w_lims(:, 2)] );
end
blobs_uvw_boundaries(inds_bls, 1:3) = min(floor(uvw_tab), [], 3);
blobs_uvw_boundaries(inds_bls, 4:6) = max(ceil(uvw_tab), [], 3);
Nicola Vigano
committed
blobs_uvw_boundaries(inds_bls, [3 6]) = gtGrainAnglesTabularFix360deg( ...
blobs_uvw_boundaries(inds_bls, [3 6]), samp_ors(ii_g).allblobs.detector(conf.det_index).uvw(inc_bls, 3), p);
blobs_etas(inds_bls) = samp_ors(ii_g).allblobs.eta(inc_bls);
Nicola Vigano
committed
blobs_ids(inds_bls) = grs(ii_g).fwd_sim(conf.det_index).difspotID;
was_selected(inds_bls) = grs(ii_g).proj(conf.det_index).selected;
blob_use_raw_images(inds_bls) = conf.use_raw_images(ii_g);
base_counter = base_counter + num_bl_grain;
end
is_merged = false(size(blobs_ids));
% We will now scan for blobs which have the same ID and merge them
for ii_b = 1:num_blobs
ind_b_others = (ii_b+1:num_blobs)';
matches = (blobs_ids(ii_b) == blobs_ids(ind_b_others)) & ~is_merged(ind_b_others);
if (any(matches))
merge_list = [ii_b; ind_b_others(matches)];
blobs_uvw_boundaries(ii_b, 1:3) = min(blobs_uvw_boundaries(merge_list, 1:3), [], 1);
blobs_uvw_boundaries(ii_b, 4:6) = max(blobs_uvw_boundaries(merge_list, 4:6), [], 1);
was_selected(ii_b) = any(was_selected(merge_list)) || conf.always_select_shared;
if (conf.select_raw_shared)
blob_use_raw_images(ii_b) = any(blob_use_raw_images(merge_list));
Nicola Vigano
committed
else
blob_use_raw_images(ii_b) = all(blob_use_raw_images(merge_list));
Nicola Vigano
committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
end
is_merged(ind_b_others(matches)) = true;
end
end
% Updating structures, to remove merged blobs
all_blobs_list(is_merged) = [];
blobs_uvw_boundaries(is_merged, :) = [];
blob_use_raw_images(is_merged, :) = [];
blobs_etas(is_merged) = [];
was_selected(is_merged) = [];
num_blobs = numel(all_blobs_list);
fprintf('Determining which blobs are close in UVW space: ')
c = tic();
% Transforming blob boundaries to blob center + halfwidth
blobs_uvw_center_width = [ ...
blobs_uvw_boundaries(:, 4:6) + blobs_uvw_boundaries(:, 1:3), ...
blobs_uvw_boundaries(:, 4:6) - blobs_uvw_boundaries(:, 1:3) ] / 2;
% and now we scan for overlaps of blobs
is_merged = false(size(all_blobs_list));
% We will now scan for blobs which have the same ID and merge them
for ii_b = 1:num_blobs
num_chars = fprintf('%03d/%03d', ii_b, num_blobs);
ind_b_others = (ii_b+1:num_blobs)';
dist_centers_w = gtMathsCircularDistance( ...
blobs_uvw_center_width(ii_b, 3), ...
blobs_uvw_center_width(ind_b_others, 3), num_images);
dist_centers_uv = abs( bsxfun(@minus, ...
blobs_uvw_center_width(ii_b, 1:2), ...
blobs_uvw_center_width(ind_b_others, 1:2)) );
dist_centers = [dist_centers_uv, dist_centers_w];
sizes_sum = bsxfun(@plus, ...
blobs_uvw_center_width(ii_b, 4:6), ...
blobs_uvw_center_width(ind_b_others, 4:6));
Nicola Vigano
committed
sizes_sum = bsxfun(@plus, sizes_sum, conf.merge_dist_pixels);
matches = all(dist_centers < sizes_sum, 2) ...
Nicola Vigano
committed
& reshape(~is_merged(ind_b_others), [], 1);
if (any(matches))
merge_list = [ii_b; ind_b_others(matches)];
blobs_uvw_boundaries(ii_b, 1:3) = min(blobs_uvw_boundaries(merge_list, 1:3), [], 1);
blobs_uvw_boundaries(ii_b, 4:6) = max(blobs_uvw_boundaries(merge_list, 4:6), [], 1);
all_blobs_list(ii_b) = merge_blobs(all_blobs_list(merge_list), blobs_uvw_center_width(ii_b, 3), p);
blobs_etas(ii_b) = mean(blobs_etas(merge_list));
was_selected(ii_b) = any(was_selected(merge_list)) || conf.always_select_shared;
if (conf.select_raw_shared)
blob_use_raw_images(ii_b) = any(blob_use_raw_images(merge_list));
else
blob_use_raw_images(ii_b) = all(blob_use_raw_images(merge_list));
end
Nicola Vigano
committed
is_merged(ind_b_others(matches)) = true;
end
fprintf(repmat('\b', [1, num_chars]));
end
% Updating structures, to remove merged blobs
all_blobs_list(is_merged) = [];
blobs_uvw_boundaries(is_merged, :) = [];
blobs_uvw_center_width(is_merged, :) = [];
blob_use_raw_images(is_merged, :) = [];
was_selected(is_merged) = [];
blobs_etas(is_merged) = [];
num_blobs = numel(all_blobs_list);
% Fixing blobs' centers to be between 0-360
blobs_w_c = mod(blobs_uvw_center_width(:, 3), num_images);
blobs_uvw_boundaries(:, [3 6]) = gtGrainAnglesTabularFix360deg(blobs_uvw_boundaries(:, [3 6]), blobs_w_c, p);
for ii_b = 1:num_blobs
all_blobs_list(ii_b).bbwim = gtGrainAnglesTabularFix360deg(all_blobs_list(ii_b).bbwim, blobs_w_c(ii_b), p);
all_blobs_list(ii_b).mbbw = gtGrainAnglesTabularFix360deg(all_blobs_list(ii_b).mbbw, blobs_w_c(ii_b), p);
end
Nicola Vigano
committed
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
fprintf('\b\b: Done in %g seconds.\nDetermining which orientation regions project to the blobs: ', toc(c))
c = tic();
% Matching all ondet for all the regions, to include and select (if
% conf.always_select_shared is true) the ones that match
or_inc_pos = cell(num_grains, 1);
blobs_props = struct( ...
'ors', cell(num_blobs, 1), ...
'ors_ondet', cell(num_blobs, 1) );
temp_proj = gtFwdSimProjDefinition([], num_grains);
for ii_g = 1:num_grains
num_chars = fprintf('%03d/%03d', ii_g, num_grains);
ondet_bls = grs(ii_g).proj(conf.det_index).ondet;
ondet_uvw = samp_ors(ii_g).allblobs.detector(conf.det_index).uvw(ondet_bls, :);
or_inc_pos{ii_g} = zeros(size(ondet_bls));
for ii_o = 1:numel(ondet_bls)
matches_uv = between(ondet_uvw(ii_o, 1:2), blobs_uvw_boundaries(:, 1:2), blobs_uvw_boundaries(:, 4:5));
matches_w_m = between(ondet_uvw(ii_o, 3) - num_images, blobs_uvw_boundaries(:, 3), blobs_uvw_boundaries(:, 6));
matches_w_c = between(ondet_uvw(ii_o, 3), blobs_uvw_boundaries(:, 3), blobs_uvw_boundaries(:, 6));
matches_w_p = between(ondet_uvw(ii_o, 3) + num_images, blobs_uvw_boundaries(:, 3), blobs_uvw_boundaries(:, 6));
matches = [matches_uv(:, :, [1 1 1]), cat(3, matches_w_m, matches_w_c, matches_w_p)];
matches = any(all(matches, 2), 3);
if (any(matches))
ii_b = find(matches, 1);
or_inc_pos{ii_g}(ii_o) = ii_b;
blobs_props(ii_b).ors = [blobs_props(ii_b).ors, ii_g];
blobs_props(ii_b).ors_ondet = [blobs_props(ii_b).ors_ondet, ii_o];
Nicola Vigano
committed
end
end
fprintf(repmat('\b', [1, num_chars]));
end
if (conf.always_select_shared)
for ii_b = 1:num_blobs
was_selected(ii_b) = was_selected(ii_b) || (numel(blobs_props(ii_b).ors) > 1);
end
end
% Filtering vertical blobs
eta_ok = acosd(abs(cosd(blobs_etas))) > conf.min_eta;
was_selected = was_selected & eta_ok;
for ii_g = 1:num_grains
ondet_bls = grs(ii_g).proj(conf.det_index).ondet;
Nicola Vigano
committed
valid_inc_pos = or_inc_pos{ii_g} > 0;
temp_proj(ii_g).ondet = ondet_bls;
temp_proj(ii_g).included = find(valid_inc_pos);
temp_proj(ii_g).selected = was_selected(or_inc_pos{ii_g}(valid_inc_pos));
end
fprintf('\b\b: Done in %g seconds.\nShared blobs:\n', toc(c))
for ii_b = 1:num_blobs
fprintf(' - Blob %03d [u: %4d-%4d, v: %4d-%4d, w %4d-%4d], (was sel: %d, eta ok: %d, use raw: %d) orientation BBs:%s\n', ...
ii_b, blobs_uvw_boundaries(ii_b, [1 4 2 5 3 6]), was_selected(ii_b), eta_ok(ii_b), blob_use_raw_images(ii_b), sprintf(' %d', blobs_props(ii_b).ors))
Nicola Vigano
committed
end
% If conf.use_raw_images is selected, it is now time to act!
if (any(conf.use_raw_images))
fprintf('Loading raw images: ')
c = tic();
inds_raw_blobs = reshape(find(blob_use_raw_images), 1, []);
for ii_b = inds_raw_blobs
num_chars = fprintf('%03d/%03d', ii_b, num_blobs);
all_blobs_list(ii_b) = load_blob_simple(blobs_uvw_boundaries(ii_b, :), p, conf);
Nicola Vigano
committed
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
fprintf(repmat('\b', [1, num_chars]));
end
end
fprintf('\b\b: Done in %g seconds.\nDetermining UVW bounding boxes..', toc(c))
c = tic();
bls_bb_sizes = cat(1, all_blobs_list(:).bbsize);
[stackUSize, stackVSize] = get_stack_UV_size(bls_bb_sizes, blob_use_raw_images, conf);
fprintf('\b\b: Done in %g seconds.\n - Equalizing blob sizes: ', toc(c))
c = tic();
for ii_b = 1:num_blobs
num_chars = fprintf('%02d/%02d', ii_b, num_blobs);
all_blobs_list(ii_b) = equalize_blob_size(all_blobs_list(ii_b), stackUSize, stackVSize);
fprintf(repmat('\b', [1 num_chars]));
fprintf(repmat(' ', [1 num_chars]));
fprintf(repmat('\b', [1 num_chars]));
end
fprintf('\b\b: Done in %g seconds.\n', toc(c))
for ii_g = 1:num_grains
fprintf('Processing orientation-space region: %d/%d\n - Producing blobs and stacks..', ii_g, num_grains)
valid_inc_pos = or_inc_pos{ii_g} > 0;
local_included = temp_proj(ii_g).included;
temp_proj(ii_g).bl = all_blobs_list(or_inc_pos{ii_g}(valid_inc_pos));
temp_proj(ii_g).difstack = zeros(stackUSize, numel(local_included), stackVSize, 'single');
for ii_s = 1:numel(local_included)
% Essentially zero pads the spot to make it fit into ASTRA's
% diffraction stack
spot_pad = sum(temp_proj(ii_g).bl(ii_s).intm, 3);
temp_proj(ii_g).stack(:, ii_s, :) = spot_pad;
end
local_ondet = temp_proj(ii_g).ondet;
if (conf.use_raw_images(ii_g) && conf.use_parent_mask)
fprintf('\b\b: Done in %g seconds.\n - Producing blob masks..', toc(c))
c = tic();
sel = local_ondet(local_included);
proj_bl_masks = gt6DSpreadProjectVertices2Det(samp_ors(ii_g), volume_verts, sel, p, conf.det_index);
blobs = assign_masks(blobs, proj_bl_masks);
end
fprintf('\b\b: Done in %g seconds.\n', toc(c))
end
for ii_g = 1:num_grains
fprintf('Processing orientation-space region: %d/%d\n - Producing proj data-structure..', ii_g, num_grains)
c = tic();
proj = gtFwdSimProjDefinition();
proj.centerpix = gr_center_pix;
proj.bl = temp_proj(ii_g).bl;
proj.stack = temp_proj(ii_g).stack;
vol_size = bbox_size_pix;
% vol_size = bbox_size_pix + mean(bbox_size_pix) * 0.3;
proj.vol_size_x = vol_size(2);
proj.vol_size_y = vol_size(1);
proj.vol_size_z = vol_size(3);
proj.ondet = temp_proj(ii_g).ondet;
proj.included = temp_proj(ii_g).included;
proj.selected = temp_proj(ii_g).selected;
% Indices that indicate where the blobs are in the list
proj.included_pos = or_inc_pos{ii_g};
samp_ors(ii_g).proj(conf.det_index) = proj;
fprintf('\b\b: Done in %g seconds.\n', toc(c))
if (conf.check_spots && ii_g > 1)
samp_ors(ii_g).proj.selected = gtGuiGrainMontage(samp_ors(ii_g), [], true, conf.det_index);
end
end
if (conf.verbose)
f = figure();
ax = axes('parent', f);
hold(ax, 'on');
for ii_g = 1:num_grains
gt6DPlotOrientationBBox(ax, cat(1, samp_ors(ii_g).bb_ors(:).R_vector));
scatter3(ax, samp_ors(ii_g).R_vector(1), samp_ors(ii_g).R_vector(2), samp_ors(ii_g).R_vector(3), 30);
end
hold(ax, 'off');
drawnow();
end
cl = struct( ...
'samp_ors', {samp_ors}, ...
'blobs', {all_blobs_list}, ...
'blobs_props', {blobs_props}, ...
'conf', {conf} );
if (conf.save)
str_ids = sprintf('_%04d', grs_list);
grain_filename = fullfile(p.acq.dir, '4_grains', ...
sprintf('phase_%02d', phase_id), ...
sprintf('grain_cluster%s.mat', str_ids));
fprintf('Saving the cluster file "%s"..', grain_filename)
save(grain_filename, '-struct', 'cl', '-v7.3');
fprintf('\b\b: Done.\n')
fprintf('Saving to sample.mat..')
sample = GtSample.loadFromFile();
cl_info = GtPhase.makeCluster(grs_list, ...
cat(1, samp_ors(:).R_vector), ...
estim_space_bbox_pix, estim_orient_bbox_rod, 1);
sample.phases{phase_id}.setClusterInfo(grs_list, cl_info);
sample.saveToFile();
fprintf('\b\b: Done.\n')
end
end
function [u_size, v_size] = get_stack_UV_size(img_sizes, blob_use_raw_images, conf)
max_img_sizes = max(img_sizes(~blob_use_raw_images, 1:2), 1);
max_img_sizes = max([ceil(max_img_sizes * conf.stack_oversize); img_sizes(blob_use_raw_images, 1:2)], [], 1);
Nicola Vigano
committed
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
u_size = round(max_img_sizes(1));
v_size = round(max_img_sizes(2));
fprintf('\n');
fprintf(' Maximum images size: [%3d, %3d]\n', max_img_sizes);
fprintf('Stack images size (oversize: %1.2f): [%3d, %3d]\n', conf.stack_oversize, u_size, v_size);
fprintf('\n');
end
function blob = equalize_blob_size(blob, stackUSize, stackVSize)
new_bbsize = [stackUSize, stackVSize, blob.bbsize(3)];
shift = floor((new_bbsize - blob.bbsize) / 2);
new_bbuim = blob.bbuim - shift(1);
new_bbuim(2) = new_bbuim(1) + new_bbsize(1) - 1;
new_bbvim = blob.bbvim - shift(2);
new_bbvim(2) = new_bbvim(1) + new_bbsize(2) - 1;
blob.bbuim = new_bbuim;
blob.bbvim = new_bbvim;
blob.bbsize = new_bbsize;
blob.intm = gtPlaceSubVolume(zeros(new_bbsize, 'like', blob.intm), blob.intm, shift);
blob.mask = logical(gtPlaceSubVolume(zeros(new_bbsize, 'uint8'), blob.mask, shift));
end
function gr = twin_fwd_sim_to_gr(ref_gr, twin)
gr = ref_gr;
gr.R_vector = twin.R_vector;
gr.allblobs = twin.allblobs;
gr.proj = twin.proj;
gr.fwd_sim = twin.fwd_sim;
% And the useless stuff
gr.check = twin.fwd_sim(1).check;
gr.flag = twin.fwd_sim(1).flag;
gr.spotid = twin.fwd_sim(1).spotid;
% only included spots info
gr.cands = twin.fwd_sim(1).cands;
gr.likelihood = twin.fwd_sim(1).likelihood;
gr.difspotID = twin.fwd_sim(1).difspotID;
gr.uv_exp = twin.fwd_sim(1).uvw(:, 1:2);
gr.om_exp = twin.fwd_sim(1).uvw(:, 3);
gr.bb_exp = twin.fwd_sim(1).bb;
gr.intensity = twin.fwd_sim(1).intensity;
end
function blob = load_blob_simple(img_bboxes, p, conf)
blob = gtFwdSimBlobDefinition();
img_sizes = img_bboxes(1, 4:6) - img_bboxes(1, 1:3) + 1;
bb = [img_bboxes(1, 1:2), img_sizes(1, 1:2)];
blob_vol = gtGetRawRoi(img_bboxes(3), img_bboxes(6), p.acq, bb, conf.det_index);
blob_vol(blob_vol < 0) = 0;
blob_vol(isnan(blob_vol)) = 0;
blob_bb = [img_bboxes(1, 1:3), img_sizes(1, :)];
% Transposing to keep the same convention as spots
blob_vol = permute(blob_vol, [2 1 3]);
blob.mbbsize = img_sizes;
Nicola Vigano
committed
blob.mbbu = [blob_bb(1), blob_bb(1) + blob_bb(4) - 1];
blob.mbbv = [blob_bb(2), blob_bb(2) + blob_bb(5) - 1];
blob.mbbw = [blob_bb(3), blob_bb(3) + blob_bb(6) - 1];
blob.intm = single(blob_vol);
blob.mask = true(img_sizes);
blob.bbsize = img_sizes;
blob.bbuim = blob.mbbu;
blob.bbvim = blob.mbbv;
blob.bbwim = blob.mbbw;
Nicola Vigano
committed
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
blob_int = sum(blob.intm(blob.mask));
blob.intensity = blob_int;
end
function blob = merge_blobs(bls, ref_w, p)
blob = gtFwdSimBlobDefinition();
bls_bb_u = cat(1, bls(:).bbuim);
bls_bb_v = cat(1, bls(:).bbvim);
bls_bb_w = cat(1, bls(:).bbwim);
bls_bb_w = gtGrainAnglesTabularFix360deg(bls_bb_w, ref_w, p);
blob.bbuim = [min(bls_bb_u(:, 1)), max(bls_bb_u(:, 2))];
blob.bbvim = [min(bls_bb_v(:, 1)), max(bls_bb_v(:, 2))];
blob.bbwim = [min(bls_bb_w(:, 1)), max(bls_bb_w(:, 2))];
blob.bbsize = [ ...
blob.bbuim(2) - blob.bbuim(1) + 1, ...
blob.bbvim(2) - blob.bbvim(1) + 1, ...
blob.bbwim(2) - blob.bbwim(1) + 1 ];
bls_mbb_u = cat(1, bls(:).mbbu);
bls_mbb_v = cat(1, bls(:).mbbv);
bls_mbb_w = cat(1, bls(:).mbbw);
bls_mbb_w = gtGrainAnglesTabularFix360deg(bls_mbb_w, ref_w, p);
blob.mbbu = [min(bls_mbb_u(:, 1)), max(bls_mbb_u(:, 2))];
blob.mbbv = [min(bls_mbb_v(:, 1)), max(bls_mbb_v(:, 2))];
blob.mbbw = [min(bls_mbb_w(:, 1)), max(bls_mbb_w(:, 2))];
blob.mbbsize = [ ...
blob.mbbu(2) - blob.mbbu(1) + 1, ...
blob.mbbv(2) - blob.mbbv(1) + 1, ...
blob.mbbw(2) - blob.mbbw(1) + 1 ];
shifts = [ ...
bls_bb_u(:, 1) - blob.bbuim(1), ...
bls_bb_v(:, 1) - blob.bbvim(1), ...
bls_bb_w(:, 1) - blob.bbwim(1) ];
blob.intm = zeros(blob.bbsize, 'like', bls(1).intm);
blob.mask = zeros(blob.bbsize, 'like', bls(1).mask);
for ii_b = 1:numel(bls)
bl_vol = bls(ii_b).intm / gtMathsSumNDVol(bls(ii_b).intm) * bls(ii_b).intensity;
blob.intm = gtPlaceSubVolume(blob.intm, bl_vol, shifts(ii_b, :));
blob.mask = gtPlaceSubVolume(blob.mask, bls(ii_b).mask, shifts(ii_b, :));
end
blob.intensity = sum([bls(:).intensity]);
end
function blobs = assign_masks(blobs, proj_mask_blobs, sel)
if (~exist('sel', 'var') || isempty(sel))
sel = true(numel(blobs), 1);
end
blobs_u_lims = cat(1, blobs(sel).bbuim);
blobs_v_lims = cat(1, blobs(sel).bbvim);
proj_masks_u_lims = cat(1, proj_mask_blobs(sel).bbuim);
proj_masks_v_lims = cat(1, proj_mask_blobs(sel).bbvim);
shifts = [ ...
(proj_masks_u_lims(:, 1) - blobs_u_lims(:, 1)), ...
(proj_masks_v_lims(:, 1) - blobs_v_lims(:, 1)) ];
shifts(:, 3) = 0;
inds = find(sel);
for ii = 1:numel(inds)
ii_b = inds(ii);
mask = uint8(proj_mask_blobs(ii_b).mask);
mask = mask(:, :, ones(blobs(ii_b).bbsize(3), 1));
blobs(ii_b).mask = gtPlaceSubVolume( ...
zeros(blobs(ii_b).bbsize, 'uint8'), mask, shifts(ii, :) );
blobs(ii_b).intm(~blobs(ii_b).mask) = 0;
blob_int = gtMathsSumNDVol(blobs(ii_b).intm);
blobs(ii_b).intensity = blob_int;
end
end