|
|
function [img, seg, names, counts] = LM2segments(D, imagesize, HOMEIMAGES, HOMELMSEGMENTS) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if nargin == 2 && nargout==1 |
|
|
|
|
|
|
|
|
|
|
|
imgtmp = zeros(imagesize); |
|
|
annotationtmp = LMsortlayers(D, imgtmp); |
|
|
|
|
|
|
|
|
[S_instances, classes] = LMobjectmask(annotationtmp, size(imgtmp)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Mclasses = zeros([imagesize(1) imagesize(2)]); |
|
|
for k = size(S_instances,3):-1:1; |
|
|
S_instances(:,:,k) = k*S_instances(:,:,k); |
|
|
Mclasses = Mclasses+(Mclasses==0).*S_instances(:,:,k); |
|
|
end |
|
|
img = uint16(Mclasses); |
|
|
|
|
|
return |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
if nargin==4 |
|
|
precomputed = 1; |
|
|
|
|
|
if length(D) == 1 |
|
|
fileseg = fullfile(HOMELMSEGMENTS, D(1).annotation.folder, [D(1).annotation.filename(1:end-4) '.mat']); |
|
|
|
|
|
if exist(fileseg, 'file') |
|
|
load(fileseg) |
|
|
seg = S; |
|
|
img = []; |
|
|
return |
|
|
end |
|
|
end |
|
|
else |
|
|
precomputed = 0; |
|
|
HOMELMSEGMENTS = ''; |
|
|
end |
|
|
|
|
|
Nimages = length(D); |
|
|
|
|
|
|
|
|
if ~isfield(D(1).annotation.object, 'namendx') |
|
|
[D, names, counts] = LMcreateObjectIndexField(D); |
|
|
else |
|
|
[names, counts, imagendx, objectndx, objclass_ndx] = LMobjectnames(D, 'name'); |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Nobjectclasses = length(names); |
|
|
|
|
|
if nargout>0 |
|
|
if Nimages > 1 |
|
|
|
|
|
seg = zeros([imagesize(1) imagesize(2) Nimages], 'uint16'); |
|
|
img = zeros([imagesize(1) imagesize(2) 3 Nimages], 'uint8'); |
|
|
end |
|
|
end |
|
|
|
|
|
if Nimages > 1 |
|
|
figure |
|
|
end |
|
|
|
|
|
for ndx = 1:Nimages |
|
|
|
|
|
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); |
|
|
|
|
|
|
|
|
if ~isempty(imagesize) |
|
|
scaling = max(imagesize(1)/nrows, imagesize(2)/ncols); |
|
|
[annotationtmp, imgtmp] = LMimscale(annotation, imgtmp, scaling, 'bicubic'); |
|
|
|
|
|
|
|
|
[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)]); |
|
|
|
|
|
Mclasses = zeros([imagesize(1) imagesize(2)]); |
|
|
else |
|
|
annotationtmp = annotation; |
|
|
|
|
|
Mclasses = zeros([size(imgtmp,1) size(imgtmp,2)]); |
|
|
end |
|
|
|
|
|
if isfield(annotationtmp, 'object') |
|
|
|
|
|
annotationtmp = LMsortlayers(annotationtmp, imgtmp); |
|
|
|
|
|
|
|
|
[S_instances, classes] = LMobjectmask(annotationtmp, size(imgtmp)); |
|
|
classesndx = [annotationtmp.object.namendx]; |
|
|
area = squeeze(sum(sum(S_instances,1),2)); |
|
|
|
|
|
j = find(area>2); |
|
|
S_instances = S_instances(:,:,j); |
|
|
classesndx = classesndx(j); |
|
|
|
|
|
|
|
|
for k = size(S_instances,3):-1:1; |
|
|
S_instances(:,:,k) = classesndx(k)*S_instances(:,:,k); |
|
|
Mclasses = Mclasses+(Mclasses==0).*S_instances(:,:,k); |
|
|
end |
|
|
end |
|
|
|
|
|
if nargout>0 |
|
|
|
|
|
seg(:,:,ndx) = uint16(Mclasses); |
|
|
img(:,:,:,ndx) = imgtmp; |
|
|
end |
|
|
|
|
|
|
|
|
if precomputed |
|
|
I = imgtmp; |
|
|
S = uint16(Mclasses); |
|
|
S_instances = uint16(S_instances); |
|
|
mkdir(fullfile(HOMELMSEGMENTS, D(ndx).annotation.folder)) |
|
|
fileseg = fullfile(HOMELMSEGMENTS, D(ndx).annotation.folder, [D(ndx).annotation.filename(1:end-4) '.mat']); |
|
|
if ~isempty(imagesize) |
|
|
save (fileseg, 'I', 'S', 'names', 'S_instances') |
|
|
else |
|
|
save (fileseg, 'S', 'names', 'S_instances') |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
if Nimages > 1 |
|
|
subplot(121) |
|
|
image(imgtmp); axis('equal'); axis('tight'); |
|
|
title(sprintf('%d (out of %d)', ndx, Nimages)) |
|
|
subplot(122) |
|
|
image(mod(Mclasses+1,256)); axis('equal'); axis('tight'); |
|
|
colormap([0 0 0; hsv(min(Nobjectclasses+1,256))]) |
|
|
drawnow |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|