How to find the maximum value of 3x3 mask in 2d array

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

I would just use a naive loop. I don't think you'll find an already optimized MATLAB function that will do it. You could parallelize using PARFOR, i.e., each parallel worker can be working on one of the seed points indicated by A(i,j) in parallel.
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?
If C(2,1)>T, then why has the mask not "propagated" to include this location?
Because C(i,j)>T is only one of the conditions to be satisfied for propagation. The location must also be a neighborhood maximum.
@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
Hi everyone. I apologize for not making my question clear. What I wanted to say was that the first step in array B should be to locate the neighborhood maximum. If the value of the neighborhood maximum is above the threshold then the mask will propagate and choose that pixel as the center of the mask. If the maximum value of neighborhood is below the threshold. The propagation will stop. However, if the value is above the threshold but it is not the neighborhood maximum then the mask will not propagate in that direction.
I hope I have cleared my question now. I really appreciate all your time and efforts.
Thanks, Irtaza

Sign in to comment.

Answers (1)

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?

Asked:

on 10 Jan 2018

Commented:

on 11 Jan 2018

Community Treasure Hunt

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

Start Hunting!