フィルターのクリア

Using pixels to find path of descent for rainwater

2 ビュー (過去 30 日間)
Johny
Johny 2013 年 11 月 5 日
回答済み: Image Analyst 2013 年 11 月 6 日
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.

回答 (2 件)

Image Analyst
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 件のコメント
Johny
Johny 2013 年 11 月 6 日
how do I make your method part of the loop? I would need to have a way of taking those offset data and placing them into two different matrices, one for row offsets and the other for column offsets.
Image Analyst
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
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.

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by