Matlab has some nice built-in features that make it convenient to experiment with images. This part of the assignment will introduce you to several of these features.
A repository of images is growing in the
directory /cs/cs153/Images and its subdirectories.
For this assignment, there is a set of test images in
/cs/cs153/Images/spamImages2008.
Matlab has a nice image interface (through its image processing
toolbox, which HMC's versions of matlab do have) named imtool.
Simply type imtool at the matlab prompt to open a
window. You should be able to open the image at
/cs/cs153/Images/a1/testimage.jpg in the customary
way, i.e., "File" ... "Open," etc.
You'll see the various options for manipulating and gathering information about an image -- try them all out! A wonderful one is "File" -- "Export to Workspace." When you choose that, matlab will give you a dialog box in which to specify the name of the variable that will hold the pixels of the image. For the moment, simply use A.
A(197,290,1), A(197,290,2), and
A(197,290,3), respectively.
You can check them out together by typing
> A(197,290,:)where the colon is used similar to the slice notation in Python. As a result, you can define a subimage:
B = A(274:342, 160:240, :)
and then use the imtool to show that: choose "File" -
"Import from Workspace" to do so.
You can also change the image pixels and then review the image. For example, running this code (in an m file is easiest, though not necessary)
for row = 274:1:342, % low:step:high
for col = 160:1:240,
A(row,col,:) = [0 255 0]; % red green blue
end
end
will change the spam in A
to a rectangle of green pixels.
In fact, you can take advantage of matlab's support for indexing into matrices. For example, you can accomplish the same change to the image (green pixels on spam) with the following three lines:
A(274:342,160:240,1) = 0; A(274:342,160:240,2) = 255; A(274:342,160:240,3) = 0;This takes advantage of the fact that when you assign a scalar to a vector or matrix, matlab automatically places that scalar into each component of the destination array. Unfortunately, the following code will not work (matlab isn't quite smart enough to figure out what you're trying to do):
A(274:342,160:240,3) = [0 255 0];but this one does work:
A(274:342,160:240,3) = 255;Reload the image A to see the difference.
Color Bands
Pixels are the starting point for most (but not all) image
interpretation.
To get comfortable at fiddling around with pixels,
try setting all of the green components of the image A
to zero to see the effect it will have.
(Remember, matlab is good at manipulating matrices.
To set all of the green components to 0, you don't need
to write a pair of nested for loops, you can just write
A(:,:,2) = 0. Try zeroing and saturating (setting to 255)
other color bands. Remember you can always reset A by
re-reading the image from file.
Colorspace conversion
Matlab has a built-in rgb2hsv function. Quoting
from Matlab's help system:
Matlab capitalizes the names of functions about which
it is providing help.
HSV = RGB2HSV(RGB) converts the RGB image RGB (3-D array) to the
equivalent HSV image HSV (3-D array).
Try it out!
Create an image named HSV that contains A's pixels in hsv
color space -- if you see lots of stuff printing out, you did not use the "print-suppressing"
semicolon. You will be able to show that image using the imtool
or matlab's alternative, imshow(H).
However, the resulting image will not be the hue-only, saturation-only, or
intensity-only image that is the next task for this assignment.
Once you've obtained and saved those three grayscale images, return to the spam-finding
on the
main assignment page .