Preface
Digital visual media has, undoubtedly, become a vital part of our everyday lives. Analog means of storing and processing information have gradually faded and are nowadays used either by aficionados of analog media, or for very specialized applications. Capturing and storing image or video information have rapidly become common, fast, and cheap processes, since almost everyone can have access to a digital electronic device that can be used for these aims, whether it is a photographic or video camera, or even a mobile phone. The outburst of visual media-capturing devices has led to an increase of amateur photographers and weekend filmmakers, who often have a problem deciding what software to use to process their stored images or videos. The rule of thumb is that free software solutions often have limited functionalities or are very complicated, while commercial solutions tend to be very expensive and sometimes do not provide all the functionalities that a user would hope for.
This book presents a rather uncommon alternative solution that might not be considered by users who only need an image, or a video editing software, but could certainly appeal to users who are also students, scientists, or just have easy access to the multifunctional, high level programming environment, called MATLAB.
What this book covers
Chapter 1, Basic Image Manipulations, introduces you to the environment of MATLAB and takes you on a tour to its basic tools and functionalities. Then, image importing and displaying in MATLAB is discussed, followed by a demonstration of the MATLAB GUI for image manipulation. Basic image transformations are covered, such as rotating/flipping, resizing, and cropping an image. Finally, different ways of writing an image are presented. The chapter includes hands-on examples that tie most of the processes covered, together.
Chapter 2, Working with Pixels in Grayscale Images, is based on examples of pixel-based processing of an image. Several classic processes for image enhancement are discussed, such as thresholding, local, or global contrast enhancement. The methods presented use several techniques that gently introduce you to the secrets of MATLAB programming. A practical example in image enhancement concludes this chapter.
Chapter 3, Morphological Operations and Object Analysis, introduces the basic methods of morphological image analysis. In it, you will learn of ways to perform binarization of a grayscale image using the thresholding methods. Edge detection and other morphological operators are presented and explained, so that you learn how to select and manipulate particular image regions that interest you the most. You will also learn the techniques that automatically detect corners, circles, and lines in an image. Several hands-on examples will vividly demonstrate all these techniques.
Chapter 4, Working with Color Images, extends previous methods to color images. Some of the processes mentioned for grayscale are now revisited for color image processing. Different color spaces and their advantages are explained with examples on color enhancement in MATLAB. You will learn how illumination and color can be separated and processed independently. The technique for color isolation is explained through a practical example and finally, some of the methods mentioned previously are used to teach you how to develop a popular application: red eye correction in your photographs.
Chapter 5, 2-Dimensional Image Filtering, dives into some more complex issues for image filtering, such as deblurring and sharpening of images. You will get to work on more sophisticated techniques for image denoising. Some more interesting and fun examples will let you start enjoying your experience more deeply. We will work on ways to apply some of the filter locally, to enhance or blur specific image regions.
Chapter 6, Mixing Images for Science or Art, will wake up the artist, or the scientist in you. You will learn the techniques that mix channels of multispectral images for scientific visualization. Then, we will present fun, hands-on examples for blending, or stitching images, to produce artistic results. We will also work on ways to create artistic HDR (High Dynamic Range) images in MATLAB. Finally, we will present a simple way to create panoramic images.
Chapter 7, Adding Motion – From Static Images to Digital Videos, introduces you to video processing by building on the previous knowledge you have acquired. The fact, that videos can be generated by static images, will help you to better comprehend basic ideas. So, after covering the basics of video frame processing in MATLAB and demonstrating how we can load and play back videos, we will show how to create a video from static images. The construction of a time-lapse video is the basic hands-on example we will be working on in this chapter.
Chapter 8, Acquiring and Processing Videos, demonstrates the functionalities of the image acquisition tool for MATLAB. You will be given step-by-step examples on ways to shoot video with your camera and use your computer as a Digital Video Recorder, using the special GUI tool contained in MATLAB. Video compression and basic color video processing techniques are also demonstrated in this chapter, accompanied by a discussion on performance issues.
Chapter 9, Spatiotemporal Video Processing, introduces you to command line manipulation and processing of videos. After covering basic video frames manipulations in MATLAB, you will learn how to deinterlace videos, using intra-frame, inter-frame, or mixed techniques. Furthermore, spatiotemporal video filtering is presented, with hands-on examples to help you get the idea.
Chapter 10, From Beginner to Expert – Handling Motion and 3-D, introduces you to methods of motion detection in videos. Building on basic knowledge, we will get to the point of creating a simple surveillance system in MATLAB. You will also be taught the basics of estimating motion using popular optical flow algorithms, included in one of the toolboxes of MATLAB. You will also be introduced to feature-based image registration for motion compensation. The working example for this will be video stabilization. Finally, we will introduce an example of three-dimensional video and cover a very basic and fun example of turning a regular video to a 3-D one.
What you need for this book
In order to practice what you read in this book, you should have access to a computer with an installed version of MATLAB. The screenshots you will see in this book are all taken from MATLAB Version R2012b, which was the most recent one at the time of writing this book. However, since MATLAB is also a programming language, you will not need to worry about any differences in the way R2012b looks compared to earlier versions. The great majority of the things we will cover in this book will be 100 percent compatible with most previous versions. In the rare cases, when we use a brand new functionality, we will also provide alternative solutions for previous versions.
The most important thing to make sure of, however, is that you have to find an installed version of MATLAB that includes at least the two basic toolboxes for image and video processing: Image Processing Toolbox and Image Acquisition Toolbox. An extra toolbox, named Computer Vision Toolbox, will also be used for a very small part of video processing. Toolboxes are collections of ready-made functions for special purposes. For those of you with a little familiarity with programming, they could be thought of as libraries. The more toolboxes included in your installation of MATLAB, the more functionalities the environment will provide for you. Most of our code in this book will be based on basic MATLAB functions included in all installations and the two toolboxes that were mentioned previously.
Who this book is for
This guide to visual media processing using MATLAB will be very useful to a beginner programmer who has little or no knowledge of the environment, but would like to use it as an alternative, or possibly, substitute solution to common image and video editors. The only thing that you will need to have before starting this book is a basic prior knowledge of image and video processing to grasp the material covered more easily. Also, some basic programming experience could come in handy, but is not necessary, since most parts of the book start from scratch.
Conventions
In this book, you will find several headings appearing frequently.
To give clear instructions of how to complete a procedure or task, we use:
Time for action – heading
- Action 1
- Action 2
- Action 3
Instructions often need some extra explanation so that they make sense, so they are followed with:
What just happened?
This heading explains the working of tasks or instructions that you have just completed.
You will also find some other learning aids in the book, including:
Pop quiz – heading
These are short multiple-choice questions intended to help you test your own understanding.
Have a go hero – heading
These practical challenges give you ideas for experimenting with what you have learned.
You will also find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: “Adapthisteq
performs global histogram equalization.”
A block of code is set as follows:
function [output] = CroppedContrastEnhancement(input,method) % Function that performs area-based image contrast enhancement with % methodsincorporated in MATLAB toolboxes % Inputs: % input - Input image % method - Enhancement method (1: histeq, 2: imadjust, % 3: adapthisteq) % Output: % output - Output image (with enhanced contrast)
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
img = imread(‘my_image.bmp’); % Read image subplot (1,2,1) % Open a figure for 2 images imshow(img) % Show original image title (‘Original image’) % Add title threshold = 150; % Set threshold level img(img > threshold) = 255; % Set pixels above 150 to 255 img(img <= threshold) = 0; % Set pixels below 150 to 0 img = logical(img); % Convert img to binary subplot (1,2,2) % Make second image spot active imshow(img) % Show thresholded image title (‘Thresholded image’) % Add title
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: “You should start with the first and easiest step, which is loading and displaying our photograph into the Workspace window.”.
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <feedback@packtpub.com>
, and mention the book title through the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
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.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <copyright@packtpub.com>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
Questions
You can contact us at <questions@packtpub.com>
if you are having a problem with any aspect of the book, and we will do our best to address it.