Before beginning this homework, create a new Notepad++ file in your cs7sXX home directory
on ieng6 to record your answers to the Questions. Save as H2_LastName.txt just like in
Homework#1.
STEP 0 Image Basics:
1. In this homework assignment, we will be working with images in MATLAB. In the most
recent lab, we used the imread() function to get a three-dimensional matrix
representing the image. Here are some examples illustrating how we can work with
images in MATLAB (i.e., you should try to execute these commands with the appropriate
setup but it is optional).
>> img = imread('wave.jpg');
The variable img will be a 3D matrix of size [height of ‘wave.jpg’] x [width of ‘wave.jpg’] x
3 (in that order). Figure 1 shows what the original image looks like.
>> imshow(img);
To display the matrix img as a picture, we will use the function imshow().
>> blueval = img(45, 50, 3);
To access the individual elements of the matrix, we will state the variable name and
three indices (or index ranges) of the matrix. In the code above, it is looking at the matrix
variable img, specifically the element at the 45th row, 50th column, and 3rd
floor. Recall
that for image matrices, the floors correspond to the red, green, or blue color values. So,
to get the red or green values, we will just specify 1 or 2 (respectively) rather than 3 for
the blue value (remember RGB!).
Question #1: What is the code to assign the red value of the pixel at row 16, column 20 of
an image matrix called img, to a variable called redval?
>> new_img2 = img(1:end, 1:2:end, :);
MATLAB allows the syntax where if we put a value in between the range, it will change
the step size. For example, in the code above, we are accessing the columns from 1 to
the end in steps of 2, i.e., every other column. So, we look at the variable img and look
at all the rows (1:end), every other column, and all the colors. Then, we save these
elements of img as a new variable called new_img2. Refer to Figure 3 for the resulting
image
EXAMPLE: a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18]
>> a(1:4:16) (try it yourself)
>> new_img3 = [img, img];
MATLAB will also allow creating a matrix of matrices as well. The code above, places
the two matrices right next to each other and saves the resulting matrix as new_img3.
Refer to
Figure 4 for this image.
Question #2: Instead of placing a comma between the two img variables, try it with a
semicolon – what happens, and why?
STEP 1:
1. Choose an image that you would like to work for this assignment. Preferably a
medium-sized one, rather than one that takes up the entire screen or one that is too tiny.
It would be fun to use something that shows your own interests, but any image is fine as
long as it is appropriate for a school/workplace environment (i.e. not offensive)
a. Save your image into the folder where you keep your MATLAB scripts. You
should see the name of your image file appear in the file list. Also, save a copy
of the original image in your cs7sXX home directory
2. Create a new script called Crop.m.
a. The first line of your script should be:
img = imread(filename);
b. This line imports filename as a matrix of numbers which MATLAB can then
interpret into an image. The matrix representing the image is called img.
Notice that the script uses the variable filename, so you will need to set
filename to something in the Command Window before you run the script (i.e.,
>> filename = ‘Bean.jpg’;).
c. Save your script (just one line of code so far).
3. In the Command Window, run your script by typing these lines:
a. Make a variable named filename and assign it a string that is the name of your
file. Note that the image file that you assigned to the variable filename now
appears in the Workspace.
b. Now call the script, and use the imshow function to show your image. Remember
that img is the variable created in Crop that is a matrix of numbers representing
the picture.
>> Crop
>> imshow(img);
c. You should see a window open within MATLAB that shows your image. Check to
see that you see your image and everything seems to be working.
d. If you examine the contents of the image matrix by double-clicking it in the
Workspace, you will see that it is a matrix of numbers between 0 and 255.
4. Now that we've checked that you can open and view images, let's finish our Crop script.
Open the Crop script.
a. Create a variable named cropped_img that will hold a cropped image made from
the first three quarters of the original image.
b. This is done using matrix indexingCropping img will be similar to what is being
done below. However, we show you how we cropped a picture in half
column-wise. How can this be adapted to crop only the last quarter of the image
instead of half?
>> new_img1= img(1:end, 1:end/2, :);
This line of code saves a portion of the matrix called img as a new matrix
called new_img1. The specific portion of img that is copied is specified by the
indices inside the parentheses. 1:end accesses the rows starting at 1 and going
to end. 1:end/2 accesses the columns from 1 to the middle, which is half the
width of the image. : accesses all the floors, so a
ll of the RGB color values. If we
didn’t take all 3 color values, the coloring in our image would change. Refer to
Figure 2 for what this image looks like.
i. First, create a new variable called num_of_cols that will hold the number
of columns in your image img.
ii. Now that you have a variable that holds the number of columns in your
image, how can you calculate the number of columns needed for the
cropped image? Store the new number of columns into a variable called
new_cols.
iii. Apply what you learned in Step 0 to crop img and store it into your
variable cropped_img.
(Hint: access the columns from 1 to new_cols)
c. Here is what the image looks like before and after the crop:
Note: You may get a warning that looks like:
Warning: Integer operands are required for colon operator
when used as index
> In Crop (line 2)
In this case, you need to convert a double (real) number to an integer. For
example, if the width of your image is 215 columns, you won't be able to select the
107.5 columns on the right! You need an integer instead of a fractional number of
rows. To do this, you can use the floor function. It takes in a number as an
argument, and returns that number rounded down to an integer. You can try
floor in the Command Window on simple examples to get the feeling for it:
>> floor(215/2)
ans =
???
>> floor(2.7)
ans =
???
>> floor(2)
ans =
???
Question #3: When you are finished testing your Crop s cript, copy and paste all the
commands into your Notepad++ document. Save the cropped_img image as
cropped_img1.jpg.
STEP 2:
1. Make another script, Squish.m that uses the cropped_img variable that you made in
Crop. Use the cropped_img input from crop.m and create a shortpic as an output
variable, where shortpic contains every other row of cropped_img.
(Hint: refer to Figure 3 of Step 0.)
2. Now within the same script, using the variable shortpic, create a variable
squished_img that will hold a new image made up of two shortpic images placed next
to each other (horizontal).
(Hint: refer to Figure 4 of Step 0.)
Question #4: When you are finished editing your Squish script, copy all commands into
your Notepad++ document. Save the squished_img image as squished_img1.jpg.
Your squished_img should look similar to this:
STEP 3:
1. Make a script called Stretch.m that uses the cropped_img which you made in
Crop. Use the cropped_img input and create a t
allpic as an output variable,
where tallpic contains every other c olumn of cropped_img.
2. Now within the same script, using the variable tallpic, create a variable
stretched_img that will hold a new image made up of two tallpic images stacked
on top of each other (vertical).
(Hint: How can you modify what you did in Step 2 to place the images together
vertically instead of horizontally?)
Question #5: When you are done testing your Stretch script, copy all commands into
your Notepad++ document. Save the stretched_img image as
stretched_img1.jpg.
Your stretched_img1 will look something like this :
STEP 4:
1. Run all your scripts on the image you selected in Step #1 above, as well as one other
image of your choosing. Testing your code with two images is a way to see if your code
is input-flexible.
2. Images Needed For Checkoff document:
● In order to get checked off, you will need 8 images in YOUR cs7sXX folder.
1) Unmodified image 1
2) Result of Crop s cript on image 1 (cropped_img1.jpg)
3) Result of Squish s cript on image 1 (squished_img1.jpg)
4) Result of Stretch on image 1 (stretched_img1.jpg)
5) Unmodified image 2
6) Result of Crop s cript on image 2 (cropped_img2.jpg)
7) Result of Squish s cript on image 2 (squished_img2.jpg)
8) Result of Stretch on image 2 (stretched_img2.jpg)
● You can save your images by going to File->Save As on the window that opens
when you run imshow(cropped_img). P
LEASE CHOOSE TO SAVE YOUR
IMAGES AS JPG/jpg format.
● Paste the script code for all three scripts into your Notepad++ document. Clearly
label the scripts as indicated in the instructions.
© Copyright 2026 Paperzz