필터 지우기
필터 지우기

How do I save 6bands layer stacked data in ENVI format?

조회 수: 6 (최근 30일)
Devendra
Devendra 2024년 4월 24일
편집: Vinayak Choyyan 2024년 5월 13일
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

채택된 답변

Vinayak Choyyan
Vinayak Choyyan 2024년 5월 7일
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
gauri
gauri 2024년 5월 13일
편집: gauri 2024년 5월 13일
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
Vinayak Choyyan 2024년 5월 13일
편집: Vinayak Choyyan 2024년 5월 13일
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"].';
Here I am using transpose operator. I have linked the documentation.
Additionally, please go through this example to see how to use a shapefile to crop and mask certain regions of your image data.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by