Only mask elements of some value with conv2
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Christopher
am 6 Okt. 2014
Kommentiert: Christopher
am 7 Okt. 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.
0 Kommentare
Akzeptierte Antwort
Matt J
am 6 Okt. 2014
Bearbeitet: Matt J
am 6 Okt. 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!