Download as pdf or txt
Download as pdf or txt
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 Processing Toolbox is a collection of functions that extend the capability of the MATLAB numeric computing environment. The toolbox supports a wide range of image processing operations, including: open image file, add noise of a given type (e.g salt &pepper ,Guassian, Speckels...) to intensity image , 2-D median filtering and adaptive filtering, Image analysis and enhancement, Color Image decomposition into RGB Channels, Image histogram, Image segmentation, image Multithresholding,, image movie , signal plotting and others , see Fig(2). Many of the toolbox functions are MATLAB M-files, a series of MATLAB statements that implement specialized image processing algorithms. You can view the MATLAB code for these functions using the statement : type function_name You can extend the capabilities of the Image Processing Toolbox by writing your own M-files, or by using the toolbox in combination with other toolboxes, such as the Signal Processing Toolbox and the Wavelet Toolbox. 1-Bit-Map Monochrome Image: in a monochrome (black/white) image, (like the example Fig (1 a) ), each pixel is stored as a single 0 or 1 value (bit). 2-A grayscale image: (like the example Fig (1 b)), usually requires that each pixel be stored as a value between 0 255 (byte), where the value represents the shade of gray of the pixel. The number of gray levels typically is an integer power of 2 (L=2K). The image formats are .Gif, .tif, .Jpeg, and .bmp.3- We also used color images (RGB images) and separated into their components (Red, Green, and Blue).

3- IMAGE TOOLBOX MENU


Fig(2) shows the main menu , how to open an image file , add noise and remove noises by filtering process and others functions.

3-1 DIGITAL FILTERS


Explore noise reduction in images (e.g salt &pepper ,Guassian, Speckels...) using linear and nonlinear filtering techniques [noise removal filter (e.g median, adaptive ,averaging,...the output image class is of as the input image class. We use IMNOISE function to Add noise to image.See Fig (3) % J = IMNOISE(I,TYPE,...) Add noise of a given TYPE to the intensity image I. TYPE is a string that can have

Our image processing toolbox is supported by a some complement of demo applications. These are very useful as templates for your own end-user applications, or for seeing how to use and combine your toolbox functions for powerful image analysis and enhancement in easy way.

one of these values: 'gaussian' Gaussian white noise with constant mean and variance. 'salt & pepper' "On and Off" pixels and finally 'speckle' Multiplicative noise. Then use function MEDFILT2 Perform 2-D median filtering., for example : % B = MEDFILT2(A,[M N]) performs median filtering of the matrix A in two dimensions. Each output pixel contains the median value in the M-by-N neighborhood around the corresponding pixel in the input image.WIENER2 Perform 2-D adaptive noise-removal filtering.WIENER2 lowpass filters an intensity image that has been degraded by constant power additive noise. WIENER2 (Adaptive filtering) uses a pixel-wise adaptive Wiener method based on statistics estimated from a local neighborhood of each pixel.

Image Segmentation is partition a digital image into disjoint connected sets of pixels, each of which corresponds to an object or region before we can analyze image content, identify or classify objects into different groups. Or Divides image into regions that are homogenous with respect to some criteria and adjacent regions differ with respect to the same criteria. Thus, region segmentation techniques can be grouped in three different classes :[1][3] 1st.Local techniques are based on the local properties of the pixels and their neighborhoods .2nd Global techniques segment an image on the basis of information obtain globally (e.g. by using image histogram; global texture properties ). 3rd Split, merge and growing techniques use both the notions of homogeneity and geometrical proximity in order to obtain good segmentation results. Finally image segmentation, a field of image analysis, is used to group pixels into regions to determine an images composition.

3-2 INTENSITY HISTOGRAM


In an image processing context, the histogram of an image normally refers to a histogram of the pixel intensity values. This histogram is a graph showing the number of 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.

4- TESTS AND RESULTS 1-Thresholding Function Test

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 g ( x, y ) = 0

if f ( x, y ) > T 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-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)

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'';',...

'line = 1;',... 'def = {''[3 3]''};',... 'glabel = inputdlg(prompt,title,line,def);',... 'filtered = medfilt2(img,[str2num(glabel{1,1})]);',... 'subplot(1,3,3);'... 'imshow(filtered);']); h25(2) = uimenu(h21,'Label','Adaptive',... 'callback',... ['prompt = {''Enter Filter Neighborhood:''};'... 'title = ''Median'';',... 'line = 1;',... 'def = {''[3 3]''};',... 'glabel = inputdlg(prompt,title,line,def);',... 'filtered = wiener2(img,[str2num(glabel{1,1})]);',... 'subplot(1,3,3);',... 'imshow(filtered);']); %Image Processing Box %open image file same as at start, then h27(1) = uimenu(h21, 'Label', 'Threshold choosing',... 'callback',... ['prompt = {''Enter Threshold:''};'... 'title = ''region labeling'';',... 'line = 1;',... 'def = {''50'' };',... 'glabel = inputdlg(prompt,title,line,def);',... 'bw = img > str2num(glabel{1,1});',... 'subplot(1,3,3);',... 'imshow(bw);']),... h27(2) = uimenu(h21,'Label','Intensity Adjustment & And Histogram Equalizatin',... 'callback',... ['IMADJDEMO;',... 'end;']); h27(3) = uimenu(h21,'Label','2-D Filtering And Filter Design',... 'callback',... ['FIRDEMO;',... 'end;']); In the same way , we can callback the following functions: imhist, thresholdf, 3Dim, Gaussian , watershed, read_show, sub_plot, im_crop, lettera,and plots signal data as defined below. h119 = uimenu(h0,'Label','seperate color image and FFT',... 'callback',... ['RGB;',... 'end;']); the end of main algorithm function RGB; [A,map,alpha] = imread('enhanc4a.Gif'); figure,imshow(A),map info = imfinfo('enhanc4a.Gif') [X,map] = imread('flowers.Tif',1); figure,imshow(X) title('Origin Flower color image'); figure(3) subplot(2,5,1); imshow(X) title('origin color image');

subplot(1,5,2) Red=X(:,:,1) imshow(Red) title('Red chanel'); subplot(1,5,3) Green=X(:,:,2); imshow(Green) title('Green chanel'); subplot(1,5,4); Blue=X(:,:,3); imshow(Blue) title('Blue chanel'); subplot(2,5,5) imhist(X) title('Blue chanel histogram'); %Fourier(fft2( and inverse Fourier(ifft2) transformations functions code A=imread('c:\lena1.gif') figure, imshow(A) title('Origin lena image'); b=fft2(A) figure,imshow(b) title('Using Fourier Transformation'); c=ifft2(b)% if you want to (rot90(b,2)) figure,imshow(c,[]) title('Using Inverse Fourier Transformation'); figure, imshow(c) %Display, scaling data to appropriate range. %to save the images in C:\ directory with name Lena.jpg imwrite(c,'C:\Lena7.jpg'); imwrite(b,'C:\Lena8.jpg'); %to collect all the images in one figure figure subplot(1,5,1) imshow(A) subplot(1,5,2) imshow(b) subplot(1,5,3) imshow(c,[]) subplot(1,5,4) imshow(c) %to show the size and the information of any image size(X) info = imfinfo('flowers.Tif') function thresholdf clear; % Threshold level parameter alfa: %[x,map]=gifread('lena.gif'): alfa=0.1;% less than 1/3 [x,map]=imread('lena2.Gif'); ix=ind2gray(x,map); I_max=max(max(ix)); I_min=min(min(ix)); level1=alfa*(I_max-I_min)+I_min; level2=2*level1; level3=3*level1;

thix1=max(ix,level1.*ones(size(ix))); thix2=max(ix,level2.*ones(size(ix))); thix3=max(ix,level3.*ones(size(ix))); figure(2);colormap(gray); subplot(2,2,1);imagesc(ix);title('lena'); subplot(2,2,2);imagesc(thix1);title('threshold one alfa'); subplot(2,2,3);imagesc(thix2);title('threshold two alfa'); subplot(2,2,4);imagesc(thix3);title('threshold three alfa'); function Guassian; %clear; % Parameters of the Gaussian filter: n1=10;sigma1=3;n2=10;sigma2=3;theta=0; % The amplitude of the noise: noise=0.1; [w,map]=imread('lena2.gif'); x=ind2gray(w,map); filter1=d2gauss(n1,sigma1,n2,sigma2,theta); x_rand=noise*randn(size(x)); y=x+x_rand; f1=conv2(x,filter1,'same'); rf1=conv2(y,filter1,'same'); figure(1); subplot(2,2,1);imagesc(x);title('lena'); subplot(2,2,2);imagesc(y);title('noisy lena'); subplot(2,2,3);imagesc(f1);title('smooth'); subplot(2,2,4);imagesc(rf1);title('noise cancel'); colormap(gray); % End of the main.m file % % The functions used in the main.m file %% % Function "d2gauss.m": % This function returns a 2D Gaussian filter with size n1*n2; theta is the angle that the filter rotated counter clockwise; and sigma1 and sigma2 are the standard deviation of the Gaussian functions. function h = d2gauss(n1,std1,n2,std2,theta); r=[cos(theta) -sin(theta); sin(theta) cos(theta)]; for i = 1 : n2 for j = 1 : n1 u = r * [j-(n1+1)/2 i-(n2+1)/2]'; h(i,j) = gauss(u(1),std1)*gauss(u(2),std2); end end h = h / sqrt(sum(sum(h.*h))); % Function "gauss.m": function y = gauss(x,std) y = exp(-x^2/(2*std^2)) / (std*sqrt(2*pi)); %%End of the functions %% *********************************** Function sub_plot filebase1='c:\image'; startfram1=1; endfram1=16; for j=0:3 figure; for i=startfram1:endfram1 filename1=[filebase1,'\image',num2str(i+j*16,'%2d'),'.tif'] [xi,mapi]=imread(filename1); subplot(4,4,i),imshow(xi,mapi) %title(filename1); end

end function read_show filebase='c:\image'; startfram=1; endfram=62; for i=startfram:endfram % filename=[filebase,num2str(i,'%2d'), '.tif'] filename=[filebase,'\image',num2str(i),'.tif'] figure(2); temp1=imread(filename,'tif'); imshow(temp1); end ************************************ Function IMREAD Read image from graphics file. [7] as follows A = IMREAD(FILENAME,FMT) reads the image in FILENAME into A. If the file contains a grayscale intensity image, A is a two-dimensional array. If the file contains a truecolor (RGB) image, A is a threedimensional (M-by-N-by-3) array. FILENAME is a string that specifies the name of the graphics file, and FMT is a string that specifies the format of the file. The file must be in the current directory or in a directory on the MATLAB path. If IMREAD cannot find a file named FILENAME, it looks for a file named FILENAME.FMT. The possible values for FMT include: 'jpg', 'tif', 'gif', 'bmp', 'png', 'hdf, 'pcx', 'xwd', 'cur', and 'ico' formats. [X, MAP] = IMREAD(FILENAME,FMT) reads the indexed image in FILENAME into X and its associated colormap into MAP. Colormap values in the image file are automatically rescaled into the range [0,1]. Data types: In most of the image file formats supported by IMREAD, pixels are stored using 8 or fewer bits per color plane. When reading such a file, the class of the output (A or X) is uint8. IMREAD also supports reading 16-bit-per-pixel data from BMP, TIFF and PNG files. For 16-bit TIFF and PNG image files, the class of the output (A or X) is uint16 and for 16-bit BMP image files, the class of the output is uint8.

6. CONCLUSION
1- Through the main program of our toolbox, it is easy to open and process different image file formats for different sizes such as BMP, GIF, and JPEG Images. 2- The toolbox shows easily the program codes with comments to be understood. 3- It is easy to run the toolbox (including the whole view of the menu items, figures and functions). It easy to run any function separately and it is possible to modify any function. 4- The toolbox provides us some image processing demos with their Matlab code such as Thrsholding , Gaussian , filtering , image segmentation, movie image from slicing images, read and show images and others, see Fig(2) .

REFERENCES
[1] Gonzalez and Woods, Digital image processing, 2nd Edition, prentice hall, 2002. [2] Bernd Jahne, Digital image processing concepts algorithms, and scientific applications, Springer-Verlag Berlin, Hidelberg 1991. [3] Kenneth R. Castelman, Digital image processing , Tsinghua Univ Press,2003. [4] http://www.mathworks.com [5] Stephen J. Chapman, Matlab Programming for Engineers, 2nd Edition, Brooks/Cole Thomson Learning, 2002. [6] I.Pitas, Digital Image Processing Algorithm and Applications. John Wiley &Sons, Inc.2002. [7] The MathWorks, inc. Mathlab, the language of technical computing version 6.1.0.450 Release 12.1, 2001.

(a)

(b)

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

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

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.

You might also like