Book Image

Visual Media Processing Using MATLAB Beginner's Guide

By : George Siogkas
Book Image

Visual Media Processing Using MATLAB Beginner's Guide

By: George Siogkas

Overview of this book

Whether you want to enhance your holiday photographs or make a professional banner image for your website, you need a software tool that offers you quick and easy ways to accomplish it. All-in-one tools tend to be rare, and Matlab is one of the best available.This book is a practical guide full of step-by-step examples and exercises that will enable you to use Matlab as a powerful, complete, and versatile alternative to traditional image and video processing software.You will start off by learning the very basics of grayscale image manipulation in Matlab to master how to analyze 3-dimensional images and videos using the same tool. The methods you learn here are explained and expanded upon so that you gradually reach a more advanced level in Matlab image and video processing. You will be guided through the steps of opening, transforming, and saving images, later to be mixed with advanced masking techniques both in grayscale and in color. More advanced examples of artistic image processing are also provided, like creating panoramic photographs or HDR images. The second part of the book covers video processing techniques and guides you through the processes of creating time-lapse videos from still images, and acquiring, filtering, and saving videos in Matlab. You will learn how to use many useful functions and tools that transform Matlab from a scientific software to a powerful and complete solution for your everyday image and video processing needs.
Table of Contents (18 chapters)
Visual Media Processing Using MATLAB Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – cropping and resizing an image, then saving it as BMP


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:

  1. Load the image using imread and save it in the variable img:

    >> img = imread('my_image.bmp');
  2. Crop the image using imcrop and save the result in the variable cropped:

    >> cropped = imcrop(img);
  3. Now, resize the resulting image to double its size using imresize and display it using imshow:

    >> cropped2 = imresize(cropped,2);
    >> imshow(cropped2);
  4. Now, let's see the sizes of the two images from steps 2 and 3 using size:

    >> 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 imwrite:

>> imwrite(cropped2,'cropped_image.bmp','BMP');

You should now be able to see the resulting BMP image in the Current Folder window.

What just happened?

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.

Have a go hero – tailoring an image to suit your needs

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.

You should start with the first and easiest step, which is loading and displaying our photograph into the Workspace window. The result would look something like the following screenshot:

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 imwrite.

Tip

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');

Pop quiz – image processing in MATLAB

Q1. MATLAB is a very good choice for image processing purposes for various reasons. Try to answer which of the following reasons are valid:

  1. MATLAB represents images as matrices and treats most variables as such.

  2. MATLAB is open source and can be used by anyone.

  3. MATLAB has a set of toolboxes that offer a variety of powerful and useful tools for image manipulation.

  4. Function imrotate rotates an image in a clockwise function.