accessing surrounding elements in an array

Hi
I'm currently a complete newbie to MATLAB and I'm having trouble with a few things. I have several png files and I've counted the number of pixels that are above a certain input. I want to smooth the image/data by accessing each in the array and averaging it with its surrounding elements, excluding the boundaries of the image.
I know how to access each individual element in an array using its coordinates, and average them out using M = mean(A) - the problem I have is with accessing the neighbouring elements!
Any help would be great!

 Réponse acceptée

Sean de Wolski
Sean de Wolski le 17 Mai 2011
%Boundaries included
A2 = conv2(double(A),ones(3),'same')/9;
idx = A > 200; %simulating you're threshold
A(idx) = A2(idx);
This will yield a matrix with a windowing averaged matrix (A2) and select points in A that don't meet a criteria and replace them.
%Boundaries kept
A2 = conv2(double(A),ones(3),'valid')/9;
Apart = A(2:end-1,2:end-1);
idx = Atemp > 200; %simulating you're threshold
Atemp(idx) = A2(idx);
A(2:end-1,2:end-1) = Atemp;

3 commentaires

Gordon
Gordon le 17 Mai 2011
I tried this using a given threshold - and it issued a similar warning to the one above. I'm not really sure what I'm supposed to do with the double and single function...
Sean de Wolski
Sean de Wolski le 17 Mai 2011
Yes, you need to convert A to double first. See the first line of my answer.
Sean de Wolski
Sean de Wolski le 17 Mai 2011
Also, note: this answer is replacing border elements. It would be straightforward to change it.

Connectez-vous pour commenter.

Plus de réponses (1)

Andrei Bobrov
Andrei Bobrov le 17 Mai 2011
variant
Aout = A;
Aout(2:end-1,2:end-1) = conv2(A,ones(3),'valid')/9;
% or "8" and [1 1 1;1 0 1;1 1 1]?
MORE EDIT
2 variants
1.
Aout(2:end-1,2:end-1) = conv2(A,ones(3),'valid')/9;
2.
Aout(2:end-1,2:end-1) = conv2(A,[1 1 1;1 0 1;1 1 1],'valid')/8;

4 commentaires

Gordon
Gordon le 17 Mai 2011
im still a little at a loss to what I'm doing, but so far I've tried using your little bit of code there in an if statement (so I can process several images simultaneously) and it returned an error for my Aout variable as 'undefined.' :
for i = 1:10;
if i < 10
A = imread(sprintf(['image00' num2str(i) '.png']));
out = A;
Aout(2:end-1,2:end-1) = conv2(A,ones(3),'valid')/8;
end
end
Andrei Bobrov
Andrei Bobrov le 17 Mai 2011
typo in the line "out = A;" must be "Aout = A;"
Sean de Wolski
Sean de Wolski le 17 Mai 2011
Why don't you just run your for loop from 1:9?
for ii = 1:9
A = imread(...)
A2 = conv2(A,ones(3),'same')/9;
idx = A > 200; %simulating you're threshold
A(idx) = A2(idx);
%Do something with A now that it's smoothed
end
Gordon
Gordon le 17 Mai 2011
I've been issued the following warning:
Warning: CONV2 on values of class UINT8 is obsolete.
Use CONV2(DOUBLE(A),DOUBLE(B)) or CONV2(SINGLE(A),SINGLE(B))
instead.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by