How to use custom enviwrite code to use map_info for generating envi header file?

4 views (last 30 days)
I want to use custom_enviwrite matlab code(attached) to use the map_info (attached) generated from sentinel2 image of jp2 format to produce the envi header file similar to attached header file. I request larger matlab community to suggest me how to achive this. I will appreciate your kind cooperation and help.
Devendra

Answers (1)

Suraj Kumar
Suraj Kumar on 6 Sep 2024
Hi Devendra,
Based on my understanding, you want to generate an ENVI header file using MATLAB functioncustom_ enviwrite’. To achieve this, you can go through the following steps and the attached code snippets:
1. Extract and format the 'map_info' and 'projection_info' strings from the data to match the ENVI header requirements.
% Construct the coordinate_information struct
map_info = 'UTM, 1.000, 1.000, 699960.000, 3300000.000, 20.000000, 20.000000, 43, North, WGS-84, units=Meters';
projection_info = 'PROJCS["WGS_1984_UTM_Zone_43N",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",75.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]';
2. Organize the data by creating structures for coordinate and wavelength data.
% Coordinate information structure
coordinate_information.map_info = map_info_str;
coordinate_information.projection_info = projection_info_str;
% Wavelength data
wavelength_data = [560.0, 665.0, 705.0, 740.0, 783.0, 842.0, 865.0];
3. Implement the custom_enviwrite function validating the inputs to match the expected formats and convert images to suitable formats if necessary. Then write the image to a binary file and create a ENVI header file.
function custom_enviwrite(image, fname, coordinate_information, wavelength_data)
img = single(image);
cl = class(img);
data_types = [4, 1, 2, 3, 12, 13];
t = data_types(strcmp({'single', 'int8', 'int16', 'int32', 'uint16', 'uint32'}, cl));
wfid = fopen(fname, 'w');
fwrite(wfid, img, cl);
fclose(wfid);
fid = fopen([fname '.hdr'], 'w');
fprintf(fid, 'ENVI\n');
fprintf(fid, 'description = {Exported from MATLAB}\n');
fprintf(fid, 'samples = %i\n', size(image, 2));
fprintf(fid, 'lines = %i\n', size(image, 1));
fprintf(fid, 'bands = %i\n', size(image, 3));
fprintf(fid, 'data type = %i\n', t);
fprintf(fid, 'interleave = bsq\n');
fprintf(fid, 'map info = {%s}\n', coordinate_information.map_info);
fprintf(fid, 'projection info = {%s}\n', coordinate_information.projection_info);
fprintf(fid, 'wavelength units = Nanometers\n');
fprintf(fid, 'wavelength = {\n%.2f', wavelength_data(1));
for j = 2:length(wavelength_data)
fprintf(fid, ', %.2f', wavelength_data(j));
end
fprintf(fid, '}\n');
fclose(fid);
end
4. Generate the ENVI files by executing the function to create the files.
fname = 'output_image';
custom_enviwrite(image_data, fname, coordinate_information, wavelength_data);
To know more about the strcmp’ or ’class’ functions in MATLAB, you can refer the following documentations:
Hope this helps!

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!