How can I save a geographically tilted image to a geoTiff file?
8 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
LeoAiE
2023-4-23
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 个评论
LeoAiE
2023-4-24
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!