成品展示:https://www.bilibili.com/video/BV1M5411W7w8/
成品展示:https://www.bilibili.com/video/BV16A411v7ZV/
impath = '图片地址';
msizex = 300; %更改图片大小
msizey = 300;
img = imread(impath);
grey = rgb2gray(img);
resizeGrey = imresize(grey, [msizex, msizey]);
eg = edge(resizeGrey, 'canny');
[x, y] = find(eg);
global Path G;
Path = [];
G = rot90(eg, 3) ;
XSN = struct('path', {});
for i = 1:msizex
for j = 1:msizey
if G(i, j) ~= 0
dfs(i, j);
% Path
XSN(end + 1).path = Path;
Path = [];
end
end
end
imshow(rot90(eg, 3))
imshow(eg)
XnSet = struct('Xn', {}, 'XnSum', {});
for i = 1:length(XSN)
tmp = fft(XSN(i).path);
A = exp(2i * pi .* ([0:length(tmp) - 1]' * [0:length(tmp) - 1]) ./ length(tmp)) ./ length(tmp);
for j = 1:length(A(:, 1))
A(j, :) = tmp .* A(j, :);
end
XnSet(end + 1).Xn = A;
XnSet(end).XnSum = cumsum(A, 2);
end
figure(1)
for i = 1:length(XnSet)
for j=1:1:length(XnSet(i).XnSum)
clf
hold on
plot(XnSet(i).XnSum(j, :));
if i > 1
for k = 1:i - 1
plot(XnSet(k).XnSum(:,end),'-r')
end
end
plot(XnSet(i).XnSum(1:j,end),'-r')
hold off
xlim([0,500]);ylim([0,500])%画框大小
pause(0.01)
end
end
function [] = dfs(x, y)
global Path G;
dx = [0, 0, 1, 1, 1, -1, -1, -1];
dy = [1, -1, 0, -1, 1, 0, 1, -1];
G(x, y) = 0;
Path(end + 1) = x + y * 1i;
for i = 1:8
if G(x + dx(i), y + dy(i)) ~= 0
dfs(x + dx(i), y + dy(i));
Path(end + 1) = x + y * 1i;
end
end
end