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 – rotating an image and displaying the result


Let's start rotating and displaying the image. Assuming you have solved the problem of the image being in a visible path, you should follow these steps:

  1. Open the image. Let's use the previous one:

    >> img = imread('my_image.bmp');
  2. 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);
  3. 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:

What just happened?

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

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.

Tip

If you want to display your images in different windows, you should replace each subplot(m,n,r) command with figure. This way, you would end up with four open windows for the example illustrated previously.

Performing image mirroring

In order to perform image mirroring, we will use one of the following functions: fliplr, flipud, and 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.