%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FUNCTION_NAME - script_use_scribbles
%
% Syntax: script_use_scribbles
%
% Inputs:
% NONE
%
% Outputs:
% Exchanges information with java to parse scribbles and suitably use
% them
%
% Author: Adarsh Kowdle
% email: apk64@cornell.edu
% Last revision: 02-June-2009
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set type of experiment - this is what controls the mode of operation and
% options like the number of columns in the GUI
% This variable gives the user flexibity to have multiple gui options
squiggle_handle.setExperiment(0);
flag = 1;
% Fix to make sure mode button works from the start
warning off;
display_fname = fullfile(java_tmp_dir,'no_segs.jpg');
seg_maps = mat2cell(display_fname);
seg_maps = repmat(seg_maps, [1 length(im_list)]);
squiggle_handle.setSegmentations(seg_maps);
warning on;
% different scribble / segment now iterations
while 1
% Wait for the done/segment now! button to be pressed - if next group is pressed
% get next group
while squiggle_handle.getDone() == 0
if(squiggle_handle.getNextGroup() == 1)
break
end
end
% Status bar to display messages
status = {'Please wait...'};
squiggle_handle.setStatus(status);
if(squiggle_handle.getNextGroup() == 1)
break
end
% Figuring out the image which was scribbled on
% Java returns path to image which was scribbled on (but lower case)
im_path = squiggle_handle.getImagen();
[pathstr, name, ext, versn] = fileparts(char(im_path));
image_name = sprintf('%s%s', name, ext);
% sc_file_id will have the id (in the im_list) of the image which was scribbled on
sc_file_id = find((strcmp(cellstr(char(im_list(:).name)), repmat(cellstr(image_name), size(im_list))))==1);
im = imread(fullfile(im_dir, im_list(sc_file_id).name));
[im_rows, im_cols, im_depth] = size(im);
% Get the vector of squiggles from java
if flag==1
squiggle_vect = squiggle_handle.getAllSquiggles();
squiggle_handle.getSquiggle();
else
squiggle_vect = squiggle_handle.getSquiggle();
end
% Figure out the number of squiggles made
num_squiggles = squiggle_vect.size();
% Check if there is atleast 1 scribble
if (num_squiggles==0)
status = {'Need atleast one (new) scribble!'};
squiggle_handle.setStatus(status);
squiggle_handle.setDone(0)
continue;
end
% Check if there is both foreground and background on first image
% Specifically made for a segmentation framework
bg_fg_flag = zeros(num_squiggles,1);
if flag == 1 % less than 2 classes have been seen
for squig_id = 0:num_squiggles-1
num_pts = squiggle_vect.get(squig_id).size();
% Check if the current scribble is foreground or background
tmp_flag = squiggle_vect.get(squig_id).get(num_pts-1);
bg_fg_flag(squig_id+1) = double(tmp_flag(1));
end
if (length(unique(bg_fg_flag))<2)
squiggle_handle.setDone(0);
status = {'First image needs both fg and bg scribbles!'};
squiggle_handle.setStatus(status);
continue
else
flag = 0;
end
end
% Figure out the size the image (on screen)
new_size = squiggle_handle.getImageSize();
im_rows_blow = new_size(2);
im_cols_blow = new_size(1);
% Form the vector of squiggle positions with labels (and scribble mask)
% 0=Background, 1=Foreground
scribble_mask = zeros([size(im,1) size(im,2)],'uint8');
for squig_id = 0:num_squiggles-1
% Check the number of points in the scribble
num_pts = squiggle_vect.get(squig_id).size();
% Check if the current scribble is foreground or background
bg_fg_flag = squiggle_vect.get(squig_id).get(num_pts-1);
bg_fg_flag = double(bg_fg_flag(1));
% mark scribbles in scribble_mask
for pt_id = 0:num_pts-2
pt = squiggle_vect.get(squig_id).get(pt_id); % Java/0-based indexing
pt = [pt(1)*im_cols/im_cols_blow; pt(2)*im_rows/im_rows_blow]; % scaling scribbles to actual image size
pt = floor(pt) + 1; % Matlab/1-based indexing
scribble_mask(pt(2),pt(1)) = bg_fg_flag + 1; %0/1 to 1/2 back/fore
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% YOUR CODE GOES HERE
% scribble_mask at this point is a 2D matrix the same size as the image
% where the background points and forground points scribbled on would be
% labeled 1 and 2 respectively.
% Also, note that sc_file_id will have the id of the image which was
% scribbled on.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Cell arrays which have the paths to the images used in the java gui
seg_maps = {};
im_names = {};
%keyboard
alpha = 0.7;
for p_im = 1:length(im_list)
warning off;
filename_seg = fullfile(java_tmp_dir, sprintf('seg_%d.jpg',p_im));
im = imread(fullfile(im_dir, im_list(p_im).name));
im_gray = double(rgb2gray(im));
seg_mask = zeros(size(im_gray));
image_gray = cat(3, im_gray, im_gray, im_gray);
% Once you have a segmentation (0 and 1 indicating the two classes)
% the folloing will display one of the classes in cyan
seg_image_patch = cat(3, zeros(size(seg_mask)), 255*seg_mask, 255*seg_mask);
seg_image = uint8(alpha*seg_image_patch + (1-alpha)*image_gray);
% Write the resulting segmented images into files saved in the java
% tmp directory
imwrite(seg_image, filename_seg);
seg_maps = [seg_maps filename_seg];
im_file_name = fullfile(im_dir,im_list(p_im).name);
im_names = [im_names im_file_name];
% This association makes sure that irrespective of whether you
% click on the image or segmentation matlab can infer which image
% was clicked on
squiggle_handle.associate(im_file_name, filename_seg);
warning on;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% NOTE: This GUI is capable of handling more than just segmentations,
% there are provisions to have as much as 4 columns of images in the
% different modes which can be seen in the api.doc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Setup images for displaying in the gui
display_fname = fullfile(java_tmp_dir, 'pick_an_image.jpg');
squiggle_handle.setActualImages(im_names);
squiggle_handle.setSegmentations(seg_maps);
squiggle_handle.setActualLabel(sprintf('Images'));
squiggle_handle.setSegmentationLabel(sprintf('Segmentations'));
squiggle_handle.setImageSize(500, 500);
squiggle_handle.setImage(seg_maps(sc_file_id));
squiggle_handle.setDone(0);
frame_handle.setMode(1);
status = {'Done!'};
squiggle_handle.setStatus(status);
squiggle_handle.setIterNum(1);
end % End loop on multiple scribbles
warning on;