How do I save 6bands layer stacked data in ENVI format?
3 views (last 30 days)
Show older comments
Devendra
on 24 Apr 2024
Edited: Vinayak Choyyan
on 13 May 2024
I have layer stacked 6 bands of sentinel2 data which is in jp2 format after resizing green,red and nir to 20m resolution. I want to save this 6bands layer stacked data into ENVI format. I request you all to please suggest me how to do it.
I would appreciate your kind help.
Devendra
0 Comments
Accepted Answer
Vinayak Choyyan
on 7 May 2024
Hi Devendra,
Please check out the below sample code to read, layer stack, and then write Sentinel 2 images as ENVI files.
%Read 6 bands
B02=imread("R10m\tile_id_B02_10m.jp2");
B03=imread("R10m\tile_id_B03_10m.jp2");
B04=imread("R10m\tile_id_B04_10m.jp2");
B08=imread("R10m\tile_id_B08_10m.jp2");
B11=imread("R20m\tile_id_B11_20m.jp2");
B12=imread("R20m\tile_id_B12_20m.jp2");
%resize desired bands
bandSize20m=5490;
B02=imresize(B02,[bandSize20m,bandSize20m]);
B03=imresize(B03,[bandSize20m,bandSize20m]);
B04=imresize(B04,[bandSize20m,bandSize20m]);
B08=imresize(B08,[bandSize20m,bandSize20m]);
%layer stack the bands
cube=zeros(bandSize20m,bandSize20m,6,"uint16");%6 bands
cube(:,:,1)=B02;
cube(:,:,2)=B03;
cube(:,:,3)=B04;
cube(:,:,4)=B08;
cube(:,:,5)=B11;
cube(:,:,6)=B12;
%set wavelength of each band
wavelength=[490 560 665 842 1610 2190];
%create metadata struct
metadata.WavelengthUnits="nm";
%create hypercube object
hcube=hypercube(cube,wavelength,metadata);
%write as ENVI file
enviwrite(hcube,"location\filename","DataType","uint16")
In the above demo code, I have only set one metadata as an example. You can set more metadata fields by adding to the stuct named 'metadata' in above code and all of those will apprear in the .hdr file generated by enviwrite.
You can read more about parameterised hypercube object creation here and also check out enviwrite function here.
Hope this helps. Regards.
12 Comments
gauri
on 13 May 2024
Edited: gauri
on 13 May 2024
I am attaching the code and request you to please have a look on it and suggest me how to add band names also in ENVI header file.
I also want to extract the data using shape file from this seven band stacked layer file which is a seperate question on following link;
I request you to please have a look on it and help me.
Vinayak Choyyan
on 13 May 2024
Edited: Vinayak Choyyan
on 13 May 2024
At line number 41, I see that you have passed the data as a row vector like this:
metadata.BandNames= ["B03","B04","B05","B06","B07","B08","B8A"];
Please try passing it as a column vector like this as mentioned in my previous comments:
metadata.BandNames= ["B03","B04","B05","B06","B07","B08","B8A"].';
Additionally, please go through this example to see how to use a shapefile to crop and mask certain regions of your image data.
More Answers (0)
See Also
Categories
Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!