How to find the maximum value of 3x3 mask in 2d array
3 views (last 30 days)
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
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?
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!