1

I am new to this forum, so please bear with me. I have been working on this Matlab problem for a while now:

I have a digital elevation model (DEM) new_sub(x,y) in tif format. So it is a x-by-y matrix containing heights (z). I wish to resample parts of this DEM in different resolutions and restore this in another matrix. So far I have been working with for loops to change the resolution of different areas of the DEM and then wrote the results to an xyz-file:

x y z 1 1 123 1 2 233 1 3 231 2 1 235 2 2 531 2 3 452

and so forth. Here is the code:

xmax = size(new_sub,2);
ymax = size(new_sub,1);

for k=1:200 % y
    for l=1:xmax % x
        fprintf(fid, '%d %d %d \n',l,xmax+1-k,new_sub(k,l));
    end
end

% 1:4
for k=200/2+1:size(new_sub,1)/2
    for l=1:size(new_sub,2)/2
        fprintf(fid, '%d %d %d \n',l*2,ymax+2-k*2,new_sub(k*2,l*2));
    end
end

This does work, but seems to be rather complicated. Moreover, it does not allow me to store the resampled areas in a single matrix within Matlab.

Is there a more efficient way of resampling certain areas of a Matrix with different resolutions, writing them into a new Matrix containg all resampled areas and then writing it to a file? I was looking into repmap, but could not think of a clever way of using it!

Your help is much appreciated!

THeo

TheodorBecker
  • 249
  • 1
  • 17

2 Answers2

1

To re-sample a matrix in Matlab:

For example matrix M:

M = [1  2  3  4  5; 
     6  7  8  9  10; 
     11 12 13 14 15; 
     16 17 18 19 20; 
     21 22 23 24 25];

If we wanted to sample on every nth pixel, it is as simple as this:

m = M(1:n:end, 1:n:end)

So for n=2

m = 1  3  5
    11 13 15
    21 23 25

I suggest you read up on indexing in matlab and also on using the colon operator to create vectors in matlab

Now in order to get in the "x y z" format you mentioned, first use meshgrid to generate matrices of X and Y coordinates.

[X, Y] = meshgrid(1:n:size(M,1), 1:n:size(M,2))

notice I use n to downsample X and Y. Now you just need to flatten the three matrices and combine them:

final = [X(:), Y(:), m(:)]

Finally to save as a file I suggest you type help save or help dlmwrite in the Matlab command promt and use either of those functions to save final

Dan
  • 45,079
  • 17
  • 88
  • 157
  • Thanks a lot for this solution. I started implementing it into my code and it works very well and is way faster than the loop solution I used before. Now I just need to work on a GUI that let's the user select rectangles of different resolutions in the digital elevation model – TheodorBecker Apr 16 '13 at 12:08
  • No problem, if it worked then please mark it as accepted (the outline of the arrow of at the top left of the question) and then if you liked it you should also upvote it, but I think you might need more reputation for that :/ Good luck with the GUI and be sure to go through the links I posted! – Dan Apr 16 '13 at 12:11
  • looks like I need 15 reputation to do so! – TheodorBecker Apr 16 '13 at 12:14
0

To me the easiest way to do looks like using imresize. You can treat your elevation map as an image I. Then you can cut sections out by indexing and rescaling as follows:

I = imread('my.tiff'); % read
section = I(1:200, :); % cut the first 200 rows and all columns
sectionResized = imresize(section, [numrows numcols]) % resample
imwrite(sectionResized, 'mynew.tiff'); % save
fuyas
  • 129
  • 1
  • 9
  • Thanks for this valuable hint, but I do not intend to interpolate values, because I am exporting xyz data from Matlab to triangulate them in a second program. Maybe the word "resample" was a bit misleading. Still this is very good to know for future work! – TheodorBecker Apr 16 '13 at 12:07