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


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:

  1. Use fliplr and flipud in the Command Window to perform left-right and up-down flipping of your image:

    >> img_lr = fliplr(img);
    >> img_ud = flipud(img);
  2. 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:

What just happened?

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

Have a go hero – using flipdim and comparing the results

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_lr and img_ud in your Workspace window from the previous process, you should first perform image flipping using the flipdim command.

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.

Resizing an image

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

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

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

Tip

Note that if you want to find out more about a function and the different inputs it can accept, you can use the command line help of MATLAB by typing in the word help and the name of the function you want to investigate. You can try this by typing help imresize.

Cropping an image

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

Tip

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_min and y_max. In that case, the second input of imcrop would be [y_minx_miny_max-y_minx_max-x_min].

Saving an image

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

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:

>> imwrite(img,'new_image.jpg','JPEG');

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.

If we assume that we want to save our image as JPEG, but with a quality factor of 100, lossy compression, and the comment Packt embedded in the JPEG, we should type in:

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