Only mask elements of some value with conv2

2 visualizaciones (últimos 30 días)
Christopher
Christopher el 6 de Oct. de 2014
Comentada: Christopher el 7 de Oct. de 2014
I want to use convolution, or something like convolution, to perform sum over neighboring elements. So normally I could use
x=zeros(20,20);
r=randi(1000,size(x));
mask=[ 0 0 1 1 1 0 0
0 1 1 1 1 1 0
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
0 1 1 1 1 1 0
0 0 1 1 1 0 0];
myconv=conv2(r,mask);
However, I only want the mask to add elements with the same value as the reference element (center of the mask). How can this be done? Thanks!
Note: this would be easy if there were only a few possible values, but I want it to work for many possible values in the matrix r.

Respuesta aceptada

Matt J
Matt J el 6 de Oct. de 2014
Editada: Matt J el 6 de Oct. de 2014
How about this?
[N,N]=size(mask);
vals=im2col(r,[N,N],'sliding');
ref=vals((N^2+1)/2, :); %assumes odd-dimensions of mask
result=mask(:).' * (vals.*bsxfun(@eq,vals, ref ));
result=reshape(result,size(r)-N+1);
It's a modification of conv2(...'valid'). If you wanted conv2(...,'full'), you would simply have to pre-pad r with zeros.

Más respuestas (0)

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by