Skip to content

Commit

Permalink
[fix] pseudoCT: make tissue labels dynamic, save correct images
Browse files Browse the repository at this point in the history
  • Loading branch information
jkosciessa committed Jun 10, 2024
1 parent 1163742 commit ad1870a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
9 changes: 7 additions & 2 deletions functions/preprocess_brain_pCT.m
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,16 @@

%% Plot the skin & skull from SimNIBS

labels = fieldnames(parameters.layer_labels);

skullidx = find(strcmp(labels, 'skull_cortical') | strcmp(labels, 'skull_trabecular'));

% unsmoothed skull & skin masks
% create filled bone mask
skull_mask_unsmoothed = zeros(size(mask_img_rr));
skull_mask_unsmoothed(mask_img_rr==4|mask_img_rr==5) = mask_img_rr(mask_img_rr==4|mask_img_rr==5);
skin_mask_unsmoothed = mask_img_rr==3;
skull_mask_unsmoothed(ismember(mask_img_rr,skullidx)) = ...
mask_img_rr(ismember(mask_img_rr,skullidx));
skin_mask_unsmoothed = mask_img_rr==find(strcmp(labels, 'skin'));

skin_slice = squeeze(skin_mask_unsmoothed(:,trans_pos_upsampled_grid(2),:));
skull_slice = squeeze(skull_mask_unsmoothed(:,trans_pos_upsampled_grid(2),:));
Expand Down
22 changes: 14 additions & 8 deletions functions/smooth_and_crop_layered_pseudoCT.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

labels = fieldnames(parameters.layer_labels);

csf_mask = segmented_img==6; % make this dynamic? why is this not included in layer_labels
if any(strcmp(labels, 'csf'))
csf_mask = segmented_img==find(strcmp(labels, 'csf'));
else
csf_mask = segmented_img==6;
end

% Creates a transducer for overlay in a figure
transducer_bowl = transducer_setup(parameters.transducer, trans_pos_upsampled_grid, focus_pos_upsampled_grid, ...
Expand Down Expand Up @@ -70,9 +74,9 @@

% Crops the processed segmented image
tissues_mask_img_cropped = segmented_img(min_dims(1):max_dims(1), min_dims(2):max_dims(2), min_dims(3):max_dims(3)); % Crop image
% Creates a mask around the edge of the figure to define the edge of the skull

skull_i = (tissues_mask_img_cropped == 4) | (tissues_mask_img_cropped == 5);
% Creates a mask around the edge of the figure to define the edge of the skull
skull_i = ismember(tissues_mask_img_cropped, find(strcmp(labels, 'skull_cortical') | strcmp(labels, 'skull_trabecular')));
skull_edge = edge3(tissues_mask_img_cropped==skull_i, 'approxcanny',0.1);

% Crops the original pseudoCT image in the same wave as the processed tissues mask one
Expand All @@ -99,18 +103,20 @@
plot_t1_with_transducer(segmented_img, voxel_size_mm, trans_pos_upsampled_grid, focus_pos_upsampled_grid, parameters),...
'montage')
output_plot = fullfile(parameters.output_dir,sprintf('sub-%03d_%s_skull_final_pCT%s.png', parameters.subject_id, parameters.simulation_medium, parameters.results_filename_affix));
title('Cropped (left) and original (right) segmented mask')
title('Cropped (left) and original (right) tissue mask')
export_fig(output_plot, '-native')

skull_mask_file = fullfile(parameters.output_dir, sprintf('sub-%03d_skull_mask_final_pCT', parameters.subject_id));
skull_mask = zeros(size(segmented_img));
skull_mask = zeros(size(segmented_image_cropped));
for layer_i = find(contains(labels, 'skull'))'
i = segmented_img==layer_i; % which voxels contain skull
skull_mask(i) = segmented_img(i);
i = segmented_image_cropped==layer_i; % which voxels contain skull
skull_mask(i) = segmented_image_cropped(i);
end
niftiwrite(uint8(skull_mask), skull_mask_file ,'Compressed', 1);

segmented_file = fullfile(parameters.output_dir, sprintf('sub-%03d_medium_masks_final_pCT', parameters.subject_id));
niftiwrite(uint8(segmented_img), segmented_file ,'Compressed', 1);
niftiwrite(uint8(tissues_mask_img_cropped), segmented_file ,'Compressed', 1);

% niftiwrite creates a file with the name segment_file (so
% sub-009_medium_masks_final in the specified directory) using the
% volume V uint8(smoothed_segmented_img)
Expand Down

0 comments on commit ad1870a

Please sign in to comment.