Finding the dimensions of an image
551 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Image Analyst
on 4 Jul 2013
originalImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(originalImage);
1 Comment
DGM
on 3 Nov 2022
% 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)
% so unless you safeguard against the dimensionality of inputs
% you have to always discard the last output
[rows, cols, chans, ~] = size(inpict)
More Answers (3)
bazinga
on 4 Jul 2013
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.
0 Comments
DGM
on 10 Apr 2022
Edited: DGM
on 3 Nov 2022
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
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
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
So I consider imsize() to be generally safer and more convenient than using size().
0 Comments
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!