Displaying an image is usually the first step in any MATLAB image processing project. You load the file into a variable, then show that variable in a figure window. This guide covers the basic commands, what they do, and the mistakes that most often get in the way. The video below walks through the same steps.
The two basic steps: read, then show
MATLAB treats an image as a numeric array. Reading a file turns it into that array, and showing it draws the array on screen. Two functions cover most cases:
- imread reads an image file (JPG, PNG, BMP, TIF and other common formats) into a variable.
- imshow displays the image in a figure window.
Step-by-step: display an image in MATLAB
- Put the image in your current folder, or note its full path.
- Read it into a variable: img = imread(‘peppers.png’);
- Display it: imshow(img);
- Add a heading if you like: title(‘Original image’);
The file peppers.png ships with MATLAB, so it is handy for a first test. To use your own picture, replace the name with your file name, for example imread(‘photo.jpg’). If the file is somewhere else, give the full path inside the quotes.
What is inside the variable?
After reading, type size(img) in the Command Window. A color (RGB) image returns three numbers: rows, columns and 3, one layer each for red, green and blue. A grayscale image returns only rows and columns.
Most JPG and PNG files load as the uint8 type, with pixel values from 0 to 255. You can check this with class(img). For more details about the file itself, such as its format and dimensions, use imfinfo(‘peppers.png’).
Displaying grayscale images
A grayscale image is displayed the same way, with imshow. If the values do not use the full range and the picture looks too dark or washed out, tell MATLAB to scale the display between the lowest and highest pixel values:
- imshow(img, []);
This only changes how the image is shown. It does not change the data in the variable. If you need to go from a color image to gray scale, MATLAB has a dedicated function for that, and our separate tutorial covers it.
Showing several images in one figure
To compare a result with the original, place both in one window with subplot:
- subplot(1,2,1); imshow(img); title(‘Original’);
- subplot(1,2,2); imshow(gray); title(‘Processed’);
The first two numbers set the grid (1 row, 2 columns) and the third picks the position. Use figure before a new imshow call if you want each image in its own window instead.
If you do not have the Image Processing Toolbox
imshow belongs to the Image Processing Toolbox. In base MATLAB you can use image(img) or imagesc(img) instead. These draw axes and may stretch the picture, so add axis image to keep the correct proportions, and axis off to hide the tick marks. For a grayscale array, colormap gray makes it appear in gray tones rather than the default colors.
Common problems and fixes
- “Unable to find file or directory”: MATLAB cannot see the file. Check the spelling and extension, make sure the folder is the current folder, or use the full path.
- Image looks completely black or white: this often happens when the data type and value range do not match. A double image is expected to run from 0 to 1, while uint8 runs from 0 to 255. Convert with im2double or im2uint8, or use imshow(img, []).
- Image is tiny in the window: small images may be shown at a reduced size. Use imtool(img) to zoom and inspect pixel values.
- Colors look wrong: check that you are showing the right variable and that the image really has three layers.
Saving and going further
After processing, you can save a result with imwrite(result, ‘output.png’);. If you also work in other languages, the same ideas apply in OpenCV and Pillow. See our notes on opening an image in Python and writing a new image in Python for a comparison. If you want live input rather than a saved file, you can also grab an image from a USB webcam in MATLAB and show it with the same imshow command.
For more worked examples, including filtering and conversions, see the link below.
FAQ
What is the difference between imread and imshow?
imread loads the file into a variable so you can work with the pixel data. imshow only draws that data in a figure window. You need both to open and view a picture.
Why does imshow show an error in my MATLAB?
The most likely cause is a missing Image Processing Toolbox, or a typo in the file name or path. Try image(img) if the toolbox is not installed.
Can I display an image from a different folder?
Yes. Pass the full path to imread, or use addpath or change the current folder so MATLAB can find the file.
How do I show the image at its real size?
Use imshow(img, ‘InitialMagnification’, 100); to open it at 100% magnification, assuming it fits on your screen.