Image Processing Using MATLAB www.techsource.com.my Image Processing Using MATLAB® TechSource Systems Sdn Bhd ©2005 Systems Sdn Bhd www.techsource.com.my Course Outline: Working with Images in MATLAB a) Image types and classes b) Read/write images c) Display images Basic Image Processing a) Image contrast and brightness enhancement b) Image arithmetic Block Processing of Images Image Restoration a) Noise reduction (filtering) b) Image alignment Image Segmentation & Edge Detection Case Studies ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd Image Processing Using MATLAB Working with Images in MATLAB www.techsource.com.my Section Outline: Image types • Index images • Intensity images • Binary images • RGB images Importing and exporting images in MATLAB • imfinfo • imread and imwrite • imshow Converting between image formats ©2005 Systems Sdn Bhd Working with Images in MATLAB www.techsource.com.my Image Types • Four basic types of images are supported in MATLAB • Index images: m-by-3 colormap matrix • Intensity images: [0 1] or uint8 • Binary images: • RGB images: {0, 1} m-by-n-by-3 matrix >> load sampleImages ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd Image Processing Using MATLAB Working with Images in MATLAB www.techsource.com.my Image Types: MATLAB Data Types Used • A wide array of different data types exist in MATLAB, but only a subset of the data types are used to represent images in MATLAB Image data types ©2005 Systems Sdn Bhd Working with Images in MATLAB www.techsource.com.my Image Types: Index Images • An indexed image consists of a data matrix, X, and a colormap matrix, map >> imshow(indexImg, map) ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd Image Processing Using MATLAB Working with Images in MATLAB www.techsource.com.my Image Types: Intensity Images • An intensity image only consists of one matrix, I, whose values represent intensities within some range, for example [0 1] or uint8 >> imshow(intenImg) ©2005 Systems Sdn Bhd Working with Images in MATLAB www.techsource.com.my Image Types: Binary Images • In a binary image, each pixel assumes one of only two discrete values: (off) and (on) >> imshow(bwImg) ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd Image Processing Using MATLAB Working with Images in MATLAB www.techsource.com.my Image Types: RGB Images • RGB image is stored in MATLAB as an m-by-n-by-3 data where each m-by-n page defines red (R), green (G) and blue (B) color components for each pixel >> imshow(rgbImg) ©2005 Systems Sdn Bhd Working with Images in MATLAB www.techsource.com.my Overview: How Images are Represented? Image Type Intensity Binary RGB Image is an M-by-N array of integers in the range [1,P] Double Data Indexed Image is an M-by-N array of floatingpoint values The conventional dynamic range is [0,1] Image is an M-byN logical array containing only 0's and 1's Image is an M-by-N-by-3 array of floating-point values in the range [0,1] Image is an M-by-N array of unsigned 8bit integers The conventional dynamic range is [0,255] Image is an M-byN logical array containing only 0's and 1's Unlike uint8 intensity images, represents white Image is an M-by-N-by-3 array of floating-point values in the range [0,255] Colormap is a P-by-3 array of floating-point values in the range [0,1] Image is an M-by-N array of integers in the range [0,P-1] Uint8 Data ©2005 Colormap is a P-by-3 array of floating-point values in the range [0,1] Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd Image Processing Using MATLAB Working with Images in MATLAB www.techsource.com.my Color Space of RGB and HSV • There are two main types of color spaces that are used with images: RGB and Hue-Saturation- Value (HSV) ©2005 Systems Sdn Bhd Working with Images in MATLAB www.techsource.com.my Importing and Exporting Images in MATLAB • Image Processing Toolbox • imfinfo- Returns info about graphics file • imread - Read image from graphics file • imwrite- Write image to graphics file • MATLAB • uiimport - Starts the Import Wizard >> >> >> >> ©2005 imfinfo(‘canoe.tif’) [X, map] = imread(‘canoe.tif’); imshow(X, map); imwrite(X, map, ‘canoe2.bmp’); Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd Image Processing Using MATLAB Working with Images in MATLAB www.techsource.com.my Graphical Representation of Importing an Image Colormap? imfinfo('file.fmt') file.fmt x=imread('file.fmt'); [x,map]=imread('file.fmt'); Image Type Double Data Im age Type Double Data [MXN] + Uint8 Data Uint8 Data ©2005 Indexed [MXN] + Intensity Binary 0.5 0.2 0.1 0.9 0.7 0.8 45 220 100 78 110 200 98 RGB 0 1 1 0.9 0.4 0.1 0.7 0.3 0.5 0.2 0.3 0.5 0.9 0.2 0 1 1 90 140 225 100 70 30 22 10 50 230 109 220 Systems Sdn Bhd Working with Images in MATLAB www.techsource.com.my Displaying Images • imshow • image • imagesc - Display image - Create and display image object (MATLAB) - Scale data and display as image (MATLAB) • • • • • - Display colorbar (MATLAB) - Sets the color map of the image (MATLAB) - Display multiple image frames - Adjust display size of image - Display image as texture-mapped surface ©2005 colorbar colormap montage truesize warp Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd Image Processing Using MATLAB Working with Images in MATLAB www.techsource.com.my Montage • An example of displaying multiple image frames as a rectangular montage >> load mri >> montage(D, map) ©2005 Systems Sdn Bhd Working with Images in MATLAB www.techsource.com.my Warping • The warp function allows you to display an image as a texture-mapped surface >> [x, y, z] = sphere; >> load earth >> warp(x, y, z, X, map) ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd Image Processing Using MATLAB Working with Images in MATLAB www.techsource.com.my Converting Image Formats • • • • • – indexed image to intensity image – indexed image to RGB image (MATLAB) – intensity image to indexed image – RGB image or colormap to grayscale – RGB image to indexed image ã ã ã ã ã â2005 ind2gray ind2rgb gray2ind rgb2gray rgb2ind mat2gray im2bw im2double im2uint8 im2uint16 – matrix to intensity image – image to binary image by thresholding – image array to double precision – image array to 8-bit unsigned integers – image array to 16-bit unsigned integers Systems Sdn Bhd Working with Images in MATLAB www.techsource.com.my Exercise 1: Loading and Viewing an Image Load in the trees.tif file into MATLAB – What type of image is it? Can you tell before loading in the file? Display the loaded image Convert the image to an intensity image (grayscale) Now convert it to a binary (black and white) image Extra credit: Use subplots to display all three images Did you find the "Easter egg" in the "trees"? ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd Image Processing Using MATLAB Working with Images in MATLAB www.techsource.com.my Solution: Loading and Viewing an Image >> im_info = imfinfo('trees.tif'); Why use subimage? >> im_info(1).ColorType >> [I,map] = imread('trees.tif'); >> subplot(2,2,1), subimage(I,map) >> I_gray = ind2gray(I,map); >> subplot(2,2,2), subimage(I_gray) Threshold >> I_bw = im2bw(I,map,0.4); >> subplot(2,2,3), subimage(I_bw) % Easter egg >> figure >> [I2,map] = imread('trees.tif',2); >> imshow(I2,map) ©2005 Systems Sdn Bhd Working with Images in MATLAB www.techsource.com.my Section Summary: Image types • Index images • Intensity images • Binary images • RGB images Importing and exporting images in MATLAB • imfinfo • imread and imwrite • imshow Converting between image formats ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd 10 Image Processing Using MATLAB Image Restoration www.techsource.com.my Adaptive Filter The wiener2 adaptive filter tailors itself to the local image variance adaptively If the variance is large, it minimally smoothes the image If the variance is small, it performs more smoothing This type of filter is effective in reducing the effects of Gaussian whitenoise Syntax [J,noise] = wiener2(I,[m n],noise) I – Input image [m n] – Size neighborhood block used to calculate the median J – Output image noise – Noise variance ©2005 Systems Sdn Bhd Image Restoration www.techsource.com.my Wiener Filter Example >> >> >> >> >> >> ©2005 I = imread('cameraman.tif'); I_noise = imnoise(I, 'gaussian', 0.01); [I2, noise] = wiener2(I_noise,[3 3]); subplot(1,2,1), imshow(I_noise), title('Original Image') subplot(1,2,2), imshow(I2) title(['Filtered Image: noise variance =', num2str(noise)]) Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd 32 Image Processing Using MATLAB Image Restoration www.techsource.com.my Filter Demonstration Explore noise reduction in images using linear and nonlinear filtering techniques by running the following demo: >> nrfiltdemo ©2005 Systems Sdn Bhd Image Restoration www.techsource.com.my Region-Based Processing Region-based processing allows you to select a region of interest (ROI), and process only upon the selected area • A ROI is defined using a binary mask – The mask contains 1's for all pixels that are part of the region of interest and 0's everywhere else • Types of region-based processing that can be done: Specify a region of interest (roipoly, roicolor) Filter a region (roifilt2) Fill in a region (roifill) ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd 33 Image Processing Using MATLAB Image Restoration www.techsource.com.my Specifying a Region of Interest A region of interest can be specified using one of the Image Processing functions or with any user defined binary mask The options are: Using roipoly, allows you to specify a polygonal region of interest When called with no inputs a cursor can be used to create a polygon Using roicolor, where you can define the region of interest based on a color or intensity range Using Boolean indexing to create a binary mask ©2005 Systems Sdn Bhd Image Restoration www.techsource.com.my Filtering a Region Once a region has been specified, a filter can be created and implemented on the region >> I = imread('cameraman.tif'); >> imshow(I) >> BW = roipoly; >> figure, imshow(BW) >> h = fspecial('unsharp'); >> I2 = roifilt2(h,I,BW); >> imshow(I2) ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd 34 Image Processing Using MATLAB Image Restoration www.techsource.com.my Filling in a Region of Interest (ROI) >> [X,map] = imread('trees.tif'); >> I = ind2gray(X,map); >> imshow(I) >> I2 = roifill; % intensity img only >> figure, imshow(I2) ©2005 Systems Sdn Bhd Image Restoration www.techsource.com.my Image Alignment When the alignment is off in an image, you can apply some basic spatial transformation techniques Rotate the image (imrotate) Crop the image (imcrop) Resize the image (imresize) With the imtransform function, you can transform your image to any geometric shape as well ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd 35 Image Processing Using MATLAB Image Restoration www.techsource.com.my Image Rotation Syntax B = imrotate(A,angle,method) B = imrotate(A,angle,method,'crop') B – Output image A – Input image angle – Degrees of rotation in the counter-clockwise direction method – Type of interpolation: [{nearest}, bilinear, bicubic] 'crop' – Returns only central portion of B which is the same size as A ©2005 Systems Sdn Bhd Image Restoration www.techsource.com.my Image Cropping Syntax I2 = imcrop(I,rect) I2 – Output image I – Input image rect – Spatial coordinates of [xmin ymin width height] If rect is omitted, you specify the crop region on the image directly using the mouse ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd 36 Image Processing Using MATLAB Image Restoration www.techsource.com.my Image Resizing Syntax B = imresize(A,m,method) B – Output image A – Input image m – Magnification factor method – Type of interpolation: [{nearest}, bilinear, bicubic] ©2005 Systems Sdn Bhd Image Restoration www.techsource.com.my Exercise 4: Aligning an Object Rotate the image so that the letter L is standing right side up Crop the image so that only the letter is showing Resize the image so that it is the same as the original image >> [X, map] = imread('bw_L.gif'); >> I = ind2gray(X,map); ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd 37 Image Processing Using MATLAB Image Restoration www.techsource.com.my Solution: Aligning an Object >> >> >> >> [X, map] = imread('bw_L.gif'); I = ind2gray(X,map); size_I = size(I); subplot(2,2,1), imshow(I) >> J = imrotate(I,120,'nearest','crop'); >> subplot(2,2,2), imshow(J) >> K = imcrop(J); >> subplot(2,2,3), imshow(K) >> L = imresize(K,size_I); >> subplot(2,2,4), imshow(L) >> whos I J K L ©2005 Systems Sdn Bhd Image Restoration www.techsource.com.my Section Summary: Reducing noise • Filters • Region-based processing Image alignment • Rotation • Cropping • Resizing ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd 38 Image Processing Using MATLAB Image Segmentation & Edge Detection www.techsource.com.my Section Outline: Morphology and segmentation Structuring element Dilation and erosion Edge detection ©2005 Systems Sdn Bhd Image Segmentation & Edge Detection www.techsource.com.my Example Problem We are assigned to locate all the rectangular chips on a black and white integrated circuit image, but how? ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd 39 Image Processing Using MATLAB Image Segmentation & Edge Detection www.techsource.com.my Morphology & Segmentation One way we can solve the problem of identifying objects is using morphological techniques to segment the objects Morphology – technique used for processing image based on shapes Segmentation – the process used for identifying objects in an image ©2005 Systems Sdn Bhd Image Segmentation & Edge Detection www.techsource.com.my Structuring Element To process an image according to a given shape, we need to first define the shape, or structuring element The Image Processing Toolbox provides a function for creating a structuring element that can be used, strel Syntax SE = strel(shape,parameters) SE – Structuring element shape – Flat ['arbitrary' 'pair' 'diamond' 'periodicline' 'disk' 'rectangle' 'line' 'square' 'octagon'] Nonflat ['arbitrary' 'ball'] parameters – Associated with the selected shape ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd 40 Image Processing Using MATLAB Image Segmentation & Edge Detection www.techsource.com.my Below is an example of a diamond shaped structuring element with the distance from the structuring element origin to the points of the diamond being >> SE = strel('diamond',3) SE = Flat STREL object containing 25 neighbors Decomposition: STREL objects containing a total of 13 neighbors Neighborhood: 0 0 1 1 0 0 ©2005 1 1 1 1 1 1 1 1 0 1 0 0 0 Systems Sdn Bhd Image Segmentation & Edge Detection www.techsource.com.my Dilation & Erosion To dilate an image, use the imdilate function, and to erode an image, use the imerode function Syntax IM2 = imdilate(IM,SE) IM2 = imerode(IM,SE) IM2 – The dilated/eroded image IM – The image to dilate/erode SE – The structuring element ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd 41 Image Processing Using MATLAB Image Segmentation & Edge Detection www.techsource.com.my Below is an example of dilating a square binary image BW = zeros(9,10); BW(4:6,4:7) = SE = strel('square',3) BW2 = imdilate(BW,SE) BW = 0 0 0 0 0 0 0 0 0 ©2005 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 BW2 0 0 0 0 = 0 0 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 Systems Sdn Bhd Image Segmentation & Edge Detection www.techsource.com.my Example Solution To locate the chips on the integrated circuit we can use the morphological techniques of erosion and dilation >> BW1 = imread('circbw.tif'); >> SE = strel('rectangle',[40 30]); >> BW2 = imerode(BW1,SE); >> BW3 = imdilate(BW2,SE); >> L = bwlabel(BW3); >> RGB = label2rgb(L, 'spring', 'c', 'shuffle'); >> imshow(RGB) ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd 42 Image Processing Using MATLAB Image Segmentation & Edge Detection www.techsource.com.my Edge Detection The edge function of the Image Processing Toolbox makes it easy to perform edge detection >> >> >> >> >> >> >> ©2005 I = imread('circuit.tif'); BW1 = edge(I,'sobel'); BW2 = edge(I,'canny'); subplot(131),imshow(I),title(‘Original’) subplot(132),imshow(BW1),title(‘sobel’) subplot(133),imshow(BW2),title(‘canny’) edgedemo Systems Sdn Bhd Image Segmentation & Edge Detection www.techsource.com.my Section Summary: Morphology and segmentation Structuring element Dilation and erosion Edge detection ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd 43 Image Processing Using MATLAB Case Studies www.techsource.com.my Section Outline: Motion Detection Motion detection [motion_detect.m] Object tracking [BounceJasc.m] Text recognition [recognize_text.m] Text Recognition ©2005 Object Tracking Systems Sdn Bhd www.techsource.com.my ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd 44 Image Processing Using MATLAB www.techsource.com.my ©2005 Systems Sdn Bhd www.techsource.com.my ©2005 Systems Sdn Bhd Copyrighted 2005 TechSource Systems Sdn Bhd 45 Image Processing Using MATLAB www.techsource.com.my ©2005 Systems Sdn Bhd www.techsource.com.my The End Kindly return your Evaluation Form ©2005 Systems Sdn Bhd Tel: 603 – 8076 1953 Fax: 603 – 8076 1954 Email: info@techsource.com.my Web: www.techsource.com.my Tech-Support: techsupport@techsource.com.my Tech- Copyrighted 2005 TechSource Systems Sdn Bhd 46 ... Bhd Image Processing Using MATLAB Working with Images in MATLAB www.techsource.com.my Converting Image Formats • • • • • – indexed image to intensity image – indexed image to RGB image (MATLAB) .. .Image Processing Using MATLAB Working with Images in MATLAB www.techsource.com.my Section Outline: Image types • Index images • Intensity images • Binary images • RGB images Importing... Working with Images in MATLAB www.techsource.com.my Displaying Images • imshow • image • imagesc - Display image - Create and display image object (MATLAB) - Scale data and display as image (MATLAB)