Anonymous Anonymous - 8 months ago
583 0

No description

MATLAB

Propogate Mask

function [Shifted, Masks] = PropagateMaskBoundary(Mask, FOE, Sol, ImageSize)
% Sol has [Alphax, Alphay; Betax, Betay]
% bwboundaries gives [r,c] which is [y,x]
% ImageCenter is not used as it cancels out

BoundaryList = bwboundaries(Mask);
Shifted = cell(length(BoundaryList), 1);
Masks = cell(length(BoundaryList), 1);


for count = 1:length(BoundaryList)
    Shifted{count} = round([Sol(1,1)*(BoundaryList{count}(:,2) - FOE(1)) + BoundaryList{count}(:,2),...
        Sol(1,2)*(BoundaryList{count}(:,1) - FOE(2)) + BoundaryList{count}(:,1)]);

    
    Masks{count} = zeros(ImageSize);
    % Remove pixels outside Image
    ValidIdxs = Shifted{count}(:,1) <= ImageSize(2) & Shifted{count}(:,1) >= 1 & ...
        Shifted{count}(:,2) <= ImageSize(1) & Shifted{count}(:,2) >= 1;
    
    Masks{count}(sub2ind(ImageSize, Shifted{count}(ValidIdxs,2),...
        Shifted{count}(ValidIdxs,1))) = 1;
    Masks{count} = imdilate(Masks{count}, strel('disk', 1));
    % Too close to comfort!
    if(sum(sum(Masks{count}))==0)
        Masks = [];
        return;
    end
    Masks{count} = filledgegaps(Masks{count}, 1);% Max 5 pixel tolerance
    [~, LabeledEdgeImg] = edgelink(Masks{count}, 7);% Here we discard contours less than 20 pixels long
    Masks{count} = LabeledEdgeImg > 0;
    Masks{count} = imfill(Masks{count}, 'holes');
    
end

if(length(BoundaryList) == 2)
    % For Fg
    Masks = abs(Masks{1} - Masks{2});
elseif(length(BoundaryList) == 1)
    % For Bg
    Masks = Masks{1};
else
    %     Masks = ~Masks;
    % close all
    disp('Size is 3, something is wrong');
    imshow(Masks);
    pause;
    %     error('Size is 3, something is wrong');
end

end
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download