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  );

read_block.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 に答える