I have a matrix of topographic data, it is a big one. I have replaced land data with NaN and the sea depth data are positive.(a small sample can be seen bellow)
b= [
NaN NaN NaN NaN
4 NaN NaN NaN
19 14 NaN NaN
21 18 14 NaN
24 17 NaN NaN
40 13 NaN NaN
154 26 NaN NaN
232 44 NaN NaN
500 200 100 NaN
200 100 200 NaN
NaN NaN NaN NaN
];
I want to get coastal cells. it should to be: (1,1) (2,2) (3,3) (4,4) (5,3) (6,3) (7,3) (8,3) (9,4) (10,4) (11,3) (11,2) (11,1) so that we can plot a single line contour as coastline.
I read this post MATLAB, what is the best way to trace a boarder in a matrix that is changing each step? but I could not get desired coastline using it. also I write a script as bellow but it returns a tick (2 or 3 cell) line and some breaks in it.
b(~isnan(b))=9; % sea cells will be 9
b(isnan(b))=98672; % land cells will be 98672
for i=1:255
for j=2:119
if b(i,j)==9
if ((b(i,j-1)==98672)||(b(i,j+1)==98672))
b(i,j-1)=333; % 333 will be the coastline
end
if ((b(i-1,j)==98672)||(b(i+1,j)==98672))
b(i,j-1)=333;
end
end
end
end
b(b==98672)=NaN;
b(b==9)=0.001;
pcolor(b);shading flat
the breaks taking place in the points that we have Islands or V shape in coast.
would you help me? please email me too. apt.man@gmail.com
This is a problem of morphological analysis and can be solved pretty easily by dilation operator . For efficiency, make a logical matrix and subtract the dilation
a = false (size (b));
a(~isnan (b)) = true;
coastline = imdilate (a, logical (ones (3))) - a;
This code works in Octave but in Matlab I believe you need the image processing toolbox. If you don't have it, the Octave code should work in Matlab with little adjustment. Basically, the line that does it is dilated = filter2 (se, im) > 0;
.
Experiment with different structuring elements (the second argument to 'imdilate') to have difference effects of what happens on the vertices. For example, rather than a square, try a cross, [0 1 0; 1 1 1; 0 1 0]
[0 1 0; 1 1 1; 0 1 0]
This will work too:
diff([zeros(size(b,1),1) isnan(b)], [],2) ~= 0
You will get a logical matrix, of which the true
entries are the coastal cells.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.