You are on page 1of 8

NEW IMAGE PROCESSING TOOLBOX USING MATLAB CODES

DR NASSIR H. SALMAN
Zarqa Private Univ., College of Science and Information Technology-Computer Science Dept. Jordan
e-mail: naser@zpu.edu.jo

ABSTRACT
The idea is designing and programming a new image processing toolbox using Matlab codes. This toolbox is
used as an education tools to process digital images and help the students to understand how are the different image
processing functions work?; such as how the image for any format and size is opened?, how are the slice images
combining to get a movie image as a video camera did?, displaying the original images and their results after processing
in the same window for compression purposes , show image histogram, running watershed segmentation method,
enhancing, threshoding ,separate color image into its components , adding Gaussian and salt and fever noises to the
image then doing successive filtering process , and plotting any signal data profile. Also in our package, it is easy to
connect with all Matlab functions and using all Matlab dialog boxes designs. Finally all the functions in this toolbox are
collected and programmed using Matlab codes as we see through the text of this paper. The toolbox is easy to use in
image processing field.

Keywords: digital image processing toolbox , Filter, image enhance , Matlab codes image segmentation.
The easiest way to run an individual demo is to enter its
name at the MATLAB command prompt. But we built
1-INTRODUCTION toolbox (see Fig (2) supports a wide range of image
processing operations(as demos) as we mention above .
An image may be defined as a two –dimensional Also display the results in one window for each demo.
function, f(x, y), where x and y are spatial (plane) Finally; by our toolbox , we can link with any Matlab
coordinates, and the amplitude of f at any pair coordinates function using callback function (see the algorithm).The
(x, y) is called the intensity or gray level of the image at article has seven sections (1st is introduction,2nd image
that point. When x, y , and the amplitude values of f are all formats, 3rd image toolbox menu, 4th tests and results, 5th
finite, discrete quantities, we call the image a digital image. toolbox program, 6th conclusion ,and finally references .
So; a digital image is a two-dimensional array of small
square regions known as pixels. [1][2] 2- IMAGE FORMATS

What Is the Image Processing Toolbox?. The Image 1-Bit-Map Monochrome Image: in a monochrome
Processing Toolbox is a collection of functions that extend (black/white) image, (like the example Fig (1 a) ), each
the capability of the MATLAB® numeric computing pixel is stored as a single 0 or 1 value (bit).
environment. The toolbox supports a wide range of image 2-A grayscale image: (like the example Fig (1 b)), usually
processing operations, including: open image file, add requires that each pixel be stored as a value between 0 -
noise of a given type (e.g salt &pepper ,Guassian, 255 (byte), where the value represents the shade of gray of
Speckels...) to intensity image , 2-D median filtering and the pixel. The number of gray levels typically is an integer
adaptive filtering, Image analysis and enhancement, power of 2 (L=2K). The image formats are .Gif, .tif, .Jpeg,
Color Image decomposition into RGB Channels, Image and .bmp.3- We also used color images (RGB images) and
histogram, Image segmentation, image Multithresholding,, separated into their components (Red, Green, and Blue).
image movie , signal plotting and others , see Fig(2). Many
of the toolbox functions are MATLAB M-files, a series of 3- IMAGE TOOLBOX MENU
MATLAB statements that implement specialized image
processing algorithms. You can view the MATLAB code Fig(2) shows the main menu , how to open an image file ,
for these functions using the statement : type add noise and remove noises by filtering process and
function_name You can extend the capabilities of the others functions.
Image Processing Toolbox by writing your own M-files, or
by using the toolbox in combination with other toolboxes, 3-1 DIGITAL FILTERS
such as the Signal Processing Toolbox and the Wavelet
Toolbox. Explore noise reduction in images (e.g salt
&pepper ,Guassian, Speckels...) using linear and nonlinear
filtering techniques [noise removal filter (e.g median,
Our image processing toolbox is supported by a adaptive ,averaging,...the output image class is of as the
some complement of demo applications. These are very input image class. We use IMNOISE function to Add
useful as templates for your own end-user applications, or noise to image.See Fig (3)
for seeing how to use and combine your toolbox functions % J = IMNOISE(I,TYPE,...) Add noise of a given TYPE
for powerful image analysis and enhancement in easy way. to the intensity image I. TYPE is a string that can have

1
one of these values: 'gaussian' Gaussian white noise with Image Segmentation is partition a digital image into
constant mean and variance. 'salt & pepper' "On and Off" disjoint connected sets of pixels, each of which corresponds
pixels and finally 'speckle' Multiplicative noise. Then to an object or region before we can analyze image content,
use function MEDFILT2 Perform 2-D median filtering., identify or classify objects into different groups. Or Divides
for example : image into regions that are homogenous with respect to
% B = MEDFILT2(A,[M N]) performs median filtering some criteria and adjacent regions differ with respect to the
of the matrix A in two dimensions. Each output pixel same criteria. Thus, region segmentation techniques can be
contains the median value in the M-by-N neighborhood grouped in three different classes :[1][3]
around the corresponding pixel in the input
image.WIENER2 Perform 2-D adaptive noise-removal 1st.Local techniques are based on the local
filtering.WIENER2 lowpass filters an intensity image that properties of the pixels and their neighborhoods .2nd
has been degraded by constant power additive noise. Global techniques segment an image on the basis of
WIENER2 (Adaptive filtering) uses a pixel-wise adaptive information obtain globally (e.g. by using image
Wiener method based on statistics estimated from a local histogram; global texture properties ). 3rd Split, merge and
neighborhood of each pixel. growing techniques use both the notions of homogeneity
and geometrical proximity in order to obtain good
segmentation results. Finally image segmentation, a field
3-2 INTENSITY HISTOGRAM of image analysis, is used to group pixels into regions to
determine an image’s composition.
In an image processing context, the histogram of an
image normally refers to a histogram of the pixel intensity 4- TESTS AND RESULTS
values. This histogram is a graph showing the number of 1-Thresholding Function Test
pixels in an image at each different intensity value found
in that image. For an 8-bit grayscale image there are 256
different possible intensities, and so the histogram will
graphically display 256 numbers showing the distribution
of pixels amongst those grayscale values. See Fig (4) How
It Works: The operation is very simple. The image is
scanned in a single pass and a running count of the number
of pixels found at each intensity value is kept. This is then
used to construct a suitable histogram.

3-3-THRESHOLD TECHNIQUES

Thresholding is an operation that involves tests


against a function T of the form:

T = T(x, y, p(x, y), f(x, y))


where f(x, y) is the gray level of point (x,y) and p(x,y)
denoted some local property of this point- for example, the
average gray level of a neighborhood centered on (x,y).A
thresholded image g(x,y) is defined as:[1][3]
⎧1 if f ( x, y ) > T
g ( x, y ) = ⎨
⎩0 f f ( x, y ) ≤ T
Thus, pixels label 1(or any other gray level) correspond to
objects. Whereas pixels label 0 (or any other gray level not
assigned to objects) correspond to the background. When
T depends only on f(x,y) ( that is , only on gray level
values) the threshold is called global. If T depends on both
f(x,y) and p(x,y) , the threshold is called local. If, in
addition, T depends on the spatial coordinates x and y , the
threshold is called dynamic or adaptive.[2][6]. See Fig (5)
In our image toolbox we used image multithresholds and
Gaussian process. See the menu in Fig(2).

3-4 IMAGE SEGMENTATION

2
2-Guassian Function Test 3-Segmentation Function Test

4-Signal Data Plotting

5- Read-Show Function to get Movie Image of


62 slicing images (see page 4 left)

3
5- DIGITAL IMAGE TOOLBOX
PROGRAM
% started algorithm, [4] [5]
h0 = figure('MenuBar','none',...
'Name','Image Processing Program By Dr.Nassir H.
Salman 2008/Ver.1');
% open image file
h1 = uimenu(h0,'Label','&File');
h21 = uimenu(h1,'Label','&Open image',...
'callback',...
['[fname,fpath] = uigetfile(''*.tif'');',...
'if fname~=0;',...
'fname = strcat(fpath,fname);',...
'img = imread(fname);',...
'subplot(1,3,1);',...
'imshow(img);',...
'end;']);

%explore noise reduction in images (e.g salt


&pepper ,Guassian,Speckels...)
h21 = uimenu(h1,'Label','&Functions');
h24(1) = uimenu(h21,'Label','salt&pepper',...
'callback',...
['prompt = {''Enter Density:''};'...
'title = ''Salt & Pepper'';',...
'line = 1;',...
'def = {''0.1''};',...
'glabel = inputdlg(prompt,title,line,def);',...
'noisy = imnoise(img,''Salt &
Pepper'',str2num(glabel{1,1}));',...
' subplot(1,3,2);',...
'imshow(noisy);']),...
h24(2) = uimenu(h21,'Label','&Gaussian',...
'callback',...
['prompt = {''Enter Mean:'',''Enter Variance''};'...
'title = ''Gaussian'';',...
'line = 1;',...
'def = {''0.0'',''0.005''};',...
'glabel = inputdlg(prompt,title,line,def);',...
'noisy =
imnoise(img,''Gaussian'',str2num(glabel{1,1}),str2num(gla
bel{2,1}));',...
'subplot(1,3,2);',...
'imshow(noisy);']),...
h24(3) = uimenu(h21,'Label','Speckle',...
'callback',...
['prompt = {''Enter Variance:''};'...
'title = ''Speckle'';',...
'line = 1;',...
'def = {''0.1'' };',...
'glabel = inputdlg(prompt,title,line,def);',...
'noisy =
imnoise(img,''Speckle'',str2num(glabel{1,1}));',...
'subplot(1,3,2);',...
'imshow(noisy);']),...
%Noise Removal Filters e.g median,
%adaptive_
h21 = uimenu(h1,'Label','F&ilter');
h25(1) = uimenu(h21,'Label','Median',...
'callback',...
['prompt = {''Enter Filter Neighborhood:''};'...
'title = ''Median'';',...

4
'line = 1;',... subplot(1,5,2)
'def = {''[3 3]''};',... Red=X(:,:,1)
'glabel = inputdlg(prompt,title,line,def);',... imshow(Red)
'filtered = medfilt2(img,[str2num(glabel{1,1})]);',... title('Red chanel');
'subplot(1,3,3);'...
'imshow(filtered);']); subplot(1,5,3)
h25(2) = uimenu(h21,'Label','Adaptive',... Green=X(:,:,2);
'callback',... imshow(Green)
['prompt = {''Enter Filter Neighborhood:''};'... title('Green chanel');
'title = ''Median'';',... subplot(1,5,4);
'line = 1;',... Blue=X(:,:,3);
'def = {''[3 3]''};',... imshow(Blue)
'glabel = inputdlg(prompt,title,line,def);',... title('Blue chanel');
'filtered = wiener2(img,[str2num(glabel{1,1})]);',... subplot(2,5,5)
'subplot(1,3,3);',... imhist(X)
'imshow(filtered);']); title('Blue chanel histogram');

%Image Processing Box %Fourier(fft2( and inverse Fourier(ifft2) transformations


%open image file same as at start, then functions code
h27(1) = uimenu(h21, 'Label', 'Threshold choosing',... A=imread('c:\lena1.gif')
'callback',...
['prompt = {''Enter Threshold:''};'... figure, imshow(A)
'title = ''region labeling'';',... title('Origin lena image');
'line = 1;',... b=fft2(A)
'def = {''50'' };',...
'glabel = inputdlg(prompt,title,line,def);',... figure,imshow(b)
'bw = img > str2num(glabel{1,1});',... title('Using Fourier Transformation');
'subplot(1,3,3);',...
'imshow(bw);']),... c=ifft2(b)% if you want to (rot90(b,2))
h27(2) = uimenu(h21,'Label','Intensity Adjustment & figure,imshow(c,[])
And Histogram Equalizatin',... title('Using Inverse Fourier Transformation');
'callback',...
['IMADJDEMO;',... figure, imshow(c)
'end;']); %Display, scaling data to appropriate range.
h27(3) = uimenu(h21,'Label','2-D Filtering And Filter %to save the images in C:\ directory with name Lena.jpg
Design',... imwrite(c,'C:\Lena7.jpg');
'callback',... imwrite(b,'C:\Lena8.jpg');
['FIRDEMO;',...
'end;']); %to collect all the images in one figure
In the same way , we can callback the following functions: figure
imhist, thresholdf, 3Dim, Gaussian , watershed, read_show, subplot(1,5,1)
sub_plot, im_crop, lettera,and plots signal data as defined imshow(A)
below. subplot(1,5,2)
imshow(b)
h119 = uimenu(h0,'Label','seperate color image and subplot(1,5,3)
FFT',... imshow(c,[])
'callback',... subplot(1,5,4)
['RGB;',... imshow(c)
'end;']); the end of main algorithm %to show the size and the information of any image
function RGB; size(X)
[A,map,alpha] = imread('enhanc4a.Gif'); info = imfinfo('flowers.Tif')
function thresholdf
figure,imshow(A),map clear;
info = imfinfo('enhanc4a.Gif') % Threshold level parameter alfa:
[X,map] = imread('flowers.Tif',1); %[x,map]=gifread('lena.gif'):
figure,imshow(X) alfa=0.1;% less than 1/3
title('Origin Flower color image'); [x,map]=imread('lena2.Gif');
ix=ind2gray(x,map);
figure(3) I_max=max(max(ix));
subplot(2,5,1); I_min=min(min(ix));
imshow(X) level1=alfa*(I_max-I_min)+I_min;
title('origin color image'); level2=2*level1;
level3=3*level1;

5
thix1=max(ix,level1.*ones(size(ix))); end
thix2=max(ix,level2.*ones(size(ix))); function read_show
thix3=max(ix,level3.*ones(size(ix))); filebase='c:\image';
figure(2);colormap(gray); startfram=1;
subplot(2,2,1);imagesc(ix);title('lena'); endfram=62;
subplot(2,2,2);imagesc(thix1);title('threshold one alfa'); for i=startfram:endfram
subplot(2,2,3);imagesc(thix2);title('threshold two alfa'); % filename=[filebase,num2str(i,'%2d'), '.tif']
subplot(2,2,4);imagesc(thix3);title('threshold three alfa'); filename=[filebase,'\image',num2str(i),'.tif']
function Guassian; figure(2);
%clear; temp1=imread(filename,'tif');
% Parameters of the Gaussian filter: imshow(temp1);
n1=10;sigma1=3;n2=10;sigma2=3;theta=0; end
% The amplitude of the noise: ************************************
noise=0.1; Function IMREAD Read image from graphics file. [7] as
[w,map]=imread('lena2.gif'); follows
x=ind2gray(w,map); A = IMREAD(FILENAME,FMT) reads the image in
filter1=d2gauss(n1,sigma1,n2,sigma2,theta); FILENAME into A. If the file contains a grayscale
x_rand=noise*randn(size(x)); intensity image, A is a two-dimensional array. If the file
y=x+x_rand; contains a truecolor (RGB) image, A is a three-
f1=conv2(x,filter1,'same'); dimensional (M-by-N-by-3) array. FILENAME is a string
rf1=conv2(y,filter1,'same'); that specifies the name of the graphics file, and FMT is a
figure(1); string that specifies the format of the file. The file must be
subplot(2,2,1);imagesc(x);title('lena'); in the current directory or in a directory on the MATLAB
subplot(2,2,2);imagesc(y);title('noisy lena'); path. If IMREAD cannot find a file named FILENAME, it
subplot(2,2,3);imagesc(f1);title('smooth'); looks for a file named FILENAME.FMT. The possible
subplot(2,2,4);imagesc(rf1);title('noise cancel'); values for FMT include: 'jpg', 'tif', 'gif', 'bmp', 'png', 'hdf,
colormap(gray); 'pcx', 'xwd', 'cur', and 'ico' formats.
% End of the main.m file %
% The functions used in the main.m file %% [X, MAP] = IMREAD(FILENAME,FMT) reads the
% Function "d2gauss.m": indexed image in FILENAME into X and its associated
% This function returns a 2D Gaussian filter with size colormap into MAP. Colormap values in the image file
n1*n2; theta is the angle that the filter rotated counter are automatically rescaled into the range [0,1].
clockwise; and sigma1 and sigma2 are the standard
deviation of the Gaussian functions. Data types: In most of the image file formats supported
function h = d2gauss(n1,std1,n2,std2,theta); by IMREAD, pixels are stored using 8 or fewer bits per
r=[cos(theta) -sin(theta); color plane. When reading such a file, the class of the
sin(theta) cos(theta)]; output (A or X) is uint8. IMREAD also supports reading
for i = 1 : n2 16-bit-per-pixel data from BMP, TIFF and PNG files. For
for j = 1 : n1 16-bit TIFF and PNG image files, the class of the output
u = r * [j-(n1+1)/2 i-(n2+1)/2]'; (A or X) is uint16 and for 16-bit BMP image files, the
h(i,j) = gauss(u(1),std1)*gauss(u(2),std2); class of the output is uint8.
end
end
h = h / sqrt(sum(sum(h.*h)));
6. CONCLUSION
1- Through the main program of our toolbox, it is easy to
% Function "gauss.m":
function y = gauss(x,std) open and process different image file formats for different
y = exp(-x^2/(2*std^2)) / (std*sqrt(2*pi)); sizes such as BMP, GIF, and JPEG Images.
%%End of the functions %%
*********************************** 2- The toolbox shows easily the program codes with
comments to be understood.
Function sub_plot
filebase1='c:\image';
startfram1=1; 3- It is easy to run the toolbox (including the whole view
of the menu items, figures and functions). It easy to run
endfram1=16;
for j=0:3 any function separately and it is possible to modify any
figure; function.
for i=startfram1:endfram1
4- The toolbox provides us some image processing demos
filename1=[filebase1,'\image',num2str(i+j*16,'%2d'),'.tif'] with their Matlab code such as Thrsholding , Gaussian ,
filtering , image segmentation, movie image from slicing
[xi,mapi]=imread(filename1);
subplot(4,4,i),imshow(xi,mapi) images, read and show images and others, see Fig(2) .
%title(filename1);
end

6
REFERENCES
[1] Gonzalez and Woods, Digital image processing, 2nd
Edition, prentice hall, 2002. [5] Stephen J. Chapman, Matlab Programming for Engineers,
2nd Edition, Brooks/Cole Thomson Learning, 2002.
[2] Bernd Jahne, Digital image processing concepts
algorithms, and scientific applications, Springer-Verlag [6] I.Pitas, Digital Image Processing Algorithm and
Berlin, Hidelberg 1991. Applications. John Wiley &Sons, Inc.2002.

[3] Kenneth R. Castelman, Digital image processing , [7] The MathWorks, inc. Mathlab, the language of technical
Tsinghua Univ Press,2003. computing version 6.1.0.450 Release 12.1, 2001.

[4] http://www.mathworks.com

(a) (b)
Figure.(1) a) Bit-Map Monochrome Image, b) A Grayscale Image

Figure(2) Our Image Processing Toolbox Menu & Some Built Functions

7
Origin image add noise remove the noise using filter

Figure (3) Filter Operation

Figure (4) Lena Image And Its : Histogram

Figure(5) :A) Gray-Level Histogram That Can Be Partitioned By (A) A Single Threshold, And (B) Multiple Threshold.