0

假设我有文件:

famcal.2012000000000000000000625502.stellar.blu
famcal.2012000000000000000000625502.stellar.grn
famcal.2012000000000000000000625502.stellar.red
famcal.2012000000000000000000625502.stellar.nir

famcal.2012000000000000000000625503.stellar.blu
famcal.2012000000000000000000625503.stellar.grn
famcal.2012000000000000000000625503.stellar.red
famcal.2012000000000000000000625503.stellar.nir

famcal.2012000000000000000000625504.stellar.blu
famcal.2012000000000000000000625504.stellar.grn
famcal.2012000000000000000000625504.stellar.red
famcal.2012000000000000000000625504.stellar.nir

famcal.2012000000000000000000625505.stellar.blu
famcal.2012000000000000000000625505.stellar.grn
famcal.2012000000000000000000625505.stellar.red
famcal.2012000000000000000000625505.stellar.nir

famcal.2012000000000000000000625506.stellar.blu
famcal.2012000000000000000000625506.stellar.grn
famcal.2012000000000000000000625506.stellar.red
famcal.2012000000000000000000625506.stellar.nir

famcal.2012000000000000000000625507.stellar.blu
famcal.2012000000000000000000625507.stellar.grn
famcal.2012000000000000000000625507.stellar.red
famcal.2012000000000000000000625507.stellar.nir

我希望能够按此顺序使用所有这些文件,以生成一个新矩阵。

所以本质上,一段代码可以通过以下顺序:

625502
625503
625504
625505
625506
625507

并且对于这些数字集合中的每一个,请执行以下顺序:

blu
grn
red
nir

我目前正在使用从每个文件中提取小矩阵的代码,然后将它们保存到新矩阵中的新位置。例如:

a = 2000;
star_block = repmat(a,[41,78]); %populates matrix with 2000

这将创建一个填充 2000 的矩阵。我想用上面文件的输出替换这个矩阵的某些部分。

star_block(2:10,4:12) = 625502 blu
star_block(12:20,4:12) = 625502 grn
star_block(22:30,4:12) = 625502 red
star_block(32:40,4:12) = 625502 nir

star_block(2:10,17:25) = 625503 blu
star_block(12:20,17:25) = 625503 grn
star_block(22:30,17:25) = 625503 red
star_block(32:40,17:25) = 625503 nir

star_block(2:10,30:38) = 625504 blu
star_block(12:20,30:38) = 625504 grn
star_block(22:30,30:38) = 625504 red
star_block(32:40,30:38) = 625504 nir

star_block(2:10,43:51) = 625505 blu
star_block(12:20,43:51) = 625505 grn
star_block(22:30,43:51) = 625505 red
star_block(32:40,43:51) = 625505 nir

star_block(2:10,56:64) = 625506 blu
star_block(12:20,56:64) = 625506 grn
star_block(22:30,56:64) = 625506 red
star_block(32:40,56:64) = 625506 nir

star_block(2:10,69:77) = 625507 blu
star_block(12:20,69:77) = 625507 grn
star_block(22:30,69:77) = 625507 red
star_block(32:40,69:77) = 625507 nir

编辑:

band_files = dir([star_path '/*.blu']);
for i=1:length(band_files)
    blue01 = band_files(1).name; 
    green01 = strrep(blue01, 'blu', 'grn');
    red01 = strrep(blue01, 'blu', 'red');
    nir01 = strrep(blue01, 'blu', 'nir');

    blue02 = band_files(2).name; 
    green02 = strrep(blue02, 'blu', 'grn');
    red02 = strrep(blue02, 'blu', 'red');
    nir02 = strrep(blue02, 'blu', 'nir');

    blue03 = band_files(3).name; 
    green03 = strrep(blue03, 'blu', 'grn');
    red03 = strrep(blue03, 'blu', 'red');
    nir03 = strrep(blue03, 'blu', 'nir');

    blue04 = band_files(4).name; 
    green04 = strrep(blue04, 'blu', 'grn');
    red04 = strrep(blue04, 'blu', 'red');
    nir04 = strrep(blue04, 'blu', 'nir');

    blue05 = band_files(5).name; 
    green05 = strrep(blue05, 'blu', 'grn');
    red05 = strrep(blue05, 'blu', 'red');
    nir05 = strrep(blue05, 'blu', 'nir');

    blue06 = band_files(6).name; 
    green06 = strrep(blue06, 'blu', 'grn');
    red06 = strrep(blue06, 'blu', 'red');
    nir06 = strrep(blue06, 'blu', 'nir'); 
end 

blue1 = [star_path '\' blue01];
green1 = [star_path '\' green01];
nir1 = [star_path '\' nir01];
red1 = [star_path '\' red01];

%blue
fid=fopen(blue1,'rb'); % opens the file for reading
fseek (fid, 800, -1);% Skip past header, which is 800 bytes long
blue = fread(fid, [3552,6536], '*uint16', 'ieee-be');%reads in and converts .RAS file
blue_chip = extract_region_fwd(blue);
star_block(2:10,4:12) = blue_chip;%inserts star chip into main matrix

%green
fid=fopen(green1,'rb'); % opens the file for reading
fseek (fid, 800, -1);% Skip past header, which is 800 bytes long
green = fread(fid, [3552,6536], '*uint16', 'ieee-be');%reads in and converts .RAS file
green_chip = extract_region_fwd(green);
star_block(12:20,4:12) = green_chip;%inserts star chip into main matrix

%red
fid=fopen(red1,'rb'); % opens the file for reading
fseek (fid, 800, -1);% Skip past header, which is 800 bytes long
red = fread(fid, [3552,6536], '*uint16', 'ieee-be');%reads in and converts .RAS file
red_chip = extract_region_fwd(red);
star_block(22:30,4:12) = red_chip;%inserts star chip into main matrix

%nir
fid=fopen(nir1,'rb'); % opens the file for reading
fseek (fid, 800, -1);% Skip past header, which is 800 bytes long
nir = fread(fid, [3552,6536], '*uint16', 'ieee-be');%reads in and converts .RAS file
nir_chip = extract_region_fwd(nir);
star_block(32:40,4:12) = nir_chip;%inserts star chip into main matrix
4

2 回答 2

1

您可以生成这些数字,但更可靠的方法是使用dir然后通过返回的结构访问文件:

blu_files = dir('*.blu');
for i=1:length(blu_files)
  blu_file = blu_files(i).name;
  grn_file = strrep(blu_file, 'blu', 'grn');
  red_file = strrep(blu_file, 'blu', 'red');
  nir_file = strrep(blu_file, 'blu', 'nir');

  % load files ...   
end

编辑

将相同的过程放入函数中。

根据您在问题编辑中发布的代码示例,以下功能将是有意义的:

apply_chips.m

function [] = apply_chips(blue, bx, by, green, gx, gy, red, rx, ry, nir, nx, ny)
  %insert star chips into main matrix
  star_block(bx, by) = read_block(blue );  
  star_block(gx, gy) = read_block(green);
  star_block(rx, ry) = read_block(red  );
  star_block(nx, ny) = read_block(nir  );

读块.m

function [block] = read_block(filename)
  fid = fopen(filename, 'rb');                          % opens the file for reading
  fseek(fid, 800, -1);                                  % Skip past header, which is 800 bytes long
  data = fread(fid, [3552,6536], '*uint16', 'ieee-be'); % reads in and converts .RAS file
  block = extract_region_fwd(data);

您现在应该能够:

ranges.blue.x  = { 2:10,  2:10,  2:10,  2:10,  2:10,  2:10};
ranges.blue.y  = { 4:12, 17:25, 30:38, 43:51, 56:64, 69:77};
ranges.green.x = {12:20, 12:20, 12:20, 12:20, 12:20, 12:20};
ranges.green.y = { 4:12, 17:25, 30:38, 43:51, 56:64, 69:77};
ranges.red.x   = {22:30, 22:30, 22:30, 22:30, 22:30, 22:30};
ranges.red.y   = { 4:12, 17:25, 30:38, 43:51, 56:64, 69:77};
ranges.nir.x   = {32:40, 32:40, 32:40, 32:40, 32:40, 32:40};
ranges.nir.y   = { 4:12, 17:25, 30:38, 43:51, 56:64, 69:77};

blu_files = dir('*.blu');
for i=1:length(blu_files)
  blu_file = blu_files(i).name;
  grn_file = strrep(blu_file, 'blu', 'grn');
  red_file = strrep(blu_file, 'blu', 'red');
  nir_file = strrep(blu_file, 'blu', 'nir');

  apply_chips([star_path '\' blu_file], ranges.blue.x{i} , ranges.blue.y{i} , ...
              [star_path '\' grn_file], ranges.green.x{i}, ranges.green.y{i}, ...
              [star_path '\' red_file], ranges.red.x{i}  , ranges.red.y{i}  , ...
              [star_path '\' nir_file], ranges.nir.x{i}  , ranges.nir.y{i}  , ...);

end
于 2012-08-20T19:04:36.600 回答
0

这可以帮助你

这显示了您可以使用数组做什么,但是将其转换为矩阵应该不会太难

于 2012-08-20T19:04:22.773 回答