Skip to content

Instantly share code, notes, and snippets.

@JackBurdick
Created February 7, 2017 21:52
Show Gist options
  • Save JackBurdick/8a02f4363137ff725ee3c22e2bd71547 to your computer and use it in GitHub Desktop.
Save JackBurdick/8a02f4363137ff725ee3c22e2bd71547 to your computer and use it in GitHub Desktop.
create a mask + size of structuring element
%% Initialization
main_directory = 'isbi-segmentation-dataset'; % parent directory
% dialation size
strel_size = 25;
% select one or the other.. test or train
img_type = 'test';
% img_type = 'train';
% ========================= input information
% subdirectories
img_subDir = img_type;
mask_subDir = strcat(img_type, '_masks');
img_directory_path = strcat(main_directory, '/', img_subDir);
mask_directory_path = strcat(main_directory, '/', mask_subDir);
photo_ext = '.jpg'; % image type
mask_ext = '.png'; % image type
individual_file = '*'; % set to * for all files in a folder
% get base file name
str_to_strip_from_mask = '_Segmentation';
str_to_strip_from_img = '';
% full basename + ___ for mask and img
str_extra_mask = strcat(str_to_strip_from_mask, mask_ext);
str_extra_img = strcat(str_to_strip_from_img, photo_ext);
% ========================= output information
output_subDir = strcat('dilMask_', string(strel_size), '/', img_type);
%% loop though mask dir.
mask_files = dir(mask_directory_path);
for image_file = mask_files'
% build file paths
mask_file_path = image_file.name;
base_file_name = strrep(mask_file_path, str_extra_mask, '');
img_file_path = strcat(base_file_name, str_to_strip_from_img, photo_ext);
% confirm the paths are correct
% DEBUGING: sprintf('[%s]: %s -> %s',base_file_name, mask_file_path, img_file_path)
% need to skip over the . file
if base_file_name ~= '.'
% DEBUGING: confirm individual path is correct
% sprintf('[%s]: %s -> %s',base_file_name, mask_file_path, img_file_path)
% get img and mask
img = imread(strcat(main_directory, '/', img_type, '/', img_file_path));
mask = imread(strcat(main_directory, '/', img_type, '_masks/', mask_file_path));
% 100 is an arbitrary value between 0 and 255 (the current values)
% create structuring element to dialate mask
se_sized = strel('disk',strel_size);
mask_dil = imdilate(mask, se_sized);
% create mask as logical
mask_bw = logical(mask_dil>100);
% mask_bw = logical(mask>100);
% this function was adopted from: https://www.mathworks.com/matlabcentral/answers/2646-image-segmentation
maskedRgbImage = bsxfun(@times, img, cast(mask_bw,class(img)));
% save the image here after you
% name of img to output
output_img_path = strcat(main_directory, '/', output_subDir, '/', img_file_path);
% must use a char type conversion to use imwrite in this case
imwrite(maskedRgbImage, char(output_img_path));
end
end
%% show the last image to make sure it's working correctly
figure
subplot(1,3,1)
imshow(img);
title(base_file_name)
subplot(1,3,2)
imshow(mask_dil);
title('Mask-dil')
subplot(1,3,3)
imshow(maskedRgbImage);
title('segmented')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment