- read the image using the imread function of Matlab
- get the georefencing info using the getMetadata function which takes as input the xml metadata provided with S2 data
- use the maprasterref function to create the georeferencing R matrix
- use the geotiffwrite function to write the georeferenced s2 tif image
Question regarding file conversion from jp2 to tiff? Information lost during conversion...
8 次查看(过去 30 天)
显示 更早的评论
% Specify the folder where the files live.
myFolder = '/Users/ironmanroxx/Desktop/VeggiesDownload/Extracted_Images1';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jp2'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
[folder, baseFileNameYeet, extension] = fileparts(fullFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
%Skip
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
img = imread(fullFileName);
%imageArray = imread(fullFileName);
%imshow(imageArray); % Display image.
%drawnow; % Force display to update immediately.
imwrite(img, baseFileNameYeet + ".tiff");
end
So my code works but it takes off the coordiante information attaced to the jp2... anyone have ideas for help? I can't import the tiff into a GIS software because the coordinates are lost.... for reference, I'm working with satellite imagery
0 个评论
回答(1 个)
Iskander Benhadj
2020-6-3
Dear Isaac,
I fully understand your problem and I already faced it.
to solve it, you should use imread which reads only the image but also you need to extract your self the georeferencing from the metadata.
So here is the steps to follow:
please find attached all the used functions
and here is the overall code to run
hope this help you
img = imread(jp2_file);
info = getMetadata(xmlGranuleFile);
[x,y] = size(img);
%since we have three spatial resolution (10m, 20m and 60m) we need to make the distinction based
%on the size of the S2 image being processed
%create the R georeferencing matrix
if x == info.res_10.nrow && y == info.res_10.ncol
option.ModelPixelScaleTag = [10;10;0];
option.ModelTiepointTag = [0;0;0;info.res_10.ulx;info.res_10.uly;0];
bbox = [ info.res_10.ulx, info.res_10.uly - 10*info.res_10.nrow ; info.res_10.ulx + 10*info.res_10.ncol, info.res_10.uly ];
R = maprasterref(info.res_10.nrow,info.res_10.ncol, [info.res_10.uly - 10*info.res_10.nrow info.res_10.uly ], [info.res_10.ulx info.res_10.ulx + 10*info.res_10.ncol ]);
end
if x == info.res_20.nrow && y == info.res_20.ncol
option.ModelPixelScaleTag = [20;20;0];
option.ModelTiepointTag = [0;0;0;info.res_20.ulx;info.res_20.uly;0];
R = maprasterref(info.res_20.nrow,info.res_20.ncol, [info.res_20.uly - 20*info.res_20.nrow info.res_20.uly ], [info.res_20.ulx info.res_20.ulx + 20*info.res_20.ncol ]);
bbox = [ info.res_20.ulx, info.res_20.uly - 20*info.res_20.nrow ; info.res_20.ulx + 20*info.res_20.ncol, info.res_20.uly ];
end
if x == info.res_60.nrow && y == info.res_60.ncol
option.ModelPixelScaleTag = [60;60;0];
option.ModelTiepointTag = [0;0;0;info.res_60.ulx;info.res_60.uly;0];
bbox = [ info.res_60.ulx, info.res_60.uly - 60*info.res_60.nrow ; info.res_60.ulx + 60*info.res_60.ncol, info.res_60.uly ];
R = maprasterref(info.res_60.nrow,info.res_60.ncol, [info.res_60.uly - 60*info.res_60.nrow info.res_60.uly ], [info.res_60.ulx info.res_60.ulx + 60*info.res_60.ncol ]);
end
option.GTModelTypeGeoKey = 1;
option.ProjectedCSTypeGeoKey = info.code;
filenameOut = fullfile(outputfolder,[name,'.tif']);
geotiffwrite(filenameOut, bbox, img, -16, option);
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!