function [img, seg, names, counts] = LM2segments(D, imagesize, HOMEIMAGES) % % Transforms all the labelme labels into segmentation masks. % It takes into account the occlusions, removing labeled pixels that are % occluded by other objects. Depth ordering is achieved using the function % LMsortlayers.m % % 'img' and 'seg' are matrices % % HOMEANNOTATIONS = 'http://labelme.csail.mit.edu/Annotations' % HOMEIMAGES = 'http://labelme.csail.mit.edu/Images' Nimages = length(D); % Create list of objects: [names, counts, imagendx, objectndx, objclass_ndx] = LMobjectnames(D, 'name'); for k = 1:length(objclass_ndx) D(imagendx(k)).annotation.object(objectndx(k)).namendx = objclass_ndx(k); end % Initalize output variables seg = zeros([imagesize(1) imagesize(2) Nimages], 'uint16'); img = zeros([imagesize(1) imagesize(2) 3 Nimages], 'uint8'); figure for ndx = 1:Nimages % Load image imgtmp = LMimread(D, ndx, HOMEIMAGES); annotation = D(ndx).annotation; if size(imgtmp,3)==1; imgtmp = repmat(imgtmp, [1 1 3]); end [nrows ncols cc] = size(imgtmp); % Scale image so that image box fits tight in image scaling = max(imagesize(1)/nrows, imagesize(2)/ncols); [annotationtmp, imgtmp] = LMimscale(annotation, imgtmp, scaling, 'bicubic'); % Crop image to final size [nr nc cc] = size(imgtmp); sr = floor((nr-imagesize(1))/2); sc = floor((nc-imagesize(2))/2); [annotationtmp, imgtmp] = LMimcrop(annotationtmp, imgtmp, [sc+1 sc+imagesize(2) sr+1 sr+imagesize(1)]); % Sort layers annotationtmp = LMsortlayers(annotationtmp, imgtmp); % Get segmentation [mask, classes] = LMobjectmask(annotationtmp, size(imgtmp)); classesndx = [annotationtmp.object.namendx]; area = squeeze(sum(sum(mask,1),2)); j = find(area>2); % remove small objects mask = mask(:,:,j); classesndx = classesndx(j); Mclasses = zeros([imagesize(1) imagesize(2)]); for k = size(mask,3):-1:1; Mclasses = Mclasses+classesndx(k)*(Mclasses==0).*mask(:,:,k); end % Store values seg(:,:,ndx) = uint16(Mclasses); img(:,:,:,ndx) = imgtmp; % Visualization subplot(121) image(imgtmp); axis('equal'); axis('tight'); title(sprintf('%d (out of %d)', ndx, Nimages)) subplot(122) image(mod(Mclasses,128)); axis('equal'); axis('tight'); drawnow end % plot stats figure subplot(121) loglog(sort(counts, 'descend')) xlabel('count rank') ylabel('Number of instances') axis('tight') pixelcounts = hist(single(seg(:)), 0:single(max(seg(:)))); unlabeled = pixelcounts(1); pixelcounts = pixelcounts(2:end); % remove unlabeled pixels; subplot(122) loglog(sort(pixelcounts, 'descend')) xlabel('area rank') ylabel('Number of pixels') axis('tight') title(sprintf('%d categories', length(names))) figure montage(reshape(uint8(mod(seg(:,:,1:20),256)), [imagesize(1) imagesize(2) 1 20]))