Lab 24

Binary File Input/Output

CS211 Lab Policy:

Instructions:

For this lab assignment you will create two MATLAB programs named Lab24_write() and Lab24_read().  Your first program will read an image file, display the image, and save a rectangular sub-region of the image to a binary file. Your program should work with any full-color image file. You can use this image file, venegia0010.jpg, to test your program. Your second program will read the binary data file produced from the first program and display an image.

Implement MATLAB code that will perform the following tasks.

For program Lab24_write()

  1. Clear the command window.
     
  2. Use the MATLAB statement below to read the image file. The variable My_image will be a multidimensional array after the statement is executed. It's number of rows and columns define the image size. It will typically have 3 layers - the first layer holds the red intensity values, the second layer holds the green intensity values, and the third layer holds the blue intensity values. Each intensity value is in the range [0, 255], where 0 means the absence of that color and 255 means the fullest intensity possible of that color. Values in the range [0, 255] can be stored in 1 byte of memory. MATLAB's data type for these values is uint8 (unsigned integer 8-bit values).
    	My_image = imread('venegia0010.jpg');
  3. Display the image in a figure window using the image() function.
     
  4. Prompt the user for 4 values that define a rectangular sub-region of the image:
  5. Prompt the user for a file name.
     
  6. Using fopen(), fwrite(), and fclose(), write the data that represents the sub-image defined by the user's row and column limits into a binary file. A pseudocode description of the desired code is shown below.

    (Note: When you read the binary data back from the file, you will need to know the sub-image size. Therefore, you should write the dimensions of the sub-image as the first two values in the file. Each dimension can be calculated with the formula (Ending_value - Starting_value + 1).)

Open the file for writing.
Write the number of rows in the sub-image. (Use an
int32 data type.)
Write the number of columns in the sub-image. (Use an
int32 data type.)
Write all the pixel values from the sub-image. (Use an
uint8 data type.) (Use a single fwrite() call.)
Close the file.

For program Lab24_read()

  1. Clear the command window.
     
  2. Prompt the user for a file name.
     
  3. Read the binary file created by your Lab24_write() program. The following pseudocode describes what is desired:

Open the file for reading.
Read the number of rows in the sub-image. (Use an
int32 data type.)
Read the number of columns in the sub-image. (Use an
int32 data type.)
Read all the pixel values from the sub-image. (Use an
uint8 data type.) (Specify the "count" argument as rows*cols*3.)
Close the file.

  1. Now you have a problem. Your pixel data is in a vector because fread() does not understand multidimensional arrays. Therefore, you must "reshape" the data into a 3D array that has the correct dimensions. Look up the reshape() function in the MATLAB help system and "reshape" your array of pixel values to appropriate dimensions.
     
  2. Display the image in a figure window using the image() function.

Just for fun:

In a "My Documents" window, attempt to open the binary file your Lab22_write() program created. (Right-click on the file, select the "Open With..." command, and select NotePad. If there is no "Open With..." option, double-click and use the "Select application from a list" option.) Can you make any sense out of the data using a text editor? Discuss this with your instructor if you don't understand the concept of binary data.
 

Turn-in:

Submit your Lab24_write() and Lab24_read() files.