Handling images

Raster images are made up of a grid of colored pixels. In MATLAB, images can be represented as two-dimensional matrices, where each matrix element corresponds to a single pixel of the displayed image. For example, a 800x600 photo (consisting of 600 lines and 800 columns of pixel of different colors) will be stored as a 600x800 matrix. However, sometimes, a third dimension will be required to store the depth of color. For example, for RGB images, you will need to specify red, green, and blue values to create a color image.

MATLAB provides several functions to operate on and display images. The following list will give a description of the most used functions for image handling:

  • imread: Read the image from the graphics file
  • imwrite: Write the image to the graphics file
  • image: Display the image (create image object)
  • mfinfo: Get image information from the graphics file
  • imagesc: Scale data and display as an image
  • ind2rgb: Convert an indexed image to an RGB image

To import an image in MATLAB, we can use the imread() function. This function imports the image from the file specified in brackets, obtaining the format of the file from its contents. Let's see an example:

>> Coliseum = imread('coliseum.jpg');

This is a beautiful image of the Colosseum, also known as the Flavian Amphitheatre, an oval amphitheater in the centre of the city of Rome, Italy. This file has a resolution of 1650x1042 pixels (Figure 2.10), so the resulting matrix is actually 1650x1042x3.

To export an image from MATLAB, we will use the imwrite() function. It writes the data contained in a variable into a file specified by the user. The format of the exported file will be derived from the specified extension. The new file will be created in Current Folder. The bit depth of the output image will depend on the datatype in the variable and the file format. Then, to export the newly imported image to MATLAB (Figure 2.10), we will write:

>>imwrite(Coliseum,'coliseum.jpg');

Here is a wonderful image of the Colosseum in Rome, as an example resource to be imported into MATLAB:

Figure 2.10: Importing images into MATLAB

In the following chapters, we will experiment with the use of some of these features through practical examples. In any case, for a detailed description of its operation, please refer to MATLAB's help.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset