How to define variable names for resampling?

I am using following code to resample the 10m green,red and nir bands to 20m resolution;
% Resample the 10m bands to 20m
if ismember(files(i + j).name, {'*_B03_10m.jp2', '*_B04_10m.jp2', '*_B08_10m.jp2'})
img = imresize(img, 0.5, 'Method', 'bilinear');
end
But this part of code is being not executed.
The names of band files are as follows
T43RGN_20210119T053141_B03_10m
T43RGN_20210119T053141_B04_10m
T43RGN_20210119T053141_B05_20m
T43RGN_20210119T053141_B06_20m
T43RGN_20210119T053141_B07_20m
T43RGN_20210119T053141_B08_10m
I request you to kindly suggest me how to execute above mentioned part of code also.
Deva

 Respuesta aceptada

Stephen23
Stephen23 el 20 de Abr. de 2024
Editada: Stephen23 el 20 de Abr. de 2024
You should:
  • replace the ISMEMBER with ENDSWITH
  • get rid of those asterisks
For example:
endsWith(files(i+j).name, {'_B03_10m.jp2', '_B04_10m.jp2', '_B08_10m.jp2'})
or even simply:
endsWith(files(i+j).name, '_10m.jp2')
You might also like to use the IgnoreCase option.
NOTE: your code does not work because ISMEMBER matches text literally, so what you incorrectly think are wildcard characters that match anything are in fact literal asterisk characters that match... the asterisk character. The ISMEMBER docunentation does not mention wildcard or special characters anywhere: inventing features which do not exist is unlikely to work.

6 comentarios

Devendra
Devendra el 20 de Abr. de 2024
Thank you very much for your kind suggestions. Code is giving some error as follows
ans =
logical
1
T43RGN_20210119T053141_B03_10m.jp2
ans =
logical
1
T43RGN_20210119T053141_B04_10m.jp2
ans =
logical
0
Unable to perform assignment because the size of the left side is 5490-by-5490 and the size of the right side is
2745-by-2745.
It is also resampling already 20m resolution bands.
Please suggest me how to avoid 20m resolution bands and resample only 10m resolution bands.
Devendra
Stephen23
Stephen23 el 20 de Abr. de 2024
Editada: Stephen23 el 20 de Abr. de 2024
"Please suggest me how to avoid 20m resolution bands and resample only 10m resolution bands."
You use IF, just like you showed in your original question. Together with ENDSWITH, not with ISMEMBER.
For some reason you seem to be displaying the ENDSWITH output in the command window .... without using it for anything. I did not write that you should get rid of the IF, I wrote that you should replace ISMEMBER with ENDSWITH:
if endsWith(files(i+j).name, '_10m.jp2')
img = imresize(img, 0.5, 'Method', 'bilinear');
end
In any case, rather than resizing images based on the filenames, you should probably resize the images based on the images sizes. That would likely be a much more reliable approach.
Devendra
Devendra el 20 de Abr. de 2024
Editada: Walter Roberson el 20 de Abr. de 2024
I am very sorry for my mistake. It worked well. Thanks a lot for your kind help. However, when I am writing this layer stacked data into envi file using enviwrite, it is giving following error. If you could please suggest me how to use it properly, it will be great help to me. the part of code is as follows;
% Stack the image
stacked_image(:,:,j+1) = img;
end
fn = {files(i + j).name};
disp(string(fn));
end
disp(size(stacked_image));
img_names = regexp(fn,'\d+','match','once');
img_names = cellstr(reshape(img_names,1,[]));
fname = string(img_names(i)) + '_' + '6bands' + '.dat'
disp(fname);
enviwrite(stacked_image,fname);
it is giving following information
'C:\working_bpt\bands\'
T43RGN_20210119T053141_B03_10m.jp2
T43RGN_20210119T053141_B04_10m.jp2
T43RGN_20210119T053141_B05_20m.jp2
T43RGN_20210119T053141_B06_20m.jp2
T43RGN_20210119T053141_B07_20m.jp2
T43RGN_20210119T053141_B08_10m.jp2
5490 5490 6
fname =
"43_6bands.dat"
43_6bands.dat
Incorrect number or types of inputs or outputs for function enviwrite.
Error in ds_layer_stack (line 35)
enviwrite(stacked_image,fname);
Looking forward for your kind suggestions to write it in envi format.
Devendra
Devendra
Devendra el 20 de Abr. de 2024
As you have suggested
"In any case, rather than resizing images based on the filenames, you should probably resize the images based on the images sizes. That would likely be a much more reliable approach".
May I request you to please suggest me a starting point to resample based size rather than filenames?
I would appreciate your kind support and help.
Devendra
Walter Roberson
Walter Roberson el 20 de Abr. de 2024
As discussed in https://www.mathworks.com/matlabcentral/answers/2109356-how-to-use-enviwrite-in-matlab enviwrite() expects a hypercube object. A version of enviwrite() that expects a cuboid of data is at https://github.com/akayasse/ENVI-read-and-write-for-MATLAB
Devendra
Devendra el 20 de Abr. de 2024
Thanks for your help. I have written envi file using enviwrite function. However, map information (latitude and longitude) with projection are missing. Please suggest me how to incorporate map information into envi header file. I will appreciate your kind help.
Devendra

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2024a

Preguntada:

el 20 de Abr. de 2024

Comentada:

el 20 de Abr. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by