0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Basics of Image Processing using MATLAB

42,559

Published on

Published in: Technology, Art & Photos
9 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
42,559
On Slideshare
0
From Embeds
0
Number of Embeds
21
Actions
Shares
0
1,976
0
Likes
9
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. April 21, 2010 Varun K. NagarajaFinal Year, Dept of ECE NITK Surathkal
• 2. Basics of Image Processing using MATLABComputer Vision and Related Fields
• 3. Basics of Image Processing using MATLAB
• 4. Basics of Image Processing using MATLAB Activity RecognitionImage Stitching
• 5. Basics of Image Processing using MATLAB Medical Image Enhancement Image Morphing
• 6. Basics of Image Processing using MATLAB Basic operation with Matrices% Use colon at the end to suppress % Transpose of a matrix% output A_trans = A&#x2019;% To enter a matrix with real elements A_trans =A = [5 3 7; 8 9 2; 1 4.2 6e-2] 5.0000 8.0000 1.0000A = 3.0000 9.0000 4.2000 7.0000 2.0000 0.0600 5.0000 3.0000 7.0000 8.0000 9.0000 2.0000 % Matrix addition 1.0000 4.2000 0.0600 C = A + A_trans% To enter a matrix with complex C =% elementsX = [5+3*j 7+8j; 9+2j 1+4j;] 10.0000 11.0000 8.0000 11.0000 18.0000 6.2000X = 8.0000 6.2000 0.1200 5.0000 + 3.0000i 7.0000 + 8.0000i 9.0000 + 2.0000i 1.0000 + 4.0000i
• 7. Basics of Image Processing using MATLAB Basic operation with Matrices% Matrix multiplication (element wise)C = A .* A_transC = 25.0000 24.0000 7.0000 These commands or functions can be run in the 24.0000 81.0000 8.4000 MATLAB command prompt or as Script files (.m) 7.0000 8.4000 0.0036 Either% Matrix multiplication &#x2022; type edit &lt;filename&gt;.m in MATLAB commandC = A * A_trans prompt to open the editor or &#x2022; go File - New - Blank M FileC = 83.0000 81.0000 18.0200 81.0000 149.0000 45.9200 18.0200 45.9200 18.6436
• 8. Basics of Image Processing using MATLABA Simple Character Recognition Code &#x2022; Detect only particular characters and numbers in an image. &#x2022; Characters are in white and of a fixed size. &#x2022; Background is black in color. &#x2022; The image is in binary format. We will explore various concepts as we implement this.
• 9. Basics of Image Processing using MATLAB Reading images in MATLAB% Set working directory to directory% containing this tutorial% Reading an image% A = IMREAD(FILENAME,FMT) or% A = IMREAD(FILENAME.FMT)im=imread(.char recogtestimage.bmp);% It is better to suppress outputs when% reading images. Try once without the% colon at the end of command% Displaying an imageimshow(im)% To open a separate window for the% figure and not overwrite in the% existing windowfigureimshow(im)figure, imshow(im)
• 10. Basics of Image Processing using MATLAB Reading images in MATLABNow read the image &#x2018;same color.jpg&#x2019; and display it on a window.Once the image is displayed in the window, select Tools &#x2013; Data Cursor or select the shortcut on thetoolbar.Click on point A as shown, on the image. It displays three values (RGB) since it is a color image. Youcan try reading pixel values for the previous image. It will be either 0/1 since it is binary image.Hold Alt and Click on point B. This creates something called as a new datatip.Now for some fun B A
• 11. Basics of Image Processing using MATLAB Reading images in MATLABNow read the image &#x2018;same color.jpg&#x2019; and display it on a window.Once the image is displayed in the window, select Tools &#x2013; Data Cursor or select the shortcut on thetoolbar.Click on point A as shown, on the image. It displays three values (RGB) since it is a color image. Youcan try reading pixel values for the previous image. It will be either 0/1 since it is binary image.Hold Alt and Click on point B. This creates something called as a new datatip.Now for some funWhat are the RGB values at the two points? B A Adelsons checker shadow illusion (http://en.wikipedia.org/wiki/Same_color_illusion)
• 12. Basics of Image Processing using MATLAB Writing functions in MATLABLet&#x2019;s write a function charrec(im)which when called with an image file, willdisplay the characters as shown earlier&gt;&gt; im=imread(.char recogtestimage.bmp);&gt;&gt; imshow(im);&gt;&gt; charrec(im);The digits found in the image are:035The letters found in the image are:LN
• 13. Basics of Image Processing using MATLAB Writing functions in MATLAB Few examples of functions in MATLAB% Function returning no outputfunction sample(ip1,ip2,ip3,&#x2026;)...end% Function with outputsfunction [op1,op2,&#x2026;]=sample(ip1,ip2,ip3,&#x2026;)...End% save the code as sample.m. Function name% and m-file name should be the same
• 14. Basics of Image Processing using MATLAB The AlgorithmDilation&#x2022; adds pixels to the boundaries of objects in an image.&#x2022; number of pixels added from the objects in an image depends on the size and shape of the structuring element&#x2022; function strel(&#x2026;) can be used to generate the SEs.&gt;&gt; SE = strel(diamond, 1)SE =Flat STREL object containing 5 neighbors.Neighborhood: 0 1 0 1 1 1 0 1 0
• 15. Basics of Image Processing using MATLAB&gt;&gt; SE = strel(square,3)SE =Flat STREL object containing 9 neighbors.Neighborhood: 1 1 1 1 1 1 1 1 1 Check out help on strel for&gt;&gt; SE = strel(line, 7, 45) various combinationsSE =Flat STREL object containing 5 neighbors.Neighborhood: 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0
• 16. Basics of Image Processing using MATLABDilation does not necessarily mean dilation of the holes also. The holes getcontracted as shown above.Also try image erosion. Use MATLAB&#x2019;s help.
• 17. Basics of Image Processing using MATLAB Continuing with The AlgorithmWhen the dilated image of the character us subtracted from the original we getsomething like&#x2026; Next we create such images for all the characters that we want to recognize. (For all those individual character images in the folder) &gt;&gt; N = imread (.char recogN.bmp); &gt;&gt; SE = strel(square,3); &gt;&gt; N1 = imdilate(N,SE); &gt;&gt; N2 = N1 - N; &gt;&gt; figure,imshow(N2)
• 18. Basics of Image Processing using MATLAB Continuing with The AlgorithmFunction, bwhitmiss is employed to check if a particular character is present inthe given image.bwhitmiss(BW1,SE1,SE2) performs the hit&#x2010;miss operation defined by thestructuring elements SE1 and SE2. The hit&#x2010;miss operation preserves pixelswhose neighborhoods match the shape of SE1 and dont match the shape of SE2.If the matrix returned by bwhitmiss contains non zero elements, then thecharacter is found in the image. &gt;&gt; if ~isempty(nonzeros(bwhitmiss(im,N,N2))) disp(N); endAlso note the use of functions isempty and nonzerosYou can now use charrec.m to recognize few characters in a crude way.
• 19. Basics of Image Processing using MATLAB Image SegmentationGlobal Thresholding Method&gt;&gt; im=imread(automata.jpg);&gt;&gt; im_gray=rgb2gray(im);% im2bw converts grayscale image to binary% image using a global threshold&gt;&gt; bw1=im2bw(im_gray);&gt;&gt; bw2=im2bw(im_gray, threshold);% if no threshold is specified, it uses a% function graythresh to calculate the% threshold. Otsu&#x2019;s method is implemented in% graythresh function.
• 20. Basics of Image Processing using MATLAB Image SegmentationGlobal Thresholding MethodDisadvantage is when there are multiple colors for objects and backgrounds. Result with global thresholding &#x2013; one of the blocks is lost
• 21. Basics of Image Processing using MATLAB Image Segmentation Local Thresholding Method: Niblack&#x2019;s Method &gt;&gt; im=imread(blocks.jpg); &#xF0EC;255 if I( x, y ) &#xF03E; T&#xF02B; ( x, y )&#xF0FC; &gt;&gt; im_gray=rgb2gray(im); &#xF0EF; &#xF0EF;R( x, y ) &#xF03D; &#xF0ED;100 if I( x, y ) &#xF03C; T&#xF02D; ( x, y )&#xF0FD; &gt;&gt; imt=niblack(im_gray,0.5,201); &gt;&gt; figure,imshow(imt,[]) &#xF0EF; 0 &#xF0EF; &#xF0EE; otherwise &#xF0FE; % Here k=0.5 and N=201(preferably odd) T&#xF0B1; ( x, y) &#xF03D; &#xF06D; N &#xF0B1; k &#xF0B4; &#xF073; N % think about effects of values of N on % processing time and k on thresholding % level k and N are to be empirically determined
• 22. Basics of Image Processing using MATLAB Image Segmentation Local Thresholding Method: Niblack&#x2019;s Method% Since our objects of interest are white pixels,% we will consider those equal to 255&gt;&gt; imwhite=(imt==255);% observe how the above command works. It checks% pixel by pixel for the condition and returns a% matrix&gt;&gt; figure, imshow(imwhite)% Now we need to clear the noisier regions. We% use an erosion process followed by reconstruction&gt;&gt; im_eroded=imerode(imwhite,strel(square,51));&gt;&gt; im_recon=imreconstruct(im_eroded,imwhite);&gt;&gt; figure, imshow(im_recon)
• 23. Basics of Image Processing using MATLAB Connected Components% You can see pixels connected to each other% form objects in the image. These are% called connected components. Read more% about 4-connectivity and 8-connectivity% Label the connected components i.e. assign% a particular number as pixel value to one CC&gt;&gt; [bw_labelled num]=bwlabel(im_recon);&gt;&gt; figure,imshow(bw_labelled,[])% We can use regionprops() to extract some% properties of the CCs&gt;&gt; areas = regionprops(bw_labelled,Area)% Here areas is a struct variable% Try experimenting with other properties and% explore what property can be used to% distinguish between CCs
• 24. Basics of Image Processing using MATLAB% We will convert to struct to a normal array for easy operation&gt;&gt; areas1=[];&gt;&gt; for i=1:length(areas)areas1=[areas1; areas(i,1).Area];end&gt;&gt; areas1areas1 = 415711 26440 10350 8630 17971 8282 5243% We are interested in objects (the squares) with area in range 8000-% 9000&gt;&gt; index=find(areas1&gt;8000 &amp; areas1&lt;9000);&gt;&gt; finalimg=zeros(size(bw_labelled));&gt;&gt; for i=1:length(index)finalimg=finalimg+(bw_labelled==index(i));end
• 25. Basics of Image Processing using MATLAB&gt;&gt; figure,imshow(finalimg,[])This was again a very crude way, since we are depending only on value ofarea which might not remain constant if camera changes position.Most of the times the standard features available with regionprops() isnot sufficient. We will have to write our own code to extract features.Also we used hard thresholds for areas to classify CCs. Again most of thetimes, this is not followed. Classifiers using Pattern Recognition techniquesare employed.
• 26. Basics of Image Processing using MATLAB Few Other StuffYou can tryEdge detection&gt;&gt; im=imread(ouch.jpg);&gt;&gt; im_gray=rgb2gray(im);&gt;&gt; imedge=edge(im_gray,canny,[0.1 0.2]);&gt;&gt; figure,imshow(imedge)% Try different edge operators and% threshold levelsandRemoving Noise By Median Filtering(MATLAB Help) There is more to learn in Image Processing. All the Best
• 27. Basics of Image Processing using MATLAB