Since it was first released, MATLAB has been associated with technical computing and scientific programming. Due to its high uptake in academia and its large and active community, it has grown to become a versatile and multifunctional tool, providing solutions in a vast diversity of fields. Its usage in image and video processing for scientific applications has been popular for a while, but more recent versions have included processing tools that are more user-friendly and aimed at a broader spectrum of users. The usefulness of MATLAB for image processing is rather self-explanatory, since it is a programming environment specialized for matrix manipulation and images are nothing more than matrices.
In this chapter, you will be introduced to the very basics of image manipulation using MATLAB. Prior experience in using MATLAB is not required, since we will be covering everything from scratch. Some basic understanding of programming would be beneficial.
More specifically, in this chapter we will cover:
The basic details of the MATLAB environment and especially those that will be used extensively in this book
The various ways of loading, displaying, and saving an image using MATLAB
The most basic image manipulations that can be handled by MATLAB, that is, rotation, cropping, and resizing
So, let's get started.
In order to be able to start working with MATLAB, you should install it on your system. Since the installation is a very straightforward process, we will not cover it here. The only thing you have to be certain of, is that your installation includes the Image Processing and Image Acquisition toolboxes, which are necessary for the purposes of this book. A few examples towards the end will also need the Computer Vision System Toolbox in order to work. The version of MATLAB we will be using is 2012b, the latest available version at the time of writing this book.
The functions and processes covered in this book have also been tested in MATLAB 2013a during the revision process, and work without any problems.
The first thing we see when opening MATLAB 2012b is a window comprising other windows and a ribbon with various toolbars. The ribbon has grouped sets of many processes that can be helpful for a beginner, but may prove distracting when the user has acquired more experience. This is why, besides giving a few details in this chapter, we will be avoiding extensive use of the ribbon, limiting it to the most time-saving and relevant processes to digital media processing. The subwindows residing in the environment of MATLAB will be our basic tools in this book, along with the MATLAB editor, which is the basic core tool for writing your own scripts and functions.
The Command Window
The Workspace window
The Command History window
The Current Folder window
The Details window
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
It is worth noting that the environment of MATLAB 2012b has been altered significantly compared to previous versions, mainly because of the inclusion of a ribbon menu resembling the one used in Microsoft Office 2007 onward. However, since most of the processes described in this book are based on using the command line, users of previous versions will not have problems following the examples.
Most of the MATLAB usage time will probably be spent on typing commands in the command line. The command line resides in the Command Window and starts with the symbol fx which is clickable and contains a list of all installed functions organized by toolbox. Following this symbol, there is the prompt symbol, >>, followed by a blinking cursor at the place where you can enter commands. All codes that must be written in the command line will be preceded by the prompt symbol in this book.
The available commands are all the inherent MATLAB functions, most of which help you manipulate matrices (hence the name: MATrixLABoratory). In this book, we will mainly be using functions for image manipulation, included in the Image Processing and Image Acquisition toolboxes.
The Current Folder window is basically a file manager, resembling Windows Explorer. You can navigate your way through the folders in your computer, in order to find files you would like to use in your work, for example, an image you would like to load. By default, MATLAB can access the contents of the current folder and a number of folders that have been included in its path. During installation, all the folders containing the installed toolboxes have been added to MATLAB's path; hence the functions and files contained in them can be accessed no matter what the current folder is.
The Details window is also informative, displaying information about the file you have selected in the current folder window. The details are displayed only when the selected file is recognized by MATLAB.
The ribbon contains a collection of basic MATLAB processes and resides in the top part of the environment window. It is based on the latest Graphical User Interface trends, like the ones used in Microsoft Office software products since the 2007 version. It has three main tabs:
These three tabs are briefly described in the following sections.
As we can see in the previous image, there are six main groups of processes present on the HOME tab; FILE, VARIABLE, CODE, SIMULINK, ENVIRONMENT, and RESOURCES.
From these six groups, we will certainly need the FILE group processes to create or open MATLAB files. The VARIABLE group is a new addition in MATLAB 2012b, providing an intuitive, yet unnecessary way for variable creation. The group named CODE is useful for code analysis, the SIMULINK group is used for a tool that is not covered in this book, the ENVIRONMENT group is a way to tweak the setting of the MATLAB environment, and finally, RESOURCES are processes for getting online and offline help.
The PLOTS tab is a tool that helps you plot variables. Besides traditional plotting capabilities (for example, creating a graph from x and y coordinates), it can also be used as an alternative way to show images stored in variables or even to play videos stored in variables.
The APPS tab is also a rather new addition and provides a quick way to access basic MATLAB tools by clicking on their icons, rather than writing their name in the command line. We can use it to quickly access the Image Acquisition or the Image Viewer tools, which are useful for digital media processing.
A very important tool in MATLAB, besides the default ones you see when first opening the environment, is the editor. The editor can be invoked in various ways, depending on what you want to accomplish. The three basic ways are:
Click on the New Script icon on the HOME tab
Click on the Open icon on the HOME tab and then either search for a file in your computer with a
.mextension (MATLAB code) by clicking on Open…, or selecting a file from the RECENT FILES list
Once you have invoked the editor using any of the ways mentioned previously, a new window will pop up. In this window, you can write, alter, and save your code using a powerful code editor. We will not go into detail here, since to fully comprehend the functionalities of this tool, we first have to learn how to write code in MATLAB.
Now that we have seen most of the menus and windows we will be using, let's start with the very basics of image processing. MATLAB is a computing language that works with matrices. Consequently, in order for us to work with images, they have to be imported as matrix variables in MATLAB. There are several ways to accomplish this. Here, we will see the most practical ones.
In the command line, you can type commands that invoke the functions that have been installed with MATLAB. Functions can be thought of as black boxes, which can be fed with appropriate inputs and provide appropriate outputs.
The MATLAB function that can be used to import images is
imread. The easiest way to use it is to type it into the command line, passing the path to an image as the input string. This will import the image into a variable in the MATLAB workspace. The name of the variable is defined by you, by assigning a name to the output. Once you have done this, you can then display your image by using
imshow. This function is designed to display a matrix variable as an image. Let's see now how you can import and display your first image using the command line.
Let's assume that you have an image called
my_image.bmp, which you want to import and then display in MATLAB. The steps you should follow are these:
Ensure that your current folder (its contents are shown in the Current Folder window) contains the image. If not, either copy it there or select the folder that contains it.
Click in the Command Window area to be able to write something in the command line. Once you see a blinking cursor in the command line, you are ready to type.
Type the following commands in the command line:
>> img = imread('my_image.bmp'); >> imshow(img)
A window displaying the selected image must now be open. You can maximize, minimize, or adjust its size according to your liking.
The steps you followed in the previous section used two predefined MATLAB functions, one for opening an image (
imread) and one for displaying it (
imshow). Step 1 is a prerequisite for
imread to find the file. An alternative to navigating to the folder containing the image would be to include the full path to the filename given as input. If, for example, the folder containing the image was
C:\images\ ,then the first command would be:
>> img = imread('C:\images\my_image.bmp');
imread stores the image in the variable
img which is a matrix of size equal to that of the image.
The image used in the previous example, as well as all images and videos used throughout this book, can be found at the author's website. Its URL is http://www.cvrlab.com/gsiogkas/.
Using the command line is not the only way to open and display images. MATLAB also provides a basic image processing tool, called
imtool. This tool can be invoked by typing its name in the command line, or by clicking on its icon on the ribbon in the APPS tab. If you select to invoke
imtool, a new window which includes basic image manipulation choices, opens. To open an image in
imtool, we must click on File and then Open… to browse through the folders and load the image of our choice. Let's work with the same image as before, namely,
my_image.bmp. Once the image is open in
imtool, we can see and access most available functionalities of the tool on its toolbar. Starting from left to right, the icons appearing in the toolbar can be used to:
- Navigate the image using a smaller, overview window
- Inspect pixel values in a neighborhood of your choice
- Display image information
- Adjust image contrast
- Get Help.
- Crop the image
- Measure distances
- Zoom in/out
- Drag image to pan
- Adjust contrast/brightness via mouse motion
Change the scale of the displayed image
Now that we know what the
imtool can do, let's use it to get some more information about our image. Suppose that we want to know the color, depth, and size of our image, the values of the pixels around an area we want to inspect, and the distances (in pixels) of some pairs of points on the image, we will have to take the following steps:
Click on the third icon from the left, , that displays information about the image. You will see that the new window that appears contains a lot of information about the image, including its width, height, and bit depth.
- To make your task easier, you should first zoom in using the magnifying glass with the plus symbol (). Let's zoom in on the area containing the moving track (top of the image).
Then, click on and place the blue crosshair that appears on the image on the track. You will now be able to see the values of the pixels in the small area selected by your crosshair. If you enlarge the selected area by dragging its sides, the values will not be visible, but you will still be able to inspect details in the image.
- Now let's measure the dimensions of the track that we have zoomed in on. Zoom in as much as you like to see the details and then click on .
To measure the length between two points, you should left-click on the first point and keeping the left mouse button clicked, move the cursor to the second point and release the mouse button. This will draw a line between the two points you selected and place a label on it, displaying its length in pixels. Let's repeat this process to measure the dimensions of the truck.
Now that you have measured the distances you needed, you might want to save the resulting image with the overlaid distance measurements. Click on File and then Print to Figure, in order to display your processed image in a new window and then navigate to File | Save As to get a chance to select the name and type of the image you want to save.
The process described previously is generally useful for people who work with image enhancement and analysis. In image processing, you will often need to inspect an image in terms of color values or measured in terms of distance, and so on. The analysis of the image regions should be easily extractable so that the results can be passed on to others. These functionalities are covered by
imtool. In our example, we used the image information icon, which gave us an idea on what values to expect (8 bit depth means values from 0 to 255) and what the image dimensions are. Then, we located and inspected the region of our choice using the zoom in/out and the pixel values inspection tools. When we decided on the actual part of the image we wanted to measure, we used the distance measurement tool to see the dimensions of the selected object on screen. Finally, we exported our enhanced results to a new image of our chosen format, using the Print to Figure functionality.
By now you have already mastered how to open an image and display it as it is using the Command Window. Now, it is time to learn how to apply basic geometric transformations to an image and display them along with the original. Geometric transformations are probably the most common functionalities of every image editor, no matter how basic it is. They do not generally alter the content of an image, but actually change the grid of pixels so that processes, such as image rotation or mirroring are achieved. Cropping and resizing of images are also two basic geometric transformations. In this section, we will see how all these transformations can be achieved in MATLAB.
For image rotation, you can use the function
imrotate. Again, you will be working in the Command Window, where you will have to type the functions to perform the transformations. Only this time, we will use a few more lines of code, to display the results in a single window.
Open the image. Let's use the previous one:
>> img = imread('my_image.bmp');
Now, rotate the image using
imrotate. Let's try rotating the image by 90, 180, and 270 degrees, storing each result in a different variable:
>> img90 = imrotate(img,90); >> img180 = imrotate(img,180); >> img270 = imrotate(img,270);
Hopefully, you have typed all commands correctly, so now you should be able to see four different matrices in the Workspace window, each one containing a rotated version of the original image. Now let's try to display all the images in the same window. In the Command Window, type:
>> figure >> subplot(2,2,1) >> imshow(img) >> title('Original Image') >> subplot(2,2,2) >> imshow(img90) >> title('Image Rotated 90 degrees') >> subplot(2,2,3) >> imshow(img180) >> title('Image Rotated 180 degrees') >> subplot(2,2,4) >> imshow(img270) >> title('Image Rotated 270 degrees')
You should now be able to see a window displaying the original (not rotated or rotated by 0 degrees) image and its three aforementioned rotated versions, like the following screenshot:
The process you just performed can be split into two parts; the first part is the image rotation process, which took place in step 2, producing three rotated versions of your original image. The function
imrotate can take two inputs, an image and an angle by which the image will be rotated. The angle need not necessarily be a multiple of 90. It could be any arbitrary angle, between -360 and 360 degrees. Also note that the rotation is performed in a counter-clockwise fashion. If you wish to perform clockwise rotation, you should use a negative angle as a second input to
The second part of the process described previously, is for displaying the produced images. The
subplot command splits the window that opens into
m rows and
n columns and then activates the subwindow that resides in the r-th position. Following this notation, the subplot function should be called as
subplot(m,n,r). In our example, we used two rows and two columns; hence the first two inputs were equal to two. The third input was changed each time, so that the images displayed after each subplot (using
imshow) would be placed in a different subwindow. Another thing worth noting is that the subwindows in a subplot are numbered in a column-wise fashion (that is, 1 is for the first row-first column, 2 is for the first row-second column, and so on). Finally, for clarity of the displayed information,we have added a title over each displayed image, using the
title function, with the message we want entered as a string input.
In order to perform image mirroring, we will use one of the following functions:
flipdim. If you want to mirror a grayscale image, the first two functions can be used. The first one,
fliplr, is used to mirror an image about its vertical axis. This means that the columns of the image will be interchanged, so that the first column becomes the last and vice versa. Accordingly,
flipud can be used to mirror an image about the horizontal axis. These two functions only work when the input matrix is 2-dimensional (that is, a grayscale image). When we have to deal with color images, we need to use
flipdim because it can also accept a second input declaring the dimension that will be flipped.
Now let's try to perform image mirroring in both dimensions using the first two functions described previously. Again, we will use the same grayscale image as before. If you have started from scratch repeat step 1 from the previous example. Then, take the following steps:
flipudin the Command Window to perform left-right and up-down flipping of your image:
>> img_lr = fliplr(img); >> img_ud = flipud(img);
Now, display the original image and the mirrored versions in different windows, using:
>> figure, imshow(img) >> figure, imshow(img_lr) >> figure, imshow(img_ud)
Now you should see three different windows displaying the original image and its two mirrored versions. Each window that appears will be placed on top of the previous one, so you should drag-and-drop them next to each other in order to get a result as follows:
The process you just followed is mostly self-explanatory. You have used the same grayscale image as before, stored in the matrix variable
img, and then used the flipping functions of MATLAB to perform mirroring about the two axes. Then, at step 2, you executed the command to show the images in different windows. Notice that you can write more than one command in the same line, provided that they are separated either by a comma (in which case, any result generated by the command will be printed in the Command Window) or a semicolon (nothing will be printed in the Command Window).
Now let's try to use the alternative function, which can also be used for color images. How would you use
flipdim to produce the same results as in the previous example and then display all results in one window with respective titles?
This is actually a simple process, involving some of the steps described previously. Assuming you still have
img_ud in your Workspace window from the previous process, you should first perform image flipping using the
The result you will get is a window containing the left-right flipped images on the first line and the up-down flipped images on the second line. If you have done everything correctly, the two images in the first row should be identical and so should remove the two images in the second row.
A very common functionality of any self-respecting image editor is that of image resizing. MATLAB is no different, since it provides the user with resizing capabilities, using popular algorithms. More specifically, MATLAB's Image Processing Toolbox incorporates the
imresize function, which accepts at least two inputs. The first input is the matrix variable containing the image you want to resize and the second input is either a scaling factor (by which the original image size will be multiplied) or a matrix with two elements; a number of rows and a number of columns for the resized image. As an example, let's assume we have a grayscale image of 240 rows and 320 columns stored in the matrix
A. If we wanted to resize it to half its original size, that is 120 rows and 160 columns and assign the result to matrix
B, then we would have the following two equivalent ways of accomplishing that through the command line:
>> B = imresize(A,0.5); >> B = imresize(A,[120 160]);
Note that the default resizing algorithm prior to MATLAB Version R2007a was different. So, if you want to replicate results generated with earlier versions, you should use the function
The result in both cases would be exactly the same, but there is also a third method of acquiring it. Let's suppose that we want to resize image A, so that it fits vertically into a predefined space, which we know consists of 120 pixels. In that case, we wouldn't need to know its exact number of columns and instead of the commands we used previously, we could use:
>> B = imresize(A,[120,NaN]);
Here, we have to say a few words about how MATLAB performs image resizing. The default method is cubic interpolation, but you can also use nearest neighbor or bilinear interpolation. Other valid choices could be interpolation kernels, but they go beyond our scope here. In order to specify a different interpolation method, you should add a third input in your function call. For example, if in the previous example you wanted to use bilinear interpolation, you should type in:
>> B = imresize(A,[120,NaN],'bilinear');
Another useful tool incorporated in image editors is image cropping. In its most typical form, it consists of a manual tool for defining and placing a rectangular area; this process produces a new image that contains only the part of the original image that lies in the rectangle. Assuming you have loaded and displayed an image using
imshow in the command line, you can crop it and place the results in a new matrix (let's call it cropped), by typing:
>> cropped = imcrop;
Once you do that, you will have to use the mouse to define the rectangular area to be cropped, by clicking on the left mouse button and keeping it pressed while moving the mouse, until you are happy with the resulting rectangle. Once you let go off the left mouse button, you are able to adjust its position and/or size, double-click on the left mouse button when the result is acceptable. This process stores the part of the image residing in the rectangle into matrix cropped, which will have the same dimensions as the rectangle.
Another way to crop an image would be to define the rectangle by using specific coordinates. This often happens when you know the exact area you want to crop beforehand, so you can define them as a second input to
imcrop. Let's suppose that the upper left corner of the rectangular area of image A you want to crop is on pixel (x, y), where
x is the row number and
y is the column number. If the rectangle has a width of
w pixels and a height of
h pixels, you should type in the command line:
>> cropped = imcrop(A,[y,x,w,h]);
If you think that the way the MATLAB handles rectangle coordinates is impractical, you should try coming up with a way to adjust it to your needs. Suppose you want to choose a rectangle that starts at row
x_min, ends at row
x_max, and is bounded by the columns
y_max. In that case, the second input of
imcrop would be
Up to now, we have learned how to perform several image manipulations in MATLAB, but we haven't seen how the results can be saved using the Command Window. In fact, the solution is rather intuitive. Since almost everything we have seen so far had to do with calling functions with rather self-explanatory, such as
imread, writing an image is rather unsurprisingly called
Of course, like any self-respecting image processing software, MATLAB gives you a wide variety of choices regarding the type of the image you want to save. In fact, it supports most of the known image formats, such as JPEG, BMP, PGM, PNG, and TIFF. The most common way of using
imwrite is by feeding it three inputs. For example, if we need to save an image we have stored in matrix variable
img, as a JPEG image of the same size, we should use:
This command would result in saving a JPEG image named
new_image.jpg, using the default quality factor. The user has the ability to choose a different quality factor, since the matter of compression is a very important one in image processing. The higher the quality factor (it may be any integer from 0 to 100) defined by the user, the less is the image degradation caused by compression. When saving a JPEG image, the user can also define the color bit depth (8, 12, or 16 for grayscale and 8 or 12 for color images), the mode of compression (lossy or lossless), and a possible comment that might have to be saved in the JPEG. By default, the saved image will be 8 bits if grayscale (8 bits/color channel, if color) with lossy compression, quality of 75, and with no comments embedded.
>> imwrite(img,'new_image.jpg','JPEG','Quality',100,'Comment','Packt') ;
As you might have understood by now, passing optional inputs in a function is a rather straightforward process, provided that you know what these inputs are called and what their supported values are (that is, what values can be accepted).
To get a better grasp of the three functions we presented in the previous three sections, we can use the example of a very common image processing drill, which is to select and crop a part of an image we would like to keep and then resize it to our desired dimensions and save it as a new image. We'll use our previous image as a start, so our first move is to load it. The whole process is described in the following steps:
Load the image using
imreadand save it in the variable
>> img = imread('my_image.bmp');
>> cropped = imcrop(img);
>> cropped2 = imresize(cropped,2); >> imshow(cropped2);
Now, let's see the sizes of the two images from steps 2 and 3 using
>> size(cropped) ans= 114 196 >> size(cropped2) ans= 228 392
As we see, the sizes are OK (
cropped2 is double the size of
cropped, 114 x 196 versus 228 x 392). Now, we can save our final image as a BMP using
Well, you just performed one of the most common processes in the everyday life of an amateur photographer. You started by loading your image into the workspace of MATLAB in step 1, proceeded with selecting and cropping a rectangular area of your choice in step 2, went on to resize it to double its original size and check the resulting image size, and finally, saved your cropped and resized result to a BMP image file. Now, you are ready to move on to harder tasks.
Let's now suppose that we would like to process a photo from our holidays, by rotating it 90 degrees to have the proper orientations, cropping a specific area we want to keep, rotating this area as much as needed so that it is not crooked, and then resize it to 360 rows (say, we want to fit it in a specific space with 360 pixels of height). At the end, we would like to save our result in high quality JPEG, embedding a comment that reads
I just finished Chapter 1. How would you accomplish all these things?
First of all, don't panic! All these steps have already been covered in this chapter, so it's just a matter of using the right functions in the right order.
Now that your photo has been saved in a variable, you can rotate it so that it has the correct orientation. Then, use
imcrop and select the sunny area of the image with the mouse.
However, the resulting image is tilted! So, let's try to fix it. Here comes the trial and error process. You must try the various possible small angle values in
imrotate, so that you find the result you are happy with. This image should look fine if you rotate it clockwise by an angle of about 5 degrees. Optionally, we can display our steps in the same figure and get the following result:
As you can see from the last image, while our final image is not tilted, it has black borders caused by the rotation by an angle that is not a multiple of 90. This means that you need yet another cropping step, to keep the image part that we actually want. When you have edited the image to your liking, proceed in resizing the image using
imresize, and declare the number of rows you want the image to have in the second input. Finally, you can save the image in the predefined format using
The default method for rotation used in
imrotate is nearest neighbor. This method produces rather suboptimal results, when rotating an image by an angle that is not a multiple of 90 degrees. If you want to produce a better result, you could try entering a different method as a third input, like this:
>> img90c2 = imrotate(img90c,-5, 'bicubic');
Q1. MATLAB is a very good choice for image processing purposes for various reasons. Try to answer which of the following reasons are valid:
MATLAB represents images as matrices and treats most variables as such.
MATLAB is open source and can be used by anyone.
MATLAB has a set of toolboxes that offer a variety of powerful and useful tools for image manipulation.
imrotaterotates an image in a clockwise function.
This chapter was a quick dive into the ways that MATLAB can replace your everyday image editing tool, while giving you extra parameterization choices that you wouldn't have in basic software. More specifically, you have learned the basic ways to:
Load and display an image in MATLAB using the command line
Load, display, manipulate, and save an image using
Rotate, flip, or mirror an image in the command line
Crop and resize an image using functions
Save an image in a variety of formats
These processes are core functionalities of everyday image manipulation for every amateur photographer. They provide the foundations for any complex image processing task and will be used throughout the book. So, congratulations! You have set the first stepping stone to climb to more sophisticated image processing tasks. The rest of the chapters will guide you through some more complex image processing that MATLAB offers and will then move on to video processing. Depending on your needs, you will either be able to use it as a quick reference for any of the techniques it covers, or you can read through the chapters in a sequential order, as you would do in a Media Processing course.
The next chapter will introduce you to different ways to work with grayscale image pixels and manipulate their values. On finishing it, you will be able to enhance and improve the visual quality of an image. Have fun!