how to fix in imresize error ??

12 次查看(过去 30 天)
ElizabethR
ElizabethR 2016-3-5
编辑: DGM 2024-5-16
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

回答(2 个)

Image Analyst
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
Joshua Bone 2020-7-9
So imresize() doesn't support RGB images? Why does it have to be a binary image?
Image Analyst
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
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
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))
l =
l(:,:,1) = 23746472 l(:,:,2) = 13054194 l(:,:,3) = 11331211
% this is also wrong, but n is never actually used,
% which is probably also a bug
[m,n] = size(a)
m = 384
n = 1536
% this is a 1x1x3 vector
m1 = fix(m*sqrt(b/l))
m1 =
m1(:,:,1) = 11 m1(:,:,2) = 15 m1(:,:,3) = 16
% since m is used instead of n for no apparent reason
% this is an exact duplicate of m1
n1 = fix(m*sqrt(b/l))
n1 =
n1(:,:,1) = 11 n1(:,:,2) = 15 n1(:,:,3) = 16
% the size argument is a 1x2x3 array
c = imresize(a,[m1 n1]);
Error using matlab.images.internal.resize.resizeParseInputs>scaleOrSize (line 220)
Invalid scale or size input argument.

Error in matlab.images.internal.resize.resizeParseInputs>parsePreMethodArgs (line 163)
[scale, output_size] = scaleOrSize(next, next_arg);

Error in matlab.images.internal.resize.resizeParseInputs (line 28)
parsePreMethodArgs(varargin, method_arg_idx, first_param_string_idx);

Error in imresize (line 153)
params = matlab.images.internal.resize.resizeParseInputs(args{:});
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 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