Borrar filtros
Borrar filtros

How can I save a geographically tilted image to a geoTiff file?

6 visualizaciones (últimos 30 días)
Sonoma Rich
Sonoma Rich el 3 de Abr. de 2023
Comentada: LeoAiE el 24 de Abr. de 2023
How can I save an image to a geoTiff file. I know the latitude and longitude of the the four corners of the image, but the image is not aligned north/south, east/west.

Respuestas (1)

LeoAiE
LeoAiE el 23 de Abr. de 2023
I think you can use the geotiffwrite function from the Mapping Toolbox, along with a transformation matrix that maps pixel coordinates to geographic coordinates.
% Load the image (Replace 'your_image.jpg' with your image file)
I = imread('your_image.jpg');
% Define the latitude and longitude of the four corners of the image
% (Replace the example coordinates with your image's corner coordinates)
latCorners = [37.5, 37.7, 37.4, 37.6]; % [top-left, top-right, bottom-left, bottom-right]
lonCorners = [-122.2, -122.0, -122.3, -122.1]; % [top-left, top-right, bottom-left, bottom-right]
% Calculate the transformation matrix (affine transformation)
width = size(I, 2);
height = size(I, 1);
x = [0, width, 0, width];
y = [0, 0, height, height];
A = [x', y', ones(4, 1)] \ [lonCorners', latCorners'];
% Save the image as a GeoTIFF file with the specified transformation matrix
geotiffwrite('output_geotiff.tif', I, A);
  2 comentarios
Sonoma Rich
Sonoma Rich el 24 de Abr. de 2023
It does not work. I get the following message:
Error using geotiffwrite (line 245)
The referencing matrix supplied to function GEOTIFFWRITE specifies that the associated raster is rotated or skewed with respect to the
latitude/longitude system. Function GEOTIFFWRITE does not support this geometry.
LeoAiE
LeoAiE el 24 de Abr. de 2023
Sorry about that I haven't used geotiffwrite in a while but here a code I tested on an image and it worked
% Load the image (Replace 'your_image.jpg' with your image file)
I = imread('Selective Hue 02.jpg');
% Define the latitude and longitude of the four corners of the image
% (Replace the example coordinates with your image's corner coordinates)
latCorners = [37.5, 37.7, 37.4, 37.6]; % [top-left, top-right, bottom-left, bottom-right]
lonCorners = [-122.2, -122.0, -122.3, -122.1]; % [top-left, top-right, bottom-left, bottom-right]
% Calculate the transformation matrix (affine transformation)
width = size(I, 2);
height = size(I, 1);
x = [0, width, 0, width];
y = [0, 0, height, height];
A = [x', y', ones(4, 1)] \ [lonCorners', latCorners'];
A = A';
% Save the image
output_filename = 'output_image.tif';
imwrite(I, output_filename);
% Create the world file
createWorldFile(output_filename, A);
function createWorldFile(filename, A)
%CREATEWORLDFILE Creates a world file with the specified affine transformation matrix A
worldfile = [A(1,1), A(2,1), A(1,2), A(2,2), A(1,3), A(2,3)];
% Replace the image file extension with '.tfw'
[path, name, ~] = fileparts(filename);
worldfilename = fullfile(path, [name, '.tfw']);
% Write the world file
fid = fopen(worldfilename, 'w');
fprintf(fid, '%.10f\n%.10f\n%.10f\n%.10f\n%.10f\n%.10f\n', worldfile);
fclose(fid);
end

Iniciar sesión para comentar.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by