Size function regarding image
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Shyam Joshi
am 4 Apr. 2019
Bearbeitet: DGM
am 12 Nov. 2022
a=imread('image');
[~,cc]=size(a);
What does the value of cc indicate about the image?
0 Kommentare
Akzeptierte Antwort
Steven Lord
am 4 Apr. 2019
"If fewer than ndims(A) output arguments are specified, then all remaining dimension lengths are collapsed into the last argument in the list. For example, if A is a 3-D array with size [3 4 5], then [sz1,sz2] = size(A) returns sz1 = 3 and sz2 = 20."
If you want the size of the array in all its dimensions (leaving out the trailing singleton dimensions) call size with one input and one output.
A = ones(2, 3, 4);
sz = size(A) % Returns [2 3 4]
If you want the size of the array in a specific dimension, the easiest way to do this is to call size with two inputs (not two outputs) where the second is the dimension whose size you want to compute.This will work even when the second input is a number past the length of the vector returned by the one input and one output call to size.
A = ones(2, 3, 4);
sz3 = size(A, 3) % Returns 4
sz42 = size(A, 42) % Returns 1
0 Kommentare
Weitere Antworten (2)
KSSV
am 4 Apr. 2019
I = imread('peppers.png') ;
[nx,ny,nt] = size(I)
If your image is RGB, in the baove nx,ny,nz gives number of rows, columns and matrices (here 3) respectively. If binary it will be number of rows, columns with nz = 1.
[nx,ny] = size(I)
If your image is RGB, in the baove nx,ny gives number of rows, and number of columns*number of matrices. If binary it will be number of rows, columns.
It is a simple check..you can check on your own.
DGM
am 12 Nov. 2022
Bearbeitet: DGM
am 12 Nov. 2022
Another way to describe the scalar output syntax is simply that the last output argument is always the product of the sizes of any dimensions other than those previously requested.
So
[~,cc] = size(A);
returns the product of the sizes of all dimensions other than dim1.
- If A is 10x20x1, cc is 20
- If A is 10x20x3, cc is 60
- If A is 10x20x3x2, cc is 120
In other words, the last output argument does not strictly refer to a single dimension. If you want to ensure that the nth output argument always returns the size of the nth dimension of your array, you need to request and discard an extra output.
[~,cc,~] = size(A);
In this case, cc always returns the size of dim2 of A. This is the same as
cc = size(A,2);
For images, getting the number of rows, columns would be done by:
[rows,cols,~] = size(A); % discard the last output
Omitting the third output would cause the number of columns to be wrong for any RGB input.
If you can't control the dimensionality of your inputs, and you're using size() with scalar outputs, and you want the outputs to always refer to specific dimensions, then you need to discard the last output.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!.png)
