Clear Filters
Clear Filters

How to synchronise the .tif files to the maximum number of columns and rows with matlab?

1 view (last 30 days)
Hello,
I have .tif files not synchronised (they have diffrant number of colomuns and rows) as follows:
[m x n ]
4832 x 2283
5977 x 2284
4613 x 2283
5190 x 215
Since tthe size, here dropbox link tto the data dropbox
These files have indix of lon/lat, the filling value for no data is -9999999800000
I want to make all of them with max number of rows and max number of columns [5977 x 2284]. so I want to sync the lon/lat with filling the new cells with the filling value.
Thanks so much for any help on that!!

Answers (1)

Shivam
Shivam on 1 Dec 2023
Hi Rafat,
I see that you want to synchronize the .tif files to a specific size, [5977 x 2284], and set the padding cells to a default value of -9999999800000.
You can use the following process to synchronize multiple files to the desired dimensions:
  1. Iterate through each .tif file, reading them using the 'readgeoraster' function and storing the resulting array and spatial referencing information in variables A and R, respectively.
  2. Generate a matrix of the desired size, with each cell set to -9999999800000.
  3. Calculate the position to place the original image in the center and insert it into the resized image.
  4. Adjust the 'RasterSize' of the spatial referencing object 'R' to match the desired size.
  5. Utilize the 'geotiffwrite' function to write the resized image back to the original file.
You can follow the below workaround to execute the steps:
files = dir('*.tif');
desired_size = [5977, 2284];
filling_value = -9999999800000;
for i = 1:length(files)
% Read the GeoTIFF file
[A,R] = readgeoraster(files(i).name);
% Get the size of the original image
[rows, cols] = size(A);
% Create the resized image with the desired size and the filling value
resized_image = filling_value * ones(desired_size);
% Calculate the position to place the original image in the
% middle of resized_image matrix
row_start = floor((desired_size(1) - rows) / 2) + 1;
col_start = floor((desired_size(2) - cols) / 2) + 1;
% Place the original image in the resized_image
resized_image(row_start:(row_start+rows-1), col_start:(col_start+cols-1)) = A;
% Set the RasterSize of R to desired_size
R.RasterSize = desired_size;
% Write the resized image back to the original file
geotiffwrite(files(i).name, resized_image, R);
end
Also, you can use the 'imshow' function to visualize the original image, i.e., A and the resized_image. In the case of the resized_image, it may be necessary to zoom in to observe the details due to the padding around the original image.
Please refer to the following documentation to know more about the 'readgeoraster', 'geotiffwrite' and 'imshow' functions:
I hope it helps.
Best Regards,
Shivam
  1 Comment
Rafat
Rafat on 4 Dec 2023
Dear @Shivam,
Thanks so much for the great work!!
I test it on the data to understand it
Sorry seems I cc'd wrong data, so here I correct the data to match the qustion (Dropbox)
Can you help me on point # 3, You said "place the original image in the center".
So, can we use R from the biggest image (from the data) with size [5977 x 2284], so I want to insert the original images with smaller size (<5977 and/or <2284) in place where R from them (lat/lon) fit the R from the biggest image/resized imag, which maybe not in the center.
Cheers,
rafat

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!