Errors with imshow function

26 views (last 30 days)
Chance
Chance on 17 Feb 2023
Answered: Voss on 17 Feb 2023
What is happening here?

Answers (2)

Walter Roberson
Walter Roberson on 17 Feb 2023
There is no variable named image in your P2image.mat file, so your second line is being interpreted as
imshow(image())
When you call image() with no parameters, it creates an image object with a default image (that has a bunch of easter-eggs hidden in it.) The handle of the image() object is returned and is passed to imshow(). But imshow() cannot accept handles to image() objects, and so gives an error message.
We recommend that you do not use load() without assigning the result to a variable. For example,
datastruct = load('P2image.mat');
P2image = datastruct.P2image; %if the variable is named P2image in the file
imshow(P2image)
There is one other possibility:
If you had this code inside a function, then there might have been a variable named image inside the mat file after all. However, inside functions, MATLAB is explicitly permitted to refuse to pay attention the possibility that you might be "poofing" a variable into existence where the variable just happens to have the same name as a function. MATLAB is permitted to continue to treat references to the name as functions. This applies only when the variable that has the same name as a function is created by using load() without outputs, or by using evalin() or assignin() . [Note: The syms command is actually a function that uses assignin() so variables created using syms are potentially affected by this rule.]

Voss
Voss on 17 Feb 2023
The image function creates an image object.
The imshow function creates an image object and does other stuff to the axes, given an array of image data. When you pass imshow an image object rather than an array of image data, you get that error.
imshow(image)
Error using imageDisplayValidateParams
Expected input number 1, I, to be one of these types:

double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64, logical

Instead its type was matlab.graphics.primitive.Image.

Error in images.internal.imageDisplayValidateParams (line 11)
validateattributes(common_args.CData, {'numeric','logical'},...

Error in images.internal.imageDisplayParseInputs (line 79)
common_args = images.internal.imageDisplayValidateParams(common_args);

Error in imshow (line 253)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Notice the error essentially says, "Expected input #1 to be numeric (or logical) but it was an image object instead."
To avoid this error, you should pass imshow an array of image data; I imagine that would be one of the variables stored in P2image.mat, but I couldn't say which one, since I don't have that file.

Products

Community Treasure Hunt

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

Start Hunting!