-
Notifications
You must be signed in to change notification settings - Fork 10
/
binaryShape.m
64 lines (59 loc) · 2.21 KB
/
binaryShape.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function out = binaryShape(shape, varargin)
% BINARYSHAPE A simple binary image containing the chosen shape.
% Useful for simple tests and debugging.
%
% Stavros Tsogkas, <stavros.tsogkas@ecp.fr>
% Last update: January 2015
switch shape
case 'rectangle'
validArgs = {'outSize','sides','pstart','fill'};
defaultValues = {[100,100],[50,50],[25,25],false};
vmap = parseVarargin(varargin,validArgs,defaultValues);
out = rectangle(vmap('outSize'),vmap('sides'),vmap('pstart'),vmap('fill'));
case 'circle'
validArgs = {'outSize','center','radius','fill'};
defaultValues = {[100,100],[50,50],25,false};
vmap = parseVarargin(varargin,validArgs,defaultValues);
out = circle(vmap('outSize'),vmap('center'),vmap('radius'),vmap('fill'));
case 'line'
validArgs = {'outSize','pstart','pend'};
defaultValues = {[100,100],[75,25],[25,75]};
vmap = parseVarargin(varargin,validArgs,defaultValues);
out = line(vmap('outSize'),vmap('pstart'),vmap('pend'));
otherwise
error('This shape is not supported yet')
end
end
function out = rectangle(outSize,sides,startxy,fill)
out = zeros(outSize);
a = sides(1);
b = sides(2);
xstart = startxy(2);
ystart = startxy(1);
xend = xstart + b;
yend = ystart + a;
if fill
out(ystart:yend, xstart:xend) = 1;
out(ystart:yend, xstart:xend) = 1;
else
out(ystart:yend, [xstart, xend]) = 1;
out([ystart,yend], xstart:xend) = 1;
end
end
function out = circle(outSize,center,radius,fill)
out = zeros(outSize);
[x,y] = meshgrid(1:outSize(2),1:outSize(1));
if fill
out((x-center(2)).^2 + (y-center(1)).^2 <= radius^2) = 1;
else
out((x-center(2)).^2 + (y-center(1)).^2 <= (radius+1)^2 & ...
(x-center(2)).^2 + (y-center(1)).^2 >= (radius-1)^2) = 1;
out = bwmorph(out,'thin','inf');
end
out = double(out);
end
function out = line(outSize,pstart,pend)
out = zeros(outSize);
[x,y] = bresenham(pstart(2),pstart(1),pend(2),pend(1));
out(sub2ind(outSize,y,x)) = 1;
end