Matlab では、次の 2 つの方法でサイズを使用すると、幅と高さの値が異なるようです。
% way 1
[height, width] = size(myLoadedImage);
% way 2
height = size(myLoadedImage, 1);
width = size(myLoadedImage, 2)
この 2 つの方法が異なるのはなぜですか。
Matlab では、次の 2 つの方法でサイズを使用すると、幅と高さの値が異なるようです。
% way 1
[height, width] = size(myLoadedImage);
% way 2
height = size(myLoadedImage, 1);
width = size(myLoadedImage, 2)
この 2 つの方法が異なるのはなぜですか。
関数の完全なヘルプを参照してくださいsize
。具体的に言うと
[d1,d2,d3,...,dn] = size(X), for n > 1, returns the sizes of the dimensions of
the array X in the variables d1,d2,d3,...,dn, provided the number of output
arguments n equals ndims(X).
If n does not equal ndims(X), the following exceptions hold:
n < ndims(X) di equals the size of the ith dimension of X for 0<i<n, but dn
equals the product of the sizes of the remaining dimensions of X,
that is, dimensions n through ndims(X).
コメントで示されているように、画像は 3 次元配列です。ということで、マニュアル通り、3つのサイズのうち2つのみを で求める場合[h,w] = size(...)
、パラメータw
には2次元と3次元の積が含まれます。と を実行するh = size(..., 1)
とw = size(..., 2)
、1 番目と 2 番目の次元の正確な値が得られます。
ケースのシミュレーション:
>> im = randn(512, 143, 3);
>> h = size(im, 1)
h = 512
>> w = size(im, 2)
w = 143
>> [h, w] = size(im)
h = 512
w = 429
最後のケースに注意してくださいw = 143 * 3 = 429
。
@BasSwinckels の説明に加えて、次のように記述できます。
[h,w,~] = size(img);
これは、任意の数の次元 (グレースケール画像、RGB 画像、またはそれ以上の次元の配列) に対して機能します。