-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_fourier.m
75 lines (59 loc) · 1.62 KB
/
mod_fourier.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
65
66
67
68
69
70
71
72
73
74
75
% input
% img:
% detail: < total pixels
% range: <= detail
% magnitude: >=0
% GUI_use: true: image -> boundary
% false: image -> image
function out = mod_fourier(img, detail, range, magnitude, GUI_use)
%
if size(img,3)~=1
img=rgb2gray(img);
end
if ~isa(img,'logical')
mask = imbinarize(img); % binary area image
else
mask = img;
end
[B, ~] = bwboundaries(mask);
if length(B)>1
disp ("The input image has more than one area!")
out = mask;
else
codes = bd2Fdesc(mask,detail);
Fcode = codes(1:end-1,:);
Len = size(Fcode,1);
n= range;
k= magnitude;
for i=1:n
rnd1 = (rand-0.5)*k;
rnd2 = (rand-0.5)*k;
Fcode(i,1)=Fcode(i,1)*(1+rnd1);
Fcode(i,2)=Fcode(i,2)*(1+rnd2);
end
for j=Len:-1:(Len-n+1)
rnd1 = (rand-0.5)*k;
rnd2 = (rand-0.5)*k;
Fcode(j,1)=Fcode(j,1)*(1+rnd1);
Fcode(j,2)=Fcode(j,2)*(1+rnd2);
end
Fcode = [Fcode; codes(end,:)];
try
re_img = Fdesc2bd(Fcode,size(mask));
se = strel('disk',2);
CH = imdilate(re_img,se);
CH = imfill(CH,'holes');
[B, ~] = bwboundaries(CH);
bd = B{1}; % the first boundary
if GUI_use % output boundary
out=bd;
else
% output mask
out = poly2mask(bd(:,2), bd(:,1),size(mask,1),size(mask,2));
end
catch
disp ("Unexpected error or the output image may have more than one area! Try different parameters.")
figure;imagesc(re_img);
end
end
end