How to Convert a Picture to a Matrix by MATLAB

by Michael Carroll

MATLAB (Matrix Laboratory) is a computing environment often used in academic research. It is a powerful tool for performing all kinds of mathematical analyses and manipulations of matrices. A digital picture is represented in a computer as a matrix, with various matrix elements specifying different properties of each pixel in the picture. Since a digital picture is stored as a matrix, you can open it with MATLAB to analyze and manipulate the picture as a matrix of numbers.

Use the "imread" function to import a picture into MATLAB. In the following example, the picture called "example.jpg" is assigned to a matrix named "picture:" picture = imread('example.jpg'); The default file path for MATLAB files is a folder called "MATLAB" in your "My Documents" folder. If your picture is stored somewhere else, type the full path to it (e.g., "C:\Users\username\Desktop\example.jpg"). Import a picture from the Internet by using its URL in the "imread" function. Be sure to include the semicolon at the end of the command; if you don't, the function will display the entire matrix in the Command Window when it loads the picture, and this can take considerable time if the picture is large.

Import the picture and its color information into separate matrices with the following syntax, using any matrix names you like on the left side of the equal sign: [picture,picturecolors] = imread('example.jpg');

Separate the picture's transparency information into its own matrix with the following syntax, using any matrix names you like on the left side of the equal sign: [picture,picturecolors,picturetrans] = imread('example.jpg');

Manipulate the picture as a matrix using normal MATLAB commands. Try the image processing toolbox functions, which are specially designed for manipulating pictures with matrix mathematics. Save the matrix to a text file with the "dlmwrite" command, or save it back to a picture file with the "imwrite" function.


  • check Use the "imfinfo" function (e.g., "imfinfo('example.jpg')") to view detailed information about a picture file. This may help you interpret the matrix data after you import it.
  • check Type the command "imformats" to display a list of picture formats compatible with "imread" and "iminfo." Most common formats are supported by default.

About the Author

Michael Carroll is a high school mathematics teacher. He has written for various websites since 2010, specializing in programming, web design, electronics and various pieces of software. He holds a bachelor's degree in electrical engineering from the University of Texas, with specialization in embedded system design.