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..
- MATLAB Installation
- Your first run
- MATLAB Help
- Function Browser: a quick search tool
- Your journey begins
- Sample Code 1: Scaling an image to 50% of original size
- Sample Code 2: Comparing two images pixel by pixel
- Get started with these..
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:

- The toolbar is where you can access the various features, tools and options regarding your code, debugging process, formatting, etc
- The file-manager shows your current directory and the files in it. You can change the directory by double clicking any folder or using the drop-down menu at the top.
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.
- When you write a new program or type in some commands, you would almost always, use variables to store the values obtained. The workspace shows you all your current variables and their values and allows you to edit, copy or duplicate them.
- The command history window saves your recent commands and helps you in keeping a track of what you have been up to! It comes out quite handy when you have to use long commands repeatedly: Simply double click on that command in the command history window and it will start running!
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);

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.- img1 = img(:,1:2:size(img,2),:); copies all the rows to the new matrix img1. But for columns, it copies only the alternate ones. This is done for all the three components: Red, Blue and Green. Note that size(img,2) returns the horizontal size of the image (length) whereas size( img,1) would have returned the vertical size (height) which is used in the next command.
- img2 = img1(1:2:size(img1,1),:,:); copies all the columns to the new matrix img2. But for rows, it copies only the alternate ones. This is done for all the three components: Red, Blue and Green.

- Now, that we have removed the alternate rows and alternate columns from the image, we view the image using the command figure, imshow(img2);
Running the code:
- Open an empty MATLAB file from the toolbar menu (or press Ctrl+N)
- Type in the code given in the textbox above
- Save the file with some name, say code1.m (note the .m extension)
- Click once on the command window, type code1 and press 'enter'.
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 !!');
- This code first checks whether the sizes of the two images is the same or not. If it is not the same, it displays 'Images not same: Different sizes!'
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 '- If the sizes are the same and every pixel in img1 is correspondingly equal to every pixel in img2, then the code displays: 'Same images!!'.
Get started with these