bfs.m
代码语言:javascript复制function vis = bfs(img, visited, i, j)
vis = visited;
vis(i, j) = 1;
qi = [i];
qj = [j];
qsize = 1;
A = img;
while(qsize > 0)
i = qi(1, 1); j = qj(1, 1);
if(qsize > 1)
qi = qi(2:qsize);
qj = qj(2:qsize);
qsize = qsize - 1;
else
qi = [];
qj = [];
qsize = qsize - 1;
end
if(i-1>0 && j-1>0 && A(i-1, j-1)==1 && vis(i-1, j-1)==0)
qi(1, qsize 1) = i-1; qj(1, qsize 1) = j-1; qsize = qsize 1; vis(i-1, j-1) = 1;
end
if(j-1>0 && A(i, j-1)==1 && vis(i, j-1)==0)
qi(1, qsize 1) = i; qj(1, qsize 1) = j-1; qsize = qsize 1; vis(i, j-1) = 1;
end
if(i 1<=size(img, 1) && j-1>0 && A(i 1, j-1)==1 && vis(i 1, j-1)==0)
qi(1, qsize 1) = i 1; qj(1, qsize 1) = j-1; qsize = qsize 1; vis(i 1, j-1) = 1;
end
if(i-1>0 && A(i-1, j)==1 && vis(i, j)==0 && vis(i-1, j)==0)
qi(1, qsize 1) = i-1; qj(1, qsize 1) = j; qsize = qsize 1; vis(i-1, j) = 1;
end
if(i 1<=size(img, 1) && A(i 1, j)==1 && vis(i 1, j)==0)
qi(1, qsize 1) = i 1; qj(1, qsize 1) = j; qsize = qsize 1; vis(i 1, j-1) = 1;
end
if(i-1>0 && j 1<=size(img, 2) && A(i-1, j 1)==1 && vis(i-1, j 1)==0)
qi(1, qsize 1) = i-1; qj(1, qsize 1) = j 1; qsize = qsize 1; vis(i-1, j 1) = 1;
end
if(j 1<=size(img, 2) && A(i, j 1)==1 && vis(i, j 1)==0)
qi(1, qsize 1) = i; qj(1, qsize 1) = j 1; qsize = qsize 1; vis(i, j 1) = 1;
end
if(i 1<=size(img, 1) && j 1<=size(img, 2) && A(i 1, j 1)==1 && vis(i 1, j 1)==0)
qi(1, qsize 1) = i 1; qj(1, qsize 1) = j 1; qsize = qsize 1; vis(i 1, j 1) = 1;
end
end
end
connected_components.m
代码语言:javascript复制img = imread('text.png');
visited = zeros(size(img));
comp = 0;
for i = 1:size(img, 1)
for j = 1:size(img, 2)
if(img(i, j)==1 && visited(i, j)==0)
visited = bfs(img, visited, i, j);
comp = comp 1;
end
end
end
disp(comp);
fastfourier.m
代码语言:javascript复制%function B = fastfourier(img1, img2)
img1 = rgb2gray(imread('img1.jpg'));
img2 = rgb2gray(imread('img2.jpeg'));
img2 = imresize(img2, size(img1));
imgt1 = fft2(img1);
imgt2 = fft2(img2);
spectrum1 = abs(imgt1);
spectrum2 = abs(imgt2);
phase1 = angle(imgt1);
phase2 = angle(imgt2);
img1_using_spectrum = ifft2(spectrum1);
img1_using_phase = ifft2(exp(1i.*phase1));
img1_spectrum_img2_phase = ifft2(spectrum1 .* exp(1i.*phase2));
img2_spectrum_img1_phase = ifft2(spectrum2 .* exp(1i.*phase1));
figure;
subplot(2, 3, 1); imshow(uint8(img1));
subplot(2, 3, 2); imshow(uint8(phase1));
subplot(2, 3, 3); imshow(uint8(img1_using_spectrum));
subplot(2, 3, 4); imshow(uint8(img1_using_phase));
subplot(2, 3, 5); imshow(uint8(img1_spectrum_img2_phase));
subplot(2, 3, 6); imshow(uint8(img2_spectrum_img1_phase));
%end