function [R, control_ind] = binim2layers(R1, control_ind, control_pts)
R=logical(zeros(size(R1,1), size(R1,2)));
T=logical(zeros(size(R1,1), size(R1,2)));
R(:,:,1) = R1;
ind = [];
for i=1:length(control_ind)
    x = control_pts(control_ind{i},1);
    y = control_pts(control_ind{i},2);
    r = roipoly(T,x,y);
    r = r&R1;
    if any(r(:))
        R(:,:,end+1) = r;
    else
        ind = [ind; i];
    end
end
control_ind(ind) = [];
return;
for i=1:reg_x
    for j=1:reg_y
        r=logical(zeros([size(R1,1) size(R1,2)]));
        r(yr(1)+dy*(j-1)+(1:dy),xr(1)+dx*(i-1)+(1:dx))=logical(ones(dy,dx));
        r=r&R1;
        if any(r(:))
            layer=layer+1;
            R(:,:,end+1) = r;
            %corners{end+1} = [ xr(1)+dx*(i-1)+1, yr(1)+dy*(j-1)+1; xr(1)+dx*(i-1)+1, yr(1)+dy*(j-1)+dy; xr(1)+dx*(i-1)+dx, yr(1)+dy*(j-1)+ 1; xr(1)+dx*(i-1)+dx, yr(1)+dy*(j-1)+ dy];
            corners{end+1} = [yr(1)+dy*(j-1)+1; yr(1)+dy*(j-1)+ dy; xr(1)+dx*(i-1)+1; xr(1)+dx*(i-1)+dx];
            R1=xor(r,R1);
        end
    end
end
return;