Using pixels to find path of descent for rainwater
1 次查看(过去 30 天)
显示 更早的评论
So I've been given a variable called "map" which is a matrix of heights in meters. It's a 100*100 matrix. I've used
imagesc(map);axis equal;colormap gray;
to display the map as a shaded image, then I used
[r,c]=size(map) to assign r and c to the number of rows and columns.
My goal is to follow the path of steepest descent for rainwater by finding the direction to the lowest height of the 9 pixels surrounding a given pixel in the matrix of map, including the pixel itself. I need to use the row and column offsets to do this. from my limited knowledge about pixels, I'm gathering that offsets can have values of -1, 0, or 1. Also, The boundary pixels will simply be defined as 0,0 for simplicity's sake.
How do I find these offsets? Also, how can I store row and column offsets for each map point r,c to the smallest neighbor? I need to be able to write a function that returns two matrices of the same size as the original matrix so that pixel map(r,c) has pixel map(r+roffset(r,c),c+coffset(r,c))as the minimum of those 9 heights.
The first of the matrices is supposed to find, for every cell in the original matrix, the row offset of the lowest neighbor to that cell, while the second is supposed to find the column offsets of the lowest neighbor for the same cell.
The example I've been given is that if a given pixel x,y has a row offset of -1 and column offset of 1, then the lowest point neighboring that pixel would be at row x-1 and column y+1.
0 个评论
回答(2 个)
Image Analyst
2013-11-5
Well, there's the intuitive, straightforward, brute force approach with 4 nested for loops. Did you try that? It goes just like you would think to do if you explained it to me. Scan every row and every column, and for each pixel at that particular row and column, scan a 3 b y 3 window around it looking for the min value and recording its direction.
for col = 1 : columns
for row = 1 : rows
minHere = inf;
for r = -1 : 1
for c = -1 : 1
if grayImage(row+r, col+c) < minHere
direction(row, col) = .......
and so on
5 个评论
Image Analyst
2013-11-6
编辑:Image Analyst
2013-11-6
Again, let's just take the intuitive, straightforward brute force approach. How would you think you might do it? Do you think the following might work?
if r == -1 && c == -1
theDirection = 1;
elseif r == -1 && c == 0
theDirection = 2;
elseif r == -1 && c == 1
theDirection = 3;
elseif r == 0 && c == 1
theDirection = 4;
elseif r == 1 && c == 1
theDirection = 5;
elseif r == 1 && c == 0
theDirection = 6;
elseif r == 1 && c == -1
theDirection = 7;
elseif r == 0 && c == -1
theDirection = 8;
end
direction(row, col) = theDirection;
By the way, you might want look at bwdistgeodesic(): http://blogs.mathworks.com/steve/2011/11/01/exploring-shortest-paths-part-1/ or the watershed functions.
Image Analyst
2013-11-6
You can also solve this with Dynamic Programming http://en.wikipedia.org/wiki/Dynamic_programming#Checkerboard if your starting and ending points are known, or Djikstra's method if you have a starting point and just want to do a greedy depth first approach. Or you could use A* if you want to do an optimized, non-greedy approach. All of these are explained in Wikipedia.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!