About size function -Syntax : [m,n] = size(X)

조회 수: 16 (최근 30일)
blackwell41
blackwell41 2016년 7월 26일
편집: Stephen23 2016년 7월 26일
Hello, I wrote this code, but what does the number 1500 means?
i searched about 'size'through 'help' and they say '[m,n] = size(x)' returns the size of matrix X. But the size of the image I used is 300(height)*500(length).
Why the first l is 1500?
Thanks.
  댓글 수: 1
Star Strider
Star Strider 2016년 7월 26일
Images are 3D.
[h,l,d] = size(c)
will give the correct result.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2016년 7월 26일
편집: Stephen23 2016년 7월 26일
This often confuses beginners who do not bother to read the documentation.
Explanation
The important thing is that the last output variable is the product of all remaining dimensions, as the size documentation clearly states: "...but dn equals the product of the sizes of the remaining dimensions of X, that is, dimensions n through ndims(X)."
Beginners also forget that an image is (usually) not a 2D matrix, but is a 3D array. So you have a 3D array (RGB image), and are returning only two outputs... the docs clearly explain that the second output will be the product of dims two and three: rows*3 == 500*3 == 1500 (because the third dimension encodes RGB, so has size 3).
Solutions
1. One solution is to use three outputs (for a 3D array):
[rows,cols,pages] = size(...)
Where pages corresponds to the Red, Green, and Blue "pages" (or layers) of the image.
2. Use just one output:
>> size(ones(2,3,4))
ans =
2 3 4
3. Specify the dimension being measured:
>> R = size(ones(2,3,4),1)
R = 2
>> C = size(ones(2,3,4),2)
C = 3
Further Reading
This topic has been on this forum and in the MATLAB blogs:
You can test it too:
>> [R,Z] = size(ones(2,3,4)) % Z is product of dims two and three
R = 2
Z = 12
>> [R,C,P] = size(ones(2,3,4)) % each dim is returned separately.
R = 2
C = 3
P = 4
The moral of the story: read the documentation.
  댓글 수: 1
Guillaume
Guillaume 2016년 7월 26일
To add to Stephen's answer about images. Images in matlab can be of several types.
  • Binary (black and white) and greyscale images are stored as 2D matrices (row x column storing pixel intensity).
  • Indexed images are also stored as 2D matrices but have an associated colour map (row x column storing index into colour map).
  • Finally, RGB images are stored as 3D matrices (row x column x colour plane) where each plane is a colour channel. Note that an RGB image can appear greyscale if all three channels are equal.
PNG images can directly store greyscale, indexed, or rgb images. If you don't know which one is stored in your file, you can query it with imfinfo.
You can also check the number of dimensions of the matrix returned by imread with ndims

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2016년 7월 26일
size() gives you number of rows and column of a given matrix... imread() reads an image into pixels..it outputs RGB values if the image is coloured.... In your case the image you read has size of 300x1500
  댓글 수: 2
Stephen23
Stephen23 2016년 7월 26일
편집: Stephen23 2016년 7월 26일
This Answer is Wrong and Misleading
The image has size 300x500x3
Read the size documentation and my answer to know why.
Guillaume
Guillaume 2016년 7월 26일
Indeed, very very wrong answer!

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by