Why does IMRESIZE return an image outside of the range [0,1]?

13 views (last 30 days)
I am using IMRESIZE with either the 'bilinear' or 'bicubic' interpolation method. If I input an image that is in the range [0,1], the output image sometimes falls outside of this range. Here is an example that reproduces the situation:
% Create an arbitrary matrix, and normalize it
img=magic(250);
imgi=img/max(img(:));
% Resize the image
imgo=imresize(imgi,[180,180],'bilinear');
% Show the results
disp(sprintf('Input min = %g. Input max = %g.',min(imgi(:)),max(imgi(:))))
disp(sprintf('Ouput min = %g. Output max = %g.',min(imgo(:)),max(imgo(:))))
Input min = 1.6e-005. Input max = 1.
Ouput min = -0.0563929. Output max = 1.0631.
This could potentially cause problems with TrueColor (mxnx3) images, because if I then use IMSHOW (as in the code below), I get the following error:
% Create an arbitrary TrueColor image matrix, and normalize it
img=repmat(magic(250),[1 1 3]);
imgi=img/max(img(:));
% Resize the image
imgo=imresize(imgi,[180,180],'bilinear');
% Show the results
disp(sprintf('Input min = %g. Input max = %g.',min(imgi(:)),max(imgi(:))))
disp(sprintf('Ouput min = %g. Output max = %g.',min(imgo(:)),max(imgo(:))))
% Display the resized image
imshow(imgo)
??? Error using ==> image
TrueColor CData contains element out of range 0.0 <= value <= 1.0.
Error in ==> D:\Applications\MATLAB6p5\toolbox\images\images\imshow.m
On line 104 ==> hh = image(xdata, ydata, cdata, 'BusyAction', 'cancel', ...

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 10 Sep 2012
The situation is caused by the anti-aliasing filtering process (used when shrinking an image) and/or the 'bicubic' interpolation that IMRESIZE uses. This type of interpolation can cause data to fall outside of the original range. Starting with R2008b, this is documented in the 'Notes' section of the documentation page for the IMRESIZE function:
To avoid allowing your data to fall outside of the [0,1] range, you can manually clip the data results using the following code as a guide:
img=repmat(magic(250),[1 1 3]);
imgi=img/max(img(:));
imgo=imresize(imgi,[180,180],'bilinear');
% Clip the upper bound of the data to 1
imgo(imgo>1) = 1;
% Clip the lower bound of the data to 0
imgo(imgo<0) = 0;
disp(sprintf('Ouput min = %g. Output max = %g.',min(imgo(:)),max(imgo(:))))
Alternatively, you can avoid the filter when using IMRESIZE. For example:
% Create an arbitrary TrueColor image matrix, and normalize it
img=repmat(magic(250),[1 1 3]);
imgi=img/max(img(:));
% Resize the image
% Notice the last argument, which specifies that we avoid filtering
imgo=imresize(imgi,[180,180],'bilinear',0);
% Show the results
disp(sprintf('Input min = %g. Input max = %g.',min(imgi(:)),max(imgi(:))))
disp(sprintf('Ouput min = %g. Output max = %g.',min(imgo(:)),max(imgo(:))))
If you are using a TrueColor (mxnx3) image matrix with IMRESIZE and IMSHOW and you are receiving an error, then the source of the error is a result of the IMAGE command not automatically clipping RGB image data to the [0,1] range when the image is created. This IMSHOW problem has been fixed in Image Processing Toolbox 4.0 (R13+). IMSHOW now clips the data automatically. For previous releases, the example above should help you to work around the problem.

More Answers (0)

Categories

Find more on Images 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!