How to loop through several tiff images and reshape them

3 ビュー (過去 30 日間)
Zoe
Zoe 2017 年 9 月 26 日
編集済み: Jan 2017 年 9 月 26 日
I have 6 images which all have the same number of rows and columns (Basically 6 optical bands of one remote sensing imagery). The original size of each image is 1455*1464. I want to loop through them, then reshape each of them into a vector with 1455*1464 (=2130120) rows and 1 column. So far, I only know how to manually load images and do something to them, but don't understand loop. I appreciate any help! Thank you! The code looks like this:
for i = 1:1455
for j = 1:1464
for h = 1:6
A(1,h) = reshape(image(i,j,h), 2130120,6)
end
end
end
I do not think it is correct. Every time I run it, it gives me the error message: Error using reshape To RESHAPE the number of elements must not change.

採用された回答

Jan
Jan 2017 年 9 月 26 日
編集済み: Jan 2017 年 9 月 26 日
What is you input? Does "image" contain an [1455 x 1464 x 6] array? If so, use:
img = reshape(img, 1455*1464, 6);
to get a [2130120 x 6] matrix. You do not need loops to do this.
With loops you can omit the reshape:
A = zeros(2130120, 6); % Pre-allocate!!!
for i = 1:1455
k = 0;
for j = 1:1464
for h = 1:6
k = k + 1;
A(k, h) = img(i,j,h);
end
end
end
Or use:
k = i + (j - 1) * 1455;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by