Function output wrong size

3 vues (au cours des 30 derniers jours)
Tom Edwards
Tom Edwards le 26 Août 2020
Commenté : Tom Edwards le 26 Août 2020
I have to write a function that blurs an image file.
The idea is to take an input 'img', then using the value of each individual pixel, take the mean of a submatrix with that pixel as the centre of 2*w+1 sub matrix and use that value to replace the pixels in the same position in a generated new 'blurred' image. I hope that makes sense.
This is what I've written so far which appears to work but is apparently outputting a 1x9 vector with the wrong result and I can't figure out why or how to fix it. Any help appreciated.
function output = blur(img,w)
[row, col] = size(img);
img = double(img);
temp_img = zeros(row,col, 'uint8');
for i = 1:row
for j = 1:col
r1 = max(1,i-w);
r2 = min(row,i+w);
c1 = max(1,j-w);
c2 = min(col,j+w);
temp_matrix = img(r1:r2,c1:c2);
val_2 = mean(temp_matrix(:));
temp_img(r1:r2,c1:c2) = val_2;
end
end
output = uint8(temp_img);
end

Réponse acceptée

Rik
Rik le 26 Août 2020
I don't understand why you would store the result val_2 in multiple positions, instead of only in i,j.
Another thing I don't understand is why you don't use convn (or conv2 or imfilter) to do this. Those will flip the kernel, but since your kernel should be flat (and therefore symetrical), that doesn't matter.
  1 commentaire
Tom Edwards
Tom Edwards le 26 Août 2020
Actually, you know what, me either, I think I misunderstood the assignment and thought that we were supposed to use the mean value for the whole submatrix. Either way I just corrected it and it works! Thanks Rik.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Images dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by