Finding the dimensions of an image

조회 수: 555 (최근 30일)
Harry
Harry 2013년 7월 4일
댓글: DGM 2022년 11월 3일
Is there some way of getting MATLAB to find the pixel dimensions of an image uploaded using the 'imread' command?. Or more simply is there a way to test for the dimensions of the resulting matrix?

채택된 답변

Image Analyst
Image Analyst 2013년 7월 4일
originalImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(originalImage);
  댓글 수: 1
DGM
DGM 2022년 11월 3일
% a single-frame RGB image
inpict = imread('peppers.png');
% a 4-frame RGB image
inpict = repmat(inpict,[1 1 1 4]);
% the last output of size() does not refer to the size of dim3
% the last output always refers to the product of all remaining sizes
[rows, cols, chans] = size(inpict)
rows = 384
cols = 512
chans = 12
% so unless you safeguard against the dimensionality of inputs
% you have to always discard the last output
[rows, cols, chans, ~] = size(inpict)
rows = 384
cols = 512
chans = 3

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

추가 답변 (3개)

bazinga
bazinga 2013년 7월 4일
You can use the size command. Say you have read the document by imread and named it as X, just use size(X). The result will be the dimensions of your matrix, X.

DGM
DGM 2022년 4월 10일
편집: DGM 2022년 11월 3일
Everyone has already given the obvious answer, so now it's my turn to say that I actually avoid using size() for image processing. Imagine that.
I prefer to handle array size information as a vector. It tends to make it easier and more succinct to calculate related geometry information. With the available syntaxes, size() with either return a scalar, multiple scalars, or a variable-length vector. When processing images which may have differing number of channels, it's often necessary to know the size of dimensions which may or may not be present in the vector returned by size(). Dealing with the variabilitly of results from size() is trivial, but an unnecessary annoyance in my opinion.
Let's start with a simple example wherein a number of images are processed
% images can have different geometry or different number of non-singleton dimensions
% get some varied images and cram them into a cell array
A = rgb2gray(imread('peppers.png')); % an I image
B = imread('peppers.png'); % an RGB image
C = cat(4,B,fliplr(B),flipud(B),rot90(B,2)); % a 4D RGB image
IMGS = {A B C};
% let's process these varied images in a loop
% starting simple, just show sizes using size()
% note the vectors are all different lengths
for k = 1:numel(IMGS)
thisimage = IMGS{k};
thissize = size(thisimage)
end
thissize = 1×2
384 512
thissize = 1×3
384 512 3
thissize = 1×4
384 512 3 4
Now let's elaborate on this loop. Let's say we wanted to do something that's a function of the number of image channels or frames (e.g. process the image pagewise and framewise). We can't use the size vectors we just calculated, because there's no guarantee that they have a third or fourth element.
Either we have to explicitly call size() again to get that info:
for k = 1:numel(IMGS)
thisimage = IMGS{k};
thissize = size(thisimage); % we already called size() and got a vector ...
for f = 1:size(thisimage,4) % ... but we can't use it for this
for c = 1:size(thisimage,3) % ... and we can't use it for this either
% do something to this channel
thisimage(:,:,c,f) = medfilt2(thisimage(:,:,c,f));
end
end
% and maybe we do other stuff...
end
... or we have to call size() like this:
for k = 1:numel(IMGS)
thisimage = IMGS{k};
[m,n,c,f,~] = size(thisimage); % explicitly get the size of each dim as scalars
thissize = [m n c f]; % now we can guarantee that the vector has the elements we need
% note that when using size() with scalar outputs like this, the last output cannot be
% relied upon to be correct, hence the necessity of discarding one extra output every time.
% That's just another reason to avoid size().
for f = 1:thissize(4) % and we can use it without potentially redundant size() calls
for c = 1:thissize(3) % imagine that
% do something to this channel
thisimage(:,:,c,f) = medfilt2(thisimage(:,:,c,f));
end
end
% and maybe we do other stuff...
end
Both of these work, but I don't like dealing with either. MIMT has a simple tool called imsize() (attached) that will return a fixed-length size vector. By default, the vector is length 4, which should be sufficient for sane images.
for k = 1:numel(IMGS)
thisimage = IMGS{k};
thissize = imsize(thisimage); % by default, imsize() returns a 4-element vector
for f = 1:thissize(4)
for c = 1:thissize(3)
% do something to this channel
thisimage(:,:,c,f) = medfilt2(thisimage(:,:,c,f));
end
end
% and maybe we do other stuff...
end
The fact that the vector is fixed-length makes simple comparisons like this very succinct, without the need to safeguard against dimensionality differences.
sizesdiffer = any(imsize(A) ~= imsize(B)) % assuming images have no more than 4 dims
sizesdiffer = logical
1
The length of the vector returned by imsize() can be optionally specified. This makes it handy when one only wants to know the page geometry of an image and doesn't want to deal with extraneous entries.
for k = 1:numel(IMGS)
thisimage = IMGS{k};
thisgeometry = imsize(thisimage,2)
end
thisgeometry = 1×2
384 512
thisgeometry = 1×2
384 512
thisgeometry = 1×2
384 512
So I consider imsize() to be generally safer and more convenient than using size().

Matt J
Matt J 2013년 7월 4일
편집: Matt J 2013년 7월 4일
Or more simply is there a way to test for the dimensions of the resulting matrix?
The SIZE command

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by