%EUSIPCO 2012
%Imperial College London 
%Andriy Gelman, Jon Onativia and Pier Luigi Dragotti 

function output = extract_layer(input, label, layer)

number_of_layers = max(max(max(label)));
%label(label == -1)= number_of_layers; %Assuming final layer is the background

%Extract Signal
%label_layer = label;

if length(size(input)) == 4
   label(:,:,2,:) = squeeze(label(:,:,1,:));
   label(:,:,3,:) = squeeze(label(:,:,1,:));
end
label_layer = zeros(size(label));

if (input(1,1,1)+257)==255
    label_layer=uint8(label_layer);
end

%label_layer(label_layer ~= layer) = 0;
label_layer(label == layer) = 1;
output = input.*label_layer;

end 