Join Patrick Royal for an in-depth discussion in this video Working with images, part of Learning MATLAB.
When most people hear the term "matrix", they don't tend to associate it with an image However, a matrix is precisely what an image is. It's a two-dimensional collection of values corresponding with pixels of color. As a result, MATLAB can actually import an image just as it would an Excel spreadsheet. And can work with the data within as if were purely numerical. To import an image, double-click on it in your active folder. In this example, I'll import the file named building.jpg in exercise files.
These specific format of an image matrix depends on the file format itself. But the most common format is RGB, where each pixel contains three values corresponding to a saturation in red, green and blue colors. As a result, MATLAB models this image as a three dimensional matrix. The first two dimensions correspond to the height and width of an image, and the third is always equal to three, so that each pixel can have the correct three values stored in it. Modifying an image is a complicated mathematical problem that is beyond the scope of this course, but MATLAB treats image data as nothing but a number. So any formula that works with pixels of the available data type will function as expected in MATLAB.
For instance, one very simple modification we could do would be to remove all of the red pixels from this image. To do that, we can type in building of colon, comma, colon, comma 1 equals zeroes of 685 comma 1,024. Basically, what this does is it takes the first slice of the building matrix, which corresponds to the red values, and sets it equal to a zero matrix of the same size.
When we run this, and then type in image of building to display the building, we can see that it now displays completely green and blue with no red color at all. A few simple functions that work well with MATLAB images are Image, Image SC, ImageRead, and ImageWrite. The image function displays your data as an image, which is useful as a way to see how the image changes as a result of any modifications made in the context of a script.
Imagesc also displays an image, but it first scales the colors of the image to include the full range of colors available. This is roughly equivalent to increasing the contrast of an image. And it's useful as a way of insuring that the image will display correctly on different monitors with different color schemes. The imageread and imagewrite commands do not display an image directly, instead they allow you to load an image from a file or store on MATLAB Matrix as an image, respectively. This saves you from having to manually load and store images before and after running a script.
With these tools, working with images in MATLAB should be a lot less mystifying.
- Installing MATLAB
- Working with MATLAB variables
- Working with matrix and scalar operations
- Creating functions
- Understanding performance considerations
- Building basic plots
- Creating responsive programs
- Editing variables manually
- Working with the Statistics Toolbox