how to fix in imresize error ??

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

Risposte (2)

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 Commenti

ElizabethR
ElizabethR il 5 Mar 2016
Modificato: ElizabethR il 5 Mar 2016
thank for your suggestion. I already try but, i still getting error :(
>> a=imread('1.jpg'); ( binary Image )
>> [m, n, numberOfColorChannels]=size(a);
>> WideObject=sum(sum(a));
>> ScaleFactor=20000;
>> m1=fix(m*sqrt(ScaleFactor/WideObject));
>> n1=fix(n*sqrt(ScaleFactor/WideObject));
>> c=imresize(a,[m1 n1]);
I think it is due to the scale factor
No, not due to scale factor because if it were truly a binary image, it works. See:
grayImage=imread('cameraman.tif'); % Gray scale image
% Create a binary image
a = grayImage > 128; % Now the badly-named "a" is a binary image
[m, n, numberOfColorChannels]=size(a)
WideObject=sum(sum(a));
ScaleFactor=20000;
m1=fix(m*sqrt(ScaleFactor/WideObject));
n1=fix(n*sqrt(ScaleFactor/WideObject));
c=imresize(a,[m1, n1]);
The above code works just fine.
However if it's an RGB image, it gives the exact error message she got. See:
a=imread('peppers.png'); % Read in a color image.
[m, n, numberOfColorChannels]=size(a)
WideObject=sum(sum(a));
ScaleFactor=20000;
m1=fix(m*sqrt(ScaleFactor/WideObject));
n1=fix(n*sqrt(ScaleFactor/WideObject));
c=imresize(a,[m1, n1]);
m =
384
n =
512
numberOfColorChannels =
3
Error using imresize>scaleOrSize (line 405)
Invalid scale or size input argument.
Error in imresize>parsePreMethodArgs (line 377)
[scale, output_size] = scaleOrSize(next, next_arg);
Error in imresize>parseInputs (line 260)
[params.A, params.map, params.scale, params.output_size] = ...
Error in imresize (line 146)
params = parseInputs(args{:});
Error in test (line 7)
c=imresize(a,[m1, n1]);
So while it may look like (the badly-named) a is a binary image, it's really a color image with just 2 colors: black and white. To fix, do this:
grayImage = rgb2gray(a);
binaryImage = grayImage > 128;
Then use binaryImage instead of a from that point on.
So imresize() doesn't support RGB images? Why does it have to be a binary image?
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.

Accedi per commentare.

Tari
Tari il 16 Mag 2024

0 voti

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 Commento

DGM
DGM il 16 Mag 2024
Modificato: DGM il 16 Mag 2024
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.

Accedi per commentare.

Richiesto:

il 5 Mar 2016

Modificato:

DGM
il 16 Mag 2024

Community Treasure Hunt

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

Start Hunting!

Translated by