image processing - How to compare a matrix element with its neighbours without using a loop in MATLAB? -
i have matrix in matlab. want check 4-connected neighbours (left, right, top, bottom) every element. if current element less of neighbours set 0 otherwise keep value. can done loop, expensive have thousands of these matrices.
you might recognize nonmaxima suppression after edge detection.
one way function nlfilter image processing toolbox, applies given function each m-by-n block of matrix:
>> = magic(6) %# sample matrix = 35 1 6 26 19 24 3 32 7 21 23 25 31 9 2 22 27 20 8 28 33 17 10 15 30 5 34 12 14 16 4 36 29 13 18 11 >> b = nlfilter(a,[3 3],@(b) b(5)*all(b(5) >= b([2 4 6 8]))) b = 35 0 0 26 0 0 0 32 0 0 0 25 31 0 0 0 27 0 0 0 0 0 0 0 30 0 34 0 0 16 0 36 0 0 18 0
the above code defines anonymous function uses linear indexing center element of 3-by-3 submatrix b(5)
, compare 4-connected neighbors b([2 4 6 8])
. value in center element multiplied logical result returned function all, 1 when center element larger of nearest neighbors , 0 otherwise.
Comments
Post a Comment