Skip to content

Instantly share code, notes, and snippets.

@Hio-Been
Created July 13, 2020 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hio-Been/89d55a46bcea3a15fa01b263919da535 to your computer and use it in GitHub Desktop.
Save Hio-Been/89d55a46bcea3a15fa01b263919da535 to your computer and use it in GitHub Desktop.
MATLAB function for generating binary masks (usually for CNN)
function generate_masks_manual()
% MATLAB function for generating binary masks
% Hio-Been Han, hiobeen.han@kaist.ac.kr
% 2020-07-13.
%% (1) Environment set
[img_directory] = uigetdir();
% img_directory = [pwd '/frame_copied'];
file_list = dir( [img_directory '/*.png'] );
if isempty(file_list), file_list = dir( [img_directory '/*.jpg'] );end
img_n = length(file_list);
disp(['Directory contains ' num2str(img_n) ' files ...'])
set_directory = [img_directory '_trainset/'];
if ~isdir(set_directory), mkdir(set_directory); end
[~,shuffled_order] = sortrows( rand([1,img_n])' );
%% (2) Manual segmentation start
for imgIdx = shuffled_order'
imgname = file_list(imgIdx).name; %dir([img_directory '/*' sprintf( '%06d', target_frames(imgIdx)) '*']);
loadimg = imread( [img_directory '/' imgname ]);
saveimg_fname = imgname;
savemask_fname = ['mask' imgname(6:end)];
% Check existence
exist_masks = dir([ set_directory '*' imgname ]);
if isempty(exist_masks)
ff = figure(1); clf;
% Start segmentation
subplot(1,2,1);
try
mask = image_ROI_selection(loadimg, ['imgIdx=' num2str(sprintf('%04d',imgIdx)) ', Mark ROI' ...
' [Occlusion => Right Click]']);
catch % If failed (User abort)
mask = zeros(size(loadimg));
end
% Show result
subplot(1,2,2);
hold off
imshow(uint8(loadimg));
hold on;
title('Segmentation result')
hold on;
bd= bwboundaries(mask);
if isempty(bd)
bd = {[0,0;0,0], [0,0;0,0]};
end
plot( bd{1}(:,2), bd{1}(:,1),...
'Color', 'g', 'LineWidth', 2 );
set(gca, 'FontSize', 15);
drawnow;
pause(.5);
% Save results
imwrite(loadimg, [set_directory saveimg_fname]);
imwrite(mask, [set_directory savemask_fname]);
else
disp(['Already exist::: ' imgname]);
end
end
end
%%
function mask = image_ROI_selection(I, textInput)
if nargin == 1, textInput = ''; end
figure(1), set(gcf, 'Color', [1 1 1]);
colormap(gray);
imshow(uint8(I), 'DisplayRange', [], 'InitialMagnification',600/max(size(I))*100 );
title( textInput )
disp('click points to make an initial contour,');
disp('right click to close contour and finish');
[y1 x1 b] = ginput(1);
xi = x1;
yi = y1;
if(b ~= 1) return; end
[ny nx c] = size(I);
mask = zeros(ny,nx);
while(1)
x2 = x1;
y2 = y1;
[y1 x1 b] = ginput(1);
if(b ~= 1)
x1 = xi;
y1 = yi;
end
lx = x2-x1;
ly = y2-y1;
len = ceil((lx^2+ly^2)^(1/2))+1;
x = round(x1:(lx)/(len-1):x2);
y = round(y1:(ly)/(len-1):y2); %make another one for y
if(length(x) == 0)
x = round(x1) * ones(1,len);
end
if(length(y) == 0)
y = round(y1) * ones(1,len);
end
this_index2 = sub2ind(size(mask),x, y);
if size(this_index2) == [1 1]
title('Try Again!', 'FontSize', 18)
sprintf Try_Again!
end
try mask(this_index2) = 1; end
idx = find(mask==1);
backup_mask = mask;
if(c-1)
Ir = I(:,:,1); Ig = I(:,:,2); Ib = I(:,:,3);
Ir(idx) = 0;
Ig(idx) = 255;
Ib(idx) = 0;
I(:,:,1) = Ir; I(:,:,2) = Ig; I(:,:,3) = Ib;
else
I(idx) = 255;
end
imshow(uint8(I),'DisplayRange', [], 'InitialMagnification', 600/max(size(I))*100);
title( textInput )
if(b ~= 1) break; end
end
mask = bwfill(mask, 'holes');
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment