1
votes

I am trying to write a function

[offset,coffset]=findLowNhbr(map) 

that for each pixel in a map finds the eight neighbors to the pixel, and returns two matrices with both the row and column offsets to the lowest neighbor (uses the numbers -1, 0 and 1). Border pixels are given 0 offsets for both the row and column, since they do not have neighbors.

Here is what I think the general plan for this function should be:

  1. For each point, find the eight nearest neighbors.
  2. If the neighbor is lower than the point, return -1
  3. If the neighbor is at the same elevation as the point, return 0
  4. If the neighbor is higher than the point, return +1
  5. Store these offsets in two matrices.

I am at a complete loss as to where to start, so any advice or questions are welcome!

2
What happens in case of ties?Jonas
Can you give an example of a (small) map, and the desired output matrices?Rody Oldenhuis

2 Answers

1
votes

Not entirely sure what you mean, but here's something to get you well on your way:

neighbors = cell(size(map));

for ii = 2:size(map,1)-1
    for jj = 2:size(map,1)-1

        % current element
        M = map(ii,jj);

        % extract neighbors
        N = map(ii-1:ii+i, jj-1:jj+1);

        % compare values and store
        neighbors{ii,jj} = M<N - M>N;            

    end
end

This will result in a cell-array neighbors, which contains the same number of elements as map, but each entry looks something like this:

>> neighbors{2,3}
ans = 
    0  -1   1
    1   0  -1
    1   0  -1

which is the information on all neighbors of pixel (2,3).

0
votes

Edit: This is how you can add inf to all sides of the map, assuming that map_original is your original map.

map=inf(size(map_original)+2)
map(2:end-1,2:end-1) = map_original

Assuming you have padded the map with infs on all sides, here is something to get you started:

area =-1:1;
for i=2:size(map,1)-1
    for j = 2:size(map,2)-1
         bestfound=inf;
         bestk=0;
         bestl=0;
         for k = area
             for l=area
                if k~=0 && l~=0

Like i said, this will only get you started !