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

조회 수: 1 (최근 30일)
Westin Messer
Westin Messer 2018년 2월 28일
댓글: Rik 2018년 3월 2일
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