how to fix in imresize error ??
12 次查看(过去 30 天)
显示 更早的评论
i try to make the function for resizing image, this the code:
>> a=imread('1.png');
>> b=20000;
>> l=sum(sum(a));
>> [m,n]=size(a);
>> m1=fix(m*sqrt(b/l));
>> n1=fix(m*sqrt(b/l));
>> c=imresize(a,[m1 n1]);
but, i get the error :
Error using imresize>scaleOrSize (line 382) Invalid scale or size input argument.
Error in imresize>parsePreMethodArgs (line 354) [scale, output_size] = scaleOrSize(next, next_arg);
Error in imresize>parseInputs (line 248) [params.A, params.map, params.scale, params.output_size] = ...
Error in imresize (line 141) params = parseInputs(varargin{:});
i don't understand what cause of the error and i try to fix the error but i can't fix it. Please Help, Thank you
0 个评论
回答(2 个)
Image Analyst
2016-3-5
It's probably color. To fix, try this:
[m, n, numberOfColorChannels]=size(a)
And use descriptive names like rows, columns, grayImage, etc. not an alphabet soup of names like a,b,c,m,n,i,j, etc. That makes it hard to read code, especially when it's not commented.
5 个评论
Joshua Bone
2020-7-9
So imresize() doesn't support RGB images? Why does it have to be a binary image?
Image Analyst
2020-7-9
imresize() works fine for RGB images as well as gray scale images, but you have to pass it the number of rows and columns. When she incorrectly did
% Never use size() like this with the array that comes directly out of imread()!
[m,n]=size(a);
the (poorly-named) n was actually (the number of columns) * (the number of color channels), or if "a" were an RGB image, the number of color channels is 3 so n is 3 times as big as it should be.
Tari
2024-5-16
Error using matlab.images.internal.resize.resizeParseInputs>parsePreMethodArgs (line 148)
When the second argument is a 1-by-3 vector, IMRESIZE interprets it as an M-by-3 colormap. If you intend to specify
an output size, use the syntax: imresize(A,___,'OutputSize',SZ) where SZ is a 1-by-2 vector.
1 个评论
DGM
2024-5-16
编辑:DGM
2024-5-16
While that's true, the error returned will be different than what OP was getting.
Error using matlab.images.internal.iptcheckmap (line 55)
Function IMRESIZE expected input number 2, MAP, to be a valid colormap. Valid colormaps cannot have values outside the range [0,1].
Obviously, that happens because any meaningful 1x3 size tuple in pixels is going to have values >1. Even if you gave it a nonsense sub-unity triplet, the error would still be
Error using imresize>fixupSizeAndScale (line 713)
Either scale or output size must be specified.
There are at least two problems with OP's code, if not three. @Image Analyst's link explains the problem with using size() carelessly, but due to what are likely other errors in OP's code, their misuse of size() probably didn't even get a chance to come into play.
Based on the error itself, we know that the image is RGB. This is supported by the fact that the file is a JPG. While single-channel JPGs exist, they are uncommon. Furthermore, there are no unit-scale JPGs (e.g. float or logical class). The fact that the geometry calculation is a function of the numeric scale of the data is probably causing a problem since OP seems to be presuming that the incoming image is in unit-scale.
% the inputs
a = imread('peppers.png'); % uint8, RGB (384x512x3)
b = 20000; % an unexplained magic number
% this is wrong, since it does not sum across pages
% or otherwise take them into account.
% it also doesn't really make sense that the pixel values
% (which are a strong function of the numeric class)
% should be used as a geometry scaling factor.
% so this might also be a conceptual bug and/or
% a misunderstanding of JPG functionality and numeric scale.
l = sum(sum(a))
% this is also wrong, but n is never actually used,
% which is probably also a bug
[m,n] = size(a)
% this is a 1x1x3 vector
m1 = fix(m*sqrt(b/l))
% since m is used instead of n for no apparent reason
% this is an exact duplicate of m1
n1 = fix(m*sqrt(b/l))
% the size argument is a 1x2x3 array
c = imresize(a,[m1 n1]);
This is my guess at what's intended. The user had a bunch of binary masks which they ruined by saving them as JPGs. Now they're RGB images, mis-scaled, and full of artifacts.
inpict = imread('i_ruin_my_images_with.jpg'); % this is a garbage JPG
outputarea = 20000; % a parameter
% clean up the input
inpict = im2gray(inpict);
inpict = imbinarize(inpict);
% rescale the image
inputarea = nnz(inpict); % get the total blob area
szi = size(inpict,1:2); % only get the specific dimensions needed
szo = fix(szi*sqrt(outputarea/inputarea)); % use vectors
outpict = imresize(inpict,szo);
Now the total blob area in the output image is as close as it can be to the specified parameter without exceeding it.
另请参阅
类别
在 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!