Image Processing in MATLAB | Fundamental Operations

1. Reading Images

Images are read into the MATLAB Environment using imread() function which takes filename with applicable extention as argument
For Example:

>> I=imread('nature.jpg');

This will read JPEG image ‘nature’ into the image array.



Note: The semicolon(;) at the end of command line is used to suppress the output in MATLAB.If ‘;’ is not used at the end, it will show the output of the specified operation.

2.Displaying Images

imshow() function is used to display images in MATLAB. The basic syntax of imshow() is

   imshow(f, G);

Here f is image matrix and G is number of intensity level used to display the image.Second Argument in the above syntax is optional. If G is omitted its value defaults to 256 levels.

When we use the syntax

  imshow(f, [Low, High]);

It displays all value less than or equal to ‘Low’ as black and all values greater than or equal to ‘High’ as white. The values between ‘Low’ and ‘High’ are displayed as the intermediate intensity value using the default number of levels.

Examples:

Showing Grayscale Images

>> imshow(f);

This will display the grayscale image f.
Also, we can write

>> imshow(f, [90, 180]);

It will display all value less than or equal to 90 as black and all values greater than or equal to 180 as white. The values between 90 and 180 are displayed as the intermediate intensity value using the default number of levels.

Showing Binary images

>> imshow(BW);

It displays the binary image BW. It displays pixels with the value 0 (zero) as black and pixels with the value 1 as white.


Showing RGB images

>> imshow(f);

It displays the RGB image f.

3. Writing images to disk

Images are written to disk using imwrite() function. The basic syntax of imwrite() is

imwrite(f, 'filename');

Here f is our image and filename is the name of file including a recognized file format extension. Alternatively we can also specify desired format explicitly with a third argument.

For example

>> imwrite(f, 'nature.jpg');

Alternatively we can write it as,

>> imwrite(f, 'nature', 'jpg');

The above commands write image f to a filename nature with extension jpg.

Note: The imwrite() function have some optional parameters too, depending upon file format.
For example:

>> imwrite(f, 'filename', 'quality', q); 

The above syntax is applicable for only JPEG images.Here q is an integer and can take value between 0 to 100.Lower the value of q higher the degradation due to JPEG compression.

Some Other Important functions


i.) imfinfo

It displays the details of an image file
Example:

>> imfinfo nature.jpg;

Output:

          Filename: 'nature.jpg'
        FileModDate: '23-Jun-2016 09:57:04'
           FileSize: 238290
             Format: 'jpg'
      FormatVersion: ''
              Width: 1920
             Height: 1200
           BitDepth: 24
          ColorType: 'truecolor'
    FormatSignature: ''
    NumberOfSamples: 3
       CodingMethod: 'Huffman'
      CodingProcess: 'Sequential'
            Comment: {'CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), quality = 92?'} 

ii.) size()

size() function take image matrix as an argument and give the row and column dimension of an image f.This function is used to determine the size of an image automatically.
Example :

>> size(f)

Output :

ans =
  1920 1200

Here 1920 is the number of rows and 1200 is the number of columns in image f.

Note : As we know RGB images are represented using 3-D matrix in MATLAB. So, if f is a RGB image the above function will produce output as

ans =
  1920 1200 3

iii.) whos f

This function will show additional detail about image matrix f.
Example :

whos f

Output :

 Name        Size                Bytes    Class    Attributes
  f         300x400x3            360000   uint8   


My Personal Notes arrow_drop_up

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.