Lab 24
Binary File Input/Output
CS211 Lab Policy:
- This lab exercise will not be graded.
- Submit as much as you have completed before the end of the lab period in
which it is assigned.
- If you do not finish this lab work, it is to your advantage to finish it
outside of class. Please re-submit your finished work to the course web
site.
- You may receive help from anyone in completing this lab.
- You may not submit another student's code as part of your
lab.
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()
- Clear the command window.
- 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');
- Display the image in a figure window using the
image() function.
- Prompt the user for 4 values that define a rectangular sub-region of the
image:
- a starting row value
- an ending row value
- a starting column value
- an ending column value
- Prompt the user for a file name.
- 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()
- Clear the command window.
- Prompt the user for a file name.
- 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.
- 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.
- 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.