You are on page 1of 6

# MODULE NO.

8 TITLE:

/ / / /5 /5 /

PROFESSOR

## Module 8 Matlab Image Processing Prepared by Engr. AU Cabatit

page 1 of

SIGNLAB Signals, Spectra and SignalProcessing Experiment Title Module MATLAB IMAGE PROCESSING
Objectives: By the end of the session the students should be: y familiar with image processing, image representation, reading and writing images in matlab environment. y Some basic operations, linear filters, nonlinear filters Introduction: I. Image representation. There are five types of images in MATLAB: 1. Grayscale. A grayscale image M pixels tall and N pixels wide is represented as a matrix of double datatype of size MN. Element values (e.g., MyImage(m,n)) denote the pixel grayscale intensities in [0,1] with 0=black and 1=white. 2. Truecolor RGB. A truecolor red-green-blue (RGB) image is represented as a three-dimensional MN3 double matrix. Each pixel has red, green, blue components along the third dimension with values in [0,1], for example, the color components of pixel (m,n) are MyImage(m,n,1) = red, MyImage(m,n,2) = green, MyImage(m,n,3) = blue. 3. Indexed. Indexed (paletted) images are represented with an index matrix of size MN and a colormap matrix of size K3. The colormap holds all colors used in the image and the index matrix represents the pixels by referring to colors in the colormap. For example, if the 22nd color is magenta MyColormap(22,:) = [1,0,1], then MyImage(m,n) = 22 is a magenta-colored pixel. 4. Binary. A binary image is represented by an MN logical matrix where pixel values are 1 (true) or 0 (false). 5. uint8. This type uses less memory and some operations compute faster than with double types. For simplicity, this tutorial does not discuss uint8 further. Note: Grayscale is usually the preferred format for image processing. In cases requiring color, an RGB color image can be decomposed and handled as three separate grayscale images. Indexed images must be converted to grayscale or RGB for most operations. II. Reading and writing image files MATLAB can read and write images with the imread and imwrite commands. Matlab supports several image formats and to check the list type imformats on the command window. >> imformats
EXT ISA INFO READ WRITE ALPHA DESCRIPTION -------------------------------------------------------------------------------------bmp isbmp imbmpinfo readbmp writebmp 0 Windows Bitmap (BMP) gif isgif imgifinfo readgif writegif 0 Graphics Interchange Format (GIF) jpg jpeg isjpg imjpginfo readjpg writejpg 0 Joint Photographic Experts Group (JPEG) pbm ispbm impnminfo readpnm writepnm 0 Portable Bitmap (PBM) pcx ispcx impcxinfo readpcx writepcx 0 Windows Paintbrush (PCX) pgm ispgm impnminfo readpnm writepnm 0 Portable Graymap (PGM) png ispng impnginfo readpng writepng 1 Portable Network Graphics (PNG) pnm ispnm impnminfo readpnm writepnm 0 Portable Any Map (PNM) ppm isppm impnminfo readpnm writepnm 0 Portable Pixmap (PPM)
ECE DEPARTMENT 9 FEU EAST ASIA COLLEGE Version 1.0. 2009 Module 8 Matlab Image Processing Prepared by Engr. AU Cabatit page 2 of

...

When reading images, an unfortunate problem is that imread returns the image data in uint8 datatype, which must be converted to double and rescaled before use. Instead of calling imread directly, create the following Mfile function to read and convert images:
function Img = getimage(Filename) %GETIMAGE % % % Read an image given a filename The image is V = GETIMAGE(FILENAME) where FILENAME is an image file.

returned either as an MxN double matrix for a grayscale image or as an MxNx3 double matrix for a color image, with elements in [0,1].

## Img = reshape(cat(3,Map(Img,1),Map(Img,2),Map(Img,3)),size(Img,1),size(Img,2),3); else Img = Img/255; End % Rescale to [0,1]

Linear filters Linear filtering is the cornerstone technique of signal processing. To briefly introduce, a linear filter is an operation where at every pixel xm,n of an image, a linear function is evaluated on the pixel and its neighbors to compute a new pixel value ym,n.

## A linear filter in two dimensions has the general form ym,n =

j k

hj,k xm

j,n k

where x is the input, y is the output, and h is the filter impulse response. Different choices of h lead to filters that smooth, sharpen, and detect edges, to name a few applications. The right-hand side of the above equation is denoted concisely as h x and is called the convolution of h and x. Exercises: % To convert or lead an image to matrix that matlab can read
ECE DEPARTMENT 9 FEU EAST ASIA COLLEGE Version 1.0. 2009 Module 8 Matlab Image Processing Prepared by Engr. AU Cabatit page 3 of

% variable = Img( filename.ext ); % always place a semicolon less matlab will display all matrix elements % or use variable = imread( filename.ext ) A=imread( Tree.jpg ); % To view image in matlab: or Imshow(variable) % image(variable) image(A); % or use Imshow(A); % determines the size of the matrix row and column [row, column] = size(A); % shows all the variables used and its dimensions whos % extracts red (1), green (2), or blue (3) from an rgb image syntax (color = variablename (:,:,n)) Red = A(:,:,1) ; image(Red); Green=A(:,:,2); image(Green); Blue=A(:,:,3); image(Blue); % combine all rgb and convert to gray scale B = (A(:,:,1) + A(:,:,2) +A(:,:,3))/3; Imshow(B); % To resize an image A1=imsize(A,0.2); A2=imsize(A1,5); % downsample to 1/5th of the size % upsample back to original size

% Histogram is a measure of the distribution of density within pixels. figure();imhist(A); % Tresholding changing the contrast of an image A3=max(A,100); %removes all values below 100 imshow(A3); A4=min(A,100); %removes all values above 100 Imshow(A4); % A light smoothing filter h = [0 1 0; 1 4 1; 0 1 0]; h = h/sum(h(:)); % Normalize the filter uSmooth = conv2padded(u,h); % A sharpening filter h = [0 -1 0; -1 8 -1; 0 -1 0]; h = h/sum(h(:)); % Normalize the filter uSharp = conv2padded(u,h); % Sobel edge detection hx = [1 0 -1; 2 0 -2; 1 0 -1]; hy = rot90(hx,-1); u_x = conv2padded(u,hx); u_y = conv2padded(u,hy); EdgeStrength = sqrt(u_x.^2 + u_y.^2); % Moving average
ECE DEPARTMENT 9 FEU EAST ASIA COLLEGE Version 1.0. 2009 Module 8 Matlab Image Processing Prepared by Engr. AU Cabatit page 4 of

WindowSize = 5; h1 = ones(WindowSize,1)/WindowSize; uSmooth = conv2padded(h1,h1,u); % Gaussian filtering sigma = 3.5; FilterRadius = ceil(4*sigma); % Truncate the Gaussian at 4*sigma h1 = exp(-(-FilterRadius:FilterRadius).^2/(2*sigma^2)); h1 = h1/sum(h1); % Normalize the filter uSmooth = conv2padded(h1,h1,u); Maximum of 6 images per page Machine Problem: Write a program that will accept different elements for a 3 by 3 filter kernel and perform image convolution using the same filter kernel.

## Figure 2-1 Emboss using convolution

int myEmboss(void *inData, unsigned int inRowBytes, void *outData, unsigned int outRowBytes, unsigned int height, unsigned int width, void *kernel, unsigned int kernel_height, unsigned int kernel_width, int divisor , vImage_Flags flags ) { uint_8 kernel = {-2, -2, 0, -2, 6, 0, 0, 0, 0}; // 1 vImage_Buffer src = { inData, height, width, inRowBytes }; // 2 vImage_Buffer dest = { outData, height, width, outRowBytes }; // 3 unsigned char bgColor[4] = { 0, 0, 0, 0 }; // 4
ECE DEPARTMENT 9 FEU EAST ASIA COLLEGE Version 1.0. 2009 Module 8 Matlab Image Processing Prepared by Engr. AU Cabatit page 5 of

vImage_Error err; // 5 err = vImageConvolve_ARGB8888( *dest, NULL, 0, //unsigned int srcOffsetToROI_X, 0, srcOffsetToROI_Y, kernel, *kernel, kernel_height, //unsigned int kernel_width, //unsigned int divisor, //int bgColor, flags | kvImageBackgroundColorFill //vImage_Flags flags ); return err; } //const signed int //unsigned int &src, &dest, //const vImage_Buffer *src //const vImage_Buffer

## Questions: Q1. What is a histogram?

Histogram is a graphical representation showing a visual impression of the distribution of data. It is an estimate of the probability distribution of a continuous variable
Q2. Describe image convolution?

convolution is a mathematical operation on two functions f and g, producing a third function that is typically viewed as a modified version of one of the original functions. Convolution is similar to cross-correlation. It has applications that include probability, statistics, computer vision, image and signal processing, electrical engineering, and differential equations.
Q3. Describe the process of image convolution? Discussion:

Matlab has applications that include probability, statistics, computer vision, image and signal processing, electrical engineering, and differential equations.
. Conclusion:

Convolution is a mathematical operation on two functions f and g, producing a third function that is typically viewed as a modified version of one of the original functions. Convolution is similar to cross-correlation. It has applications that include probability, statistics, computer vision, image and signal processing, electrical engineering, and differential equations.

page 6 of