Tuesday, March 27, 2012

[Lab02] Image Acquisition and Basic Operation

Image Acquisition

Take any picture of anything you like by using a digital camera and transfer the picture from your camera to your computer. The image should be able to view it by standard image viewing software such as Windows Picture and Fax Viewer).



Read the image into MATLAB

>> x = imread (‘obama.png’);



Display the Information

>> whos x
Name      Size          Bytes     Class    Attributes
     x         278x200x3     166800    uint8             

The class should be “unit8” and the 3rd dimension of size of x should be 3 which denote it is a colour image which correspond the three components which are Red, Green and Blue channels.


Red       x(:,:,1)
Green     x(:,:,2)
Blue      x(:,:,1)

The first colon denotes all rows, the second colon all columns, and the third index denotes the color (1 for red, 2 for green and 3 for blue).


Display the image    

>> imshow(x)




Convert Color Image to Grayscale


>>x_g = rgb2gray(x);
>>figure, imshow(x_g)

rgb2gray command converts the truecolor image RGB to the grayscale intensity image. rgb2gray converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance.




Display R, G or B Channel of the Image

There are many ways to display an image in certain channel (red, green or blue channel). You can try either:

  • clear or set the other channels to 0
  • using colormap


For the first option, we need to assign a different variable for the image so that we do not overwrite the original image. As for the example below, we assigned image x to three different variables (x_green, x_red and x_blue for green, red and blue channel respectively).


Red
>>x_red = x;
>>x_red(:,:,2) = 0;
>>x_red(:,:,3) = 0;
Green
>>x_green = x;
>>x_green(:,:,1) = 0;
>>x_green(:,:,3) = 0;
Blue
>>x_blue = x;
>>x_blue(:,:,1) = 0;
>>x_blue(:,:,2) = 0;



Take note that (:,:,1) represent red channel, (:,:,2) represents green channel and (:,:,3) represents blue channel . So, if we want to display the red channel, we must set the green and blue channel to 0 and vice versa.

For the second option, we need to extract each of these layers by using their respective indices (Red = 1, Green = 2, Blue = 3).

>>img_r = I(:,:,1);
>>image(img_r), colorbar;



The image above shows red channel of image x. By default, the colorbar has a scale of 0 to 64. It is difficult to use this rainbow-colored scale. The color intensity for full-color images ranges is from 0 to 255. Thus, we need to modify the scale by using colormap. This can be done as below.

Red
>>R = x(:,:,1);
>>figure;
>>image(R),colormap([[0:1/255:1]',zeros(256,1),zeros(256,1)]), colorbar;

Green
>>G = x(:,:,2);
>>figure;
>>image(G),colormap([zeros(256,1),[0:1/255:1]',zeros(256,1)]), colorbar;

Blue
>>B = x(:,:,3);
>>figure;
>>image(B),colormap([zeros(256,1),zeros(256,1),[0:1/255:1]']),colorbar;




Transpose an Image

To transpose the image, use imshow command followed by the variable name with apostrophe ( ).

>>imshow(x_g');




Crop an Image

To crop an image, use imcrop command followed by the variable name and the size of the crop rectangle, rect. rect is a four-element position vector [xmin ymin width height] that specifies the size and position of the crop rectangle.

>> cropImg = imcrop(x_g,[1 1 100 100]);
>> figure, imshow (cropImg);




Flip an Image

To flip an image, use flipdim command followed by the variable and number of 1 (vertically) or 2 (horizontally)

>>upsidedown=flipdim(x_g,1);
>>figure,imshow(upsidedown);



>>left2right=flipdim(x_g,2);
>>figure,imshow(left2right);

.

[Lab01] Image Processing Toolbox



Reminder: This blog ONLY covers basics that are related to Image Processing. For further information, you are recommended to read the MATLAB Getting Started documentation. Make sure that your MATLAB has been installed Image Processing Toolbox. For information about installing the toolbox, see the MATLAB Installation Guide.



Read an Image
To read an image, use the imread command followed by the name of the image with its extension as an argument. For example below, we are assigning an image named Porsche.tiff to a variable, I:


>>I = imread('Porsche.tiff');


Display an Image
To display the image that we have read, use the imshow command followed by the variable as an argument. A popup window will appeared with the image stored in the variable. Example below will display the image stored in variable I.

>>imshow(I);



Check the Image in Memory
To check how the files are stored in memory, use whos command. As for this case, only I is only stored in the memory, so MATLAB responds with the result of I. These files also can be easily seen in the workspace on the right panel of your MATLAB (this might be different with you MATLAB interface. If you do not have workspace panel, go to Desktop >> Workspace)


>>whos

Name       Size         Bytes     Class    Attributes

I         342x546       186732    uint8             




Perform Histogram Equalization
To see the distribution of intensities of the image stored in I, use imhist command followed by the variable I as the argument. For example below, we use figure command to open new figure window and display the histogram.


>>figure, imhist(I)


To spread the intensity values over the full range, use histeq command followed by the variable as the argument. In result, the image will be more contrast. This command is useful if the histogram produced by imhist command gives narrow intensity range.




>>I2 = histeq(I)
>>figure, imshow(I2)







Write the Image
To write the image I2, the image I that we have adjusted, in simple words, we would like to save the image to the disk, use imwrite command followed by the argument consists of the variable and the name and the extension that you want the image saved as. For example below, we would like to save the I2 image as Porsche2.png.

>>imwrite (I2, Porsche2.png’)




Check the Contents of the File
To check the contents of the file, use imfinfo command followed by the name of the file with its extension as the argument. The example below checks the content the newly written file.

>>imfinfo(‘Porsche2.png’)
ans =

                  Filename: [1x100 char]
               FileModDate: '13-Mar-2012 16:50:08'
                  FileSize: 71980
                    Format: 'png'
             FormatVersion: []
                     Width: 546
                    Height: 342
                  BitDepth: 8
                 ColorType: 'grayscale'
           FormatSignature: [137 80 78 71 13 10 26 10]
                  Colormap: []
                 Histogram: []
             InterlaceType: 'none'
              Transparency: 'none'
    SimpleTransparencyData: []
           BackgroundColor: []
           RenderingIntent: []
            Chromaticities: []
                     Gamma: []
               XResolution: []
               YResolution: []
            ResolutionUnit: []
                   XOffset: []
                   YOffset: []
                OffsetUnit: []
           SignificantBits: []
              ImageModTime: '13 Mar 2012 08:50:07 +0000'
                     Title: []
                    Author: []
               Description: []
                 Copyright: []
              CreationTime: []
                  Software: []
                Disclaimer: []
                   Warning: []
                    Source: []
                   Comment: []
                 OtherText: []

Tuesday, March 20, 2012

Simple Operation for Matrices


    Addition 


>> A+B


ans =

6     8    10    12


>> C+D
ans =

5

7

9
  Subtraction


>> A-B
ans =

-4    -4    -4    -4







>> B-A
ans =

4     4     4     4


>> C-D
ans =

-3

-3

-3
>> D-C
ans =

3

3

3
  Multiplication


>> 2*A
ans =

2     4     6     8

>> A*3
ans =

3     6     9    12


>> 2*D
ans =

8

10

12
>> D*4
ans =

16

20

24
  Division


>> B/4
ans =

1.2500    1.5000    1.7500    2.0000


>> C/2
ans =

0.5000

1.0000

1.5000
  Sum of all elements
  in matrix


>> sum(A)
ans =

10


>> sum(D)
ans=

15
  Mean


>> mean(A)
ans =

2.5000


>> mean(D)
ans =

5
  Maximum element
  in matrix


>> max(A)
ans =

4


>> max(D)
ans =

6
  Minumum element
  in matrix 


>> min(A)
ans =

1   


>> min(D)


ans =

4

Tuesday, March 6, 2012

Scalar, Vector and Matrices

SCALAR, VECTOR AND MATRICES

Scalar
 Real Numbers a = 5

Vector


 A collection of numbers arranged in :
  • column (column vector)
  • row (row vector)

 column vector : 
 row vector :


Matrix


 Ordered set of scalars arranged in a  rectangular pattern.


 A=
 


  • Scalar:
>> a = 5 
a =
     5


  • Row vector:
>> b = [1 2 3] 
b =
     1     2     3


  • Column vector:
>> c = [1;2;3] 
c =
1


2


3


  • Matrix:
>> A = [1 2 3; 4 5 6; 7 8 9] 
A =
1     2     3


4     5     6


7     8     9


  • To display number of rows and column of the matrix:
>> size(A) 
ans =
     3     3


>> [row, column] = size(A)
row =
     3
column =
     3 
>> [row, column] = size(c)
row =
     3
column =
     1


  • To list the elements in certain row or column
>> C = A(3 , :) 
C =
     7     8     9


>> D = A(: , 3) 
D =
3


6


9