Functions > Reading and Writing Files > Example: Using READRGB and READBMP
Example: Using READRGB and READBMP
Use READRGB and READBMP to read images into matrices. This example uses the image below.
dog.bmp
1. Use READRGB to read a color image.
Click to copy this expression
This matrix contains an RGB representation of the image.
2. Use READBMP to read the same a image file.
Click to copy this expression
This matrix contains a grayscale representation of the image, even though this is originally a color image.
3. Compare the sizes of the two matrices.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
M1 is three times bigger than M2. M1 is a packed image matrix, containing three matrices representing the red, green, and blue values of the image. M2 represents the gray levels of the image.
4. Use WRITERGB and WRITEBMP to write out the packed image matrix M1.
Click to copy this expression
Click to copy this expression
5. Compare the two images.
(dogRGB.bmp)
(dogBMP.bmp)
WRITERGB creates an RGB image. WRITEBMP creates three images that represent the levels of red, green, and blue of the image, respectively.
6. Calculate the width of the original image, based on the packed image matrix.
Click to copy this expression
7. Create three separate matrices for the red, green and blue values of the image.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
8. Use WRITEBMP to write the matrices to files and view the results.
Click to copy this expression
Click to copy this expression
Click to copy this expression
(rDog.bmp)
(gDog.bmp)
(bDog.bmp)
The red, green, and blue components are viewed as grayscale images.
9. Use the augment function to create a packed image matrix and manipulate the blue values.
Click to copy this expression
Click to copy this expression
10. Compare the two images.
(dogRGB2.bmp)
(dogBMP2.bmp)
WRITERGB writes the RGB image using the red, green, and blue values combined together. WRITEBMP writes a file three times wider, with the red levels, green levels, and blue levels represented as a grayscale image.
Was this helpful?