How to find the maximum value of 3x3 mask in 2d array
Show older comments
I have two arrays A and B. A is a binary array whereas B has real values.
A = [0 0 0 0 0
0 1 0 0 0
0 0 0 1 0
0 0 0 0 0];
B = [0.18 0.44 0.28 0.01 0
0.16 0.33 0.17 0.02 0.01
0.10 0.16 0.05 0.02 0.03
0.02 0.01 0 0.01 0.01];
I want 3x3 mask centered at B(i,j) whenever A(i,j) = 1. The mask will search for the maximum value in the neighborhood of the center pixel. The neighbor that has the maximum value will serve as the new center point for the mask. It will continue to propagate the mask in B until all the neighbor pixels have a value less than the threshold where T = 0.05.
Following output is required for the above-mentioned dataset.
C = [0 1 0 0 0
0 1 1 0 0
0 0 0 1 0
0 0 0 0 0];
P.S The sizes of actual arrays are 100x300. But just to have better understanding i am providing 4x5 array.
Please guide me in related to this problem.
Thanks, Irtaza
6 Comments
You state that "It will continue to propagate the mask in B until all the neighbor pixels have a value less than the threshold where T = 0.05", yet in the output example:
C = [0 1 0 0 0
0 1 1 0 0
0 0 0 1 0
0 0 0 0 0];
many positions with zeros correspond to values in B > 0.05. For example C(2,1)==0, yet B(2,1)==0.16, which is still greater than T. If C(2,1)>T, then why has the mask not "propagated" to include this location?
@Matt J: does the algorithm include locations in this neighborhood that are already marked with 1?
If not, then what does "It will continue to propagate the mask in B until all the neighbor pixels have a value less than the threshold where T = 0.05" mean?
If the algorithm includes locations already marked with 1 and must be a local maximum then after locating one local maximum then the algorithm would end and the statement "it will continue..." makes no sense: the entire task could simply by written as "find the local maximum next to each 1".
Else the algorithm excludes those locations already marked with 1 in which case the algorithm can "...continue... until all the neighbor pixels have a value less than the threshold where T = 0.05."
What you proposed does not continue "until all the neighbor pixels have a value less than the threshold where T = 0.05."
My reading of the description is
Do
find local max for each 1
add locations to mask
While local unmasked pixels<T
which would be consistent with the words "It will continue ... until..."
What you understand seems to be
find local max for each 1
add locations to mask
But then there is nothing that seems to correspond to "It will continue... until...", simply a FOR loop and it would be done.
Please clarify!
I read it like this
FOR EACH A(i,j)=1
ic=i; jc=j;
WHILE B(ic,jc)>T
C(ic,jc)=1;
(ic,jc) = local max of B(ic,jc)
END
END
Syed Haider
on 11 Jan 2018
Answers (1)
Image Analyst
on 10 Jan 2018
To find the local max, you can use imdilate() in the Image Processing Toolbox.
A = [0 0 0 0 0
0 1 0 0 0
0 0 0 1 0
0 0 0 0 0];
B = [0.18 0.44 0.28 0.01 0
0.16 0.33 0.17 0.02 0.01
0.10 0.16 0.05 0.02 0.03
0.02 0.01 0 0.01 0.01];
C = imdilate(B, ones(3))
I'm also confused like the others about how the problem is phrased. If you want want to then mask it to get the local max only at the A = 1 locations and have it zero where A=0, then you can mask the local max output:
C(~logical(A)) = 0
Now you'll have the local max values only where A=1 and 0 everywhere else.
Regarding the phrase "until all the neighbor pixels have a value..." -- well, why should the neighbor values, which are of course in B, ever change?
Categories
Find more on Neighborhood and Block Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!