Lab (4) - Image Processing

Image Interpolation
Image Rotation



Image Interpolation
As we discussed in class, sometimes  when we rotate or resize an image the resulting image does not necessary have the same size as the original image.  In such cases, we need to use an interpolation technique to fix the problem.  The Image Processing Toolkit in MATLAB uses two functions, imresize and imrotate to do the resize and rotate operations.  The interpolation process is to sample the image to determine the values between defined values (basically to determine the missing values using the know values).  An example of such interpolation, is the case of an image that is enlarged.  In that case the interpolation procedure has to come up with the extra pixel values.  MATLAB provides three different interpolation techniques:
        Nearest neighbor interpolation
        Bilinear Interpolation, and
        Bicubic interpolation

All these techniques work fundamentally similar.  The idea is to use the known points and use a weighted average to find the missing values.  These methods are different in the way that they choose the neighboring pixels.
 

As a rule of tumb, the more pixels that is considered, the more accurate the result will be.  However, the larger number of pixels results in higher complexity of the computation.

The interpolation functions in MATLAB takes an argument that specifies the type of interpolation.  By default, the interpolation method is nearest neighbor method.  This method works well for all types of images and particularly is the appropriate method for indexed images.    For intensity images or RGB images, the bilinear or bicubic work better.
 
Following is an example of how the enlargement was done on the sample image (1).

Original image (16x16)
 

Enlarged by 2 (32x32)

Part (1) :
Create the directory lab4 under your cs3531 directory and copy the bw1.jpg from teacher's directory:

% cp ~rt/3531_s01/codes/bw1.jpg .   (don't forget the .)

This is the same 16x16 image that is shown above.  The intensity values in that image are:

P2
#
16 16
255
0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0

Suppose we want to resize this image (double the size) using the default technique (nearest neighbor interpolation).  Before we do this, let's have a look at the image and see what is going to happen.  The current image is a 16x16 image.  We want to convert that to a 32x32 image.  That means, we need to generate 16 extra pixels on the rows and 16 pixels on the columns.  The extra pixels will be inserted between the existing pixels.  The new image data will be:

% matlab
>> I = imread('bw1.jpg');
>> imshow(I);
>> Y=imresize(I,2);
>> imshow(Y);
>> Y

 P2
# Created by Paint Shop Pro 6
32 32
255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

This procedure works well for this image.  We could also use a different notation to resize the image.  In the new technique, we determine the desired size.

>> Z = imresize(I,[32,32]);
>> imshow(Z);

That results into the above image as well.  Here is a quick summary on imresize.  More information can be found using help imresize.

 IMRESIZE Resize image.
    IMRESIZE resizes an image of any type using the specified interpolation method. Supported interpolation methods
    include:
 
         'nearest'  (default) nearest neighbor interpolation
          'bilinear' bilinear interpolation
          'bicubic'  bicubic interpolation
 
 B = IMRESIZE(A,M,METHOD) returns an image that is M times the size of A. If M is between 0 and 1.0, B is smaller than A. If
 M is greater than 1.0, B is larger than A. If METHOD is omitted, IMRESIZE uses nearest neighbor interpolation.

Now lets use the bilinear interpolation to resize the image and see the effect.

>> Z = imresize(I,2, 'bilinear');

Can you explain what the differences are and how the new values have been calculated ?

>> Q = imresize(I,2, 'bicubic');

Can you explain what the differences are and how the new values have been calculated ?

Image Rotation
Suppose we want to rotate the sample image above.  If this image is assumed to be in the x-y plane, then rotation is done for an angle T around the z axis.

The MATLAB command for rotation is called imrotate.  Here is a summary on that command.

B = IMROTATE(A,ANGLE,METHOD) rotates the image A by ANGLE degrees in a counter-clockwise direction, using the specified interpolation method. METHOD is one of the interpolation methods.

If the angle is 90, 180, 270, and 360, the resulting image will be the same size of the original image.  But there is a problem with the rotation when the angle is not divisible by 90.  For instance when the angle is 45.

Let's try some examples:

>> B = imrotate(I,  90);
>> imshow(B);

This results into the following image:

with the intensity values shown below:

P2
# Created by Paint Shop Pro 6
16 16
255
255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0
255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255
0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255

A rotation by 45 degree will result into the following image:

with the intensity values shown below:

You can clearly see that in order to fit the image in the new frame the size of rotated image is no longer 16.   The new size is 23 pixels.  One simple calculation reveals that the diagonal of the original image is sqrt(16^2 + 16^2) = 22.63.  The imrotate function adds zero's to fill the new image.

In rotating an image, we will use interpolation.  We can rotate the above image using bilinear or bicubic interpolation as well.

>> B = imrotate(I,90, 'bilinear');
>> imshow(B);

or
>>B = imrotate(I,90, 'bicubic');
>> imshow(B);

Do you see a difference ?

The imrotate has an option for cropping the input image to the same size as the input image.

>> C = imrotate(I, 45, 'bilinear', 'crop');
>> imshow(C);