From ad1870a27f5b7ccb91760af92203387d57e6faba Mon Sep 17 00:00:00 2001 From: Julian Kosciessa Date: Mon, 10 Jun 2024 13:23:58 +0200 Subject: [PATCH] [fix] pseudoCT: make tissue labels dynamic, save correct images --- functions/preprocess_brain_pCT.m | 9 ++++++-- functions/smooth_and_crop_layered_pseudoCT.m | 22 +++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/functions/preprocess_brain_pCT.m b/functions/preprocess_brain_pCT.m index a90197d..ceb5f98 100755 --- a/functions/preprocess_brain_pCT.m +++ b/functions/preprocess_brain_pCT.m @@ -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),:)); diff --git a/functions/smooth_and_crop_layered_pseudoCT.m b/functions/smooth_and_crop_layered_pseudoCT.m index 3c4cfa5..ee21937 100755 --- a/functions/smooth_and_crop_layered_pseudoCT.m +++ b/functions/smooth_and_crop_layered_pseudoCT.m @@ -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, ... @@ -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 @@ -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)