Azzera filtri
Azzera filtri

What is the problem with my noise removal function?

1 visualizzazione (ultimi 30 giorni)
I am just trying to make an example to go off of to clean up gaussian noise from a picture without using the built in functions. My function should be replacing all the numbers not on the edges of my 6x6 matrix but it is only replacing the the (2,2) number. What am I doing wrong?
a = [1 2 3 4 5 6; 1 2 3 5 7 6; 3 4 5 6 7 8; 4 5 6 7 8 9; 1 2 3 1 2 3; 1 4 5 6 7 8];
kernel = (1/16).*[1 2 1;2 4 2;1 2 1];
[row,col]= size(a);
ii= 1; jj= 3; mm= 1; kk= 3; tt= 2; yy= 2;
for row = a(ii:jj)
for col = a(mm:kk)
result = a(ii:jj,mm:kk).* kernel;
b= mean2(result);
a(tt,yy)= b;
mm= mm+1; kk= kk+1; tt= tt+1;
if kk> col
break;
end
end
ii= ii+1; jj= jj+1; yy= yy+1;
if jj> row
break;
end
end
  1 Commento
David Barry
David Barry il 8 Dic 2016
Why not just use built-in functions? This is the beauty and power of MATLAB.

Accedi per commentare.

Risposta accettata

David Goodmanson
David Goodmanson il 8 Dic 2016
Modificato: David Goodmanson il 8 Dic 2016
Hi Jake, You have the right idea but there need to be some changes. First of all, you have
for row = a(ii:jj)
but that would set your row indices to the values contained in the a matrix. The row indices should be directly the indices:
for row = ii:jj
Once that change is made, further on you are trying to increment an index ii that is already specified by a for loop. Take a for loop that goes something like
for q = 5:7; do stuff; q = q+3; end
The for loop is going run three times and set the value of q to 5,6, and 7. Incrementing q inside does not do anything (unless you use q further along inside the loop). You need to use something like a 'while' loop, which lets you run the loop with incremented values . The while loop can also incorporate the conditionals you have on jj and kk. Putting that all together gives
a = [1 2 3 4 5 6; 1 2 3 5 7 6; 3 4 5 6 7 8; 4 5 6 7 8 9; 1 2 3 1 2 3; 1 4 5 6 7 8];
c = a;
kernel = (1/16).*[1 2 1;2 4 2;1 2 1];
[row,col]= size(a);
ii= 1; jj= 3; yy= 2;
while jj<= row
mm= 1; kk= 3; tt= 2;
while kk<=col
result = a(ii:jj,mm:kk).* kernel;
b = sum(sum(result));
c(yy,tt)= b;
mm= mm+1; kk= kk+1; tt= tt+1;
end
ii= ii+1; jj= jj+1; yy= yy+1;
end
disp(c)
disp(conv2(a,kernel,'valid')) % compare result
Also, this code creates 'c' as a copy of 'a' and makes the changes on it, leaving 'a' alone. If you change 'a' as you go, on your next step you will start averaging in values that have already been changed from the previous step. mean2 has been replaced by a straight sum since the kernel is doing a specified weighed average already. Row and column indices in 'c' are swapped.
You are correctly incrementing ii, jj etc. When you set them up initially, ii=1, yy=2, jj=3. So yy=ii+1, jj=ii+2, but since you increment all three of them together, that's always true. You could replace yy by ii+1 and jj by ii+2 everywhere in the code and just keep and increment ii. Same with the other three indices.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by