I'm stuck in a loop when trying to do image processing.

5 次查看(过去 30 天)
Hello, I have written this code to filter an image however the outputted image never appears, does anyone know what I am doing wrong?
clc;
clear all;
close all;
im1=imread('peppers.png');
im3=im2double(im1);
[n,m]=size(im3);
c1=0;
h = waitbar(0,'Calculating XXX please wait......');
k=1;l=1;
for l=0:1:m-1
for k=0:1:n-1
for x=0:1:n-1
for y=0:1:m-1
a=x+1;b=y+1;
c= im3(a,b) * exp(-1i*2*pi*(k*x/n + l*y/m));
c1=c1+c;
end
end
aa=l+1;bb=k+1;
im(bb,aa)=c1;
c1=0;
end
waitbar(l / m);
end
ims = im*255;
close(h)
imshow(ims);title('XXX plot');
figure
imshow(log(abs(ims)),[-1 5]); colormap(jet); colorbar;title('absolute value of XXX plot');
  2 个评论
Rik
Rik 2018-2-28
I think your loop was still working. You should really start with a small example when you have nested loops. I replaced the inner two loops with vectorized code and still it took 20 seconds to get the outermost loop to 5. See my edit and comments below.
You should re-think what you want to calculate and how you want to do it.
clc;
clear variables;%don't use clear all, there is no reason to
close all;
im1=imread('peppers.png');
im3=im2double(im1);
im3=mean(im3,3);%convert RGB to grayscale
[n,m]=size(im3);
c1=0;
h = waitbar(0,'Calculating XXX please wait......');
k=1;l=1;tic
for l=0:1:m-1
for k=0:1:n-1
if toc>20,break,end%if it takes too much time, exit the loop
[y,x]=meshgrid(0:1:m-1,0:1:n-1);
a=x+1;b=y+1;
aaa=exp(-1i*2*pi*(k*x/n + l*y/m));
c= im3.* exp(-1i*2*pi*(k*x/n + l*y/m));
c1=c1+sum(c(:));
aa=l+1;bb=k+1;
im(bb,aa)=c1;
c1=0;
end
waitbar(l / m);
end
ims = im*255;
close(h)
Rik
Rik 2018-3-2
For future people stumbling onto this post, a continued discussion and a working solution are posted here.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by