Finding the dimensions of an image
562 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Image Analyst
2013 年 7 月 4 日
originalImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(originalImage);
1 件のコメント
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)
% so unless you safeguard against the dimensionality of inputs
% you have to always discard the last output
[rows, cols, chans, ~] = size(inpict)
その他の回答 (3 件)
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.
0 件のコメント
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
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Convert Image Type についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!