function J=roiset(J,rim)
%create ROI
%http://www.mathworks.fr/matlabcentral/newsreader/view_thread/71379
%creates a mask to remove the white boundary

height=1000; %do not change!
width=1000; %do not change!
rimthickness=rim; %thickness of boundary mask (30 is a good number)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Find core boundaries
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%find left edge boundary
for ledge=1:200;
    if sum((J(:,ledge)))>1
        break
    end
end
%find upper edge boundary
for uedge=1:200;
    if sum((J(uedge,:)))>1
        break
    end
end
%find right edge of boundary
for redge=1000:-1:800;
    if sum((J(:,redge)))>1
        break
    end
end
%find bottom edge of boundary
for bedge=1000:-1:800;
    if sum((J(bedge,:)))>1
        break
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create and Apply mask
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x_centre=ledge+round((redge-ledge)/2);
y_centre=uedge+round((bedge-uedge)/2);
radius=min([(redge-ledge)/2 (bedge-uedge)/2])-rimthickness;
binaryMask = repmat(logical(1),height,width);
[X, Y] = meshgrid(((1 : width)-x_centre).^2, ((1 :height)-y_centre).^2);
binaryMask(X + Y > radius^2) = 0;
mask=find(binaryMask==0);
J(mask)=0;

