Neighbor indexing
In response to my last post about connected component labeling algorithms, blog reader JanKees wanted to know how to find neighboring pixel pairs programmatically. Prompted by this question, I thought I would describe a MATLAB indexing technique that is useful for various kinds of neighbor searching applications.
To get started, let's first review linear indexing. If you use a single subscript to index into a matrix, you access elements in the matrix based on the way they are stored in memory, which is by columns. For example:
a = magic(3)
a = 8 1 6 3 5 7 4 9 2
a(3,1) is the third element in the columnwise ordering of the elements of a, so a(3) gives you that element:
a(3)
ans = 4
a(2,3) is the eighth columnwise element, so a(8) gives you that element:
a(8)
ans = 7
We call this single subscript a ''linear index''. You can use signed offsets to linear indices to get from a particular matrix element to any of its neighbors. For example, if you subtract 1 from an element's linear index, you get the location of the element just above. For a matrix with M rows, adding M to an element's linear index gives you the element just to the right. (Caveat: This technique only works for elements that aren't on the outermost rows or columns of the matrix.)
Let's apply this technique to finding the 4-connected neighborhood pairs for a simple binary image matrix.
bw = logical([ ...
0 0 0 0 0 0 0
0 1 1 0 0 0 0
0 1 1 0 0 0 0
0 0 0 1 1 1 0
0 0 0 1 1 1 0
0 0 0 0 0 0 0 ])
bw = 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0
Step 1: Given the size of the matrix, compute neighborhood index offsets.
[M,N] = size(bw); north_offset = -1; east_offset = M; south_offset = 1; west_offset = -M;
Step 2: Find the linear indices for all the nonzero matrix elements.
idx = find(bw)
idx = 8 9 14 15 22 23 28 29 34 35
Step 3: Compute the values of all the north neighbors.
north_idx = idx + north_offset; north_neighbors = bw(north_idx)
north_neighbors = 0 1 0 1 0 1 0 1 0 1
Step 4: Construct the matrix of northern neighbor pairs. Here I'll use north_neighbors as a logical index to pick out elements where north_neighbors is nonzero.
north_pairs = [idx(north_neighbors), north_idx(north_neighbors)]
north_pairs = 9 8 15 14 23 22 29 28 35 34
Step 5: Repeat steps 3 and 4 for the other directions.
east_idx = idx + east_offset; east_neighbors = bw(east_idx); east_pairs = [idx(east_neighbors), east_idx(east_neighbors)]; south_idx = idx + south_offset; south_neighbors = bw(south_idx); south_pairs = [idx(south_neighbors), south_idx(south_neighbors)]; west_idx = idx + west_offset; west_neighbors = bw(west_idx); west_pairs = [idx(west_neighbors), west_idx(west_neighbors)]; all_pairs = [north_pairs; east_pairs; south_pairs; west_pairs]
all_pairs = 9 8 15 14 23 22 29 28 35 34 8 14 9 15 22 28 23 29 28 34 29 35 8 9 14 15 22 23 28 29 34 35 14 8 15 9 28 22 29 23 34 28 35 29
Note: Generally, you need to prepad the matrix to make sure that no nonzero elements are on the boundaries. I didn't do that here because my sample matrix didn't need it. Just remember that prepadding changes the element locations and the neighbor offset values, so you'll need to compensate for that in your own code. The details will vary depending on exactly what you are doing with the neighbor values and locations.
For more information about MATLAB indexing, including linear and logical indexing, here's a MATLAB Digest article that Loren and I wrote a few years ago.
评论
要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。