Forgot Password?

Matlab Image Processing

Do you have a keen interest in Image Processing? Do you want to know how you can easily implement Image Processing tools and techniques using MATLAB and explore the limitless possibilities it has to offer? If yes, then read on..

Installation:

Installation is easy,  just run the CD and follow the steps that the installation wizard shows you. In case you come across some problem, here is a good webpage with detailed instructions for installation: http://itc.virginia.edu/research/matlab/install/installwin.html

Your first run

This is what you would generally see when you successfully install MATLAB and run it. The interface basically consists of 5 sections:

Note: If you are in the directory 'Matlab Codes' and it has the MATLAB files 'code1.m', 'code2.m' and 'code3.m', then you can run them by simply typing code1, code2 or code3 in the command window!

The command window is the place from where you tell MATLAB what is to be done! You have the option of either writing commands directly or running a 'MATLAB' file (with the extension .m) which would consist of commands, functions or programs.

 

MATLAB Help


MATLAB provides an excellent documentation of all its toolboxes and their respective commands. It also includes sample codes and examples! Go to the Help option and MATLAB and click on 'Product Help' (or simply press F1).

 

Explore the Help contents and you would find loads of useful things. Here is a snapshot of the first example in Image Processing toolbox. It teaches how to read and display an image.

Following this, there are nearly a hundred examples and sample codes of various commands and tools required in image processing. The following sections have been covered for the Image Processing toolbox:

 

Function Browser: a quick search tool!

Click on 'Help' on the toolbar and go to 'Function Browser' (or press Shift+F1). This is what will pop up:

The 'Function Browser', as the name suggests, is a quick search tool for description and synopsis of commands and functions of MATLAB. In the figure towards the right, I have searched for 'contrast' and it lists all the functions and commands related to that keyword.  It gives concise information about the function you are looking for. (For details and examples, you can always go to the 'Help and Documentation' by pressing F1!). It serves as a useful tool when you are stuck in the middle of a program and you want to quickly refer the syntax of a particular function.

 

Your journey begins..

So, now that you have a basic idea of working with MATLAB and its Image Processing toolbox, you are ready for the rich programming experience that MATLAB has to offer you! As beginners, make sure you refer to the 'Help' section (F1) and 'Function Browser' (Shift+F1) frequently as this will fasten your progress. The next section describes a basic Image Processing program that you can start off with:


Sample code 1: Scaling an image to 50% of original size

% Reading the image
img=imread(img.bmp','bmp');

% Removing alternate columns
img1 = img(:,1:2:size(img,2),:);

% Removing alternate rows
img2 = img1(1:2:size(img1,1),:,:);

% Viewing the new scaled down image
figure, imshow(img2);

Here is a very simple MATLAB code that scales down an image called 'img.jpg' to 50% of its original size.
 

Original Image

 

Final Image

Understanding the code:

Tip!!: Go to the function browser and search for the functions 'imread()' and 'imshow()'. You will find the descriptions of the functions as well as the various arguments and parameters they require.

img=imread('img.bmp','bmp'); is used to read the image. The imread() function takes two arguments: the name of the image and the type of the image.                               The variable 'img' would basically be NXMX3 matrix where N is the height of the image and M is the width. The three layers correspond to the Red, Blue and Green components.

Running the code:

Sample code 2: Comparing two images pixel by pixel

img1 = imread('img1.bmp','bmp');    % Read image 1
img2 = imread('img2.bmp','bmp');    % Read image 2

if size(img1)~=size(img2)
    disp('Images not same: Different sizes!');
    return;
end
 
for i=1:size(img1,1)
    for j=1:size(img1,2)
  for k=1:3
           if img1(i,j,k)~=img2(i,j,k)
               disp('Images not same: Different pixel values');
               return;
end
        end
    end
end
disp('Same Images !!');

 

SCRATCH YOUR HEAD!! : A further enhancement to this code could be specifying a threshold to the comparison, meaning that the RGB values to be matched in img2 could lie in a range around the value in img1. For example, if the value of a particular pixel in img1 is 133, 'threshold=2' would imply that all the corresponding value in img2 could have a value of 131, 132, 133, 134 or 135 for a successful match (i.e. (133-2) <= x <= (133+2)). Thus, threshold would define the 'degree' of similarity I want in the two images.

If the sizes of the two images are the same, then the images are compared pixel by pixel. If there is any difference, in the pixels, it would display: ''Images not same: Different pixel values '

Get started with these