How to use multiple variables in a for loop?

6 次查看(过去 30 天)
Hey I was wondering if we could declare multiple variables in the declaration of the for loop while declaring it
Here is an example code:
%To make a general filter design that supports any mask size
clc;
clearvars;
a=imread('pout.tif');%Reads the image
an=imnoise(a,'gaussian');% adds noise
m=input('enter mask size: ');
n=m^2; % n contains the number of elements in the mask
o=round(m/2); % this value is used to iterate through the loop and image matrix
p=o-1; % this value is also used to iterate o,p are unique to a specific mask
[row,col]=size(an);
b=zeros(row-o,col-o); % while performing filtering outer most pixels get removed from both row and column
mask=zeros(m);
for x=1:n
mask(x)=1/n; %Basically the mask will contain a average terms i.e for 3x3 mask each term will be 1/9
end
for x=o:row-o+1
for y=o:col-o+1
c=0;
for i=-p:p
for j=-p:p
c=c+an(x+i,y+i)*mask(1); %%%%%Ideally we should be iterating through each term of the mask
%%%%%%but all terms in mask are equal so I have taken a single term.
end
end
b(x-p,y-p)=c;
end
end
figure;
subplot(2,2,1);
imshow(a);
title('Orginal');
subplot(2,2,2);
imshow(an);
title('Image with gaussian noise');
subplot(2,2,3:4);
imshow(uint8(b));
title('LowPass filtered');
>The lines of code which contain multiple number of %%% consist of the highlighted part
One possible solution the achieve the above stated issue was to :
for i=-p:p %can we say for i,k=-p,1:p,m
for j=-p:p %can we say for j,l=-p,1:p,m
c=c+an(x+i,y+i)*mask(k,l);%make variables k,l which iterates through (1,1) to (5,5) of the mask in case
% of mask having size 5x5
end
end
I am not sure if something like this is allowed or possible in matlab.
Appreciate your reply.
Please ask me if I am not clear enough:)
  2 个评论
dpb
dpb 2018-8-5
mask=zeros(m);
for x=1:n
mask(x)=1/n; %Basically the mask will contain a average terms i.e for 3x3 mask each term will be 1/9
end
What the above produces is NOT what the comment says...the above is simply
mask=1./(1:n);
or the sequence of 1, 1/2, 1/3, ... 1/n. Which is really wanted; either way there's no need for a loop; the alternative per the comment would be
mask=ones(n)/n;
I didn't read the rest in detail but strikes me what you're trying to do is already done for you...see filter2 or the link therein to conv2.
For image processing there are probably other functions specifically for images that do same/similar...I don't have it so am not that familiar with its content.
SEBASTIAN CHENNATTU
Hey, thanks for the reply
In the comment I made the assumption that we were working with a mask of size 3(I forgot to mention it ).
The example was quite long so i will try and shorten it a little:
Assuming:
>variable 'an' contains an black and white image of size 'row'x'col'
>variable 'mask' is a 3x3 or 5x5 or greater odd value square matrix,the contents of each location in matrix (1,1)....(3,3) being 1/9 for a 3x3 mask and 1/25 for a 5x5 mask similarly.
>For simplicity I am assuming mask to be of size 3x3 so all elements will contain 1/9.
> According to the technique of image filtering I am trying to use , the mask need to be placed over the entire image.
>To achieve this we place the 3x3 elements of mask over the first 3x3 elements of the image matrix (by placing the mask the effective mathematical equivalent is multiplication),An addition of all the elements is taken after this the sum is place in a new matrix 'b' (here) now the mask is shifted on place to the right of the 'an' i.e(1,2)
>Once the row is done it gets shifted down by one column, effectively the new image is placed in matrix 'b'.
for x=o:row-o+1
for y=o:col-o+1
c=0;
for i=-p:p
for j=-p:p
c=c+an(x+i,y+i)*mask(1); %%%%%Ideally we should be iterating through each term of the mask
%%%%%%but all terms in mask are equal so I have taken a single term.
end
end
b(x-p,y-p)=c;
end
end
> The first two for loops iterate through the 'an' matrix ,
>The second two for loops iterate through the elements of 'an' matrix when the 3x3 mask is placed it becomes of size 3x3.
>Now if we look care fully we should also be iterating through the elements of the 'mask' itself in the second for loop i.e (1,1)of 'an' should multiply with mask(1) and (1,2) of 'an' should multiply with mask(2).
>I have simply written mask(1) because in this filter incidentally all the elements of the mask are the same.
>So my question is whether it is possible to add the iteration of the mask elements in the second for loop.
>If not how exactly can we go about implementing it.
Trust me in my head this explanation looked much shorter. I hope I could clarify my doubt.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Author Block Masks 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by