Top Banner
To read an image into ‘a’ variable a=imread(C:\Users\m12cs018\Desktop\ r1.jpg) To obtain information about a graphics file and its contents: imfinfo('C:\Users\m12cs018\Desktop\ r1.jpg') Filename: 'C:\Users\m12cs018\ Desktop\r1.jpg' FileModDate: '13-Mar-2013 14:26:31' FileSize: 93228 Format: 'png' FormatVersion: [] Width: 194 Height: 279 BitDepth: 24 ColorType: 'truecolor' FormatSignature: [137 80 78 71 13 10 26 10] Colormap: [ ] Histogram: [ ] InterlaceType: 'none' Transparency: 'none' SimpleTransparencyData: [ ] BackgroundColor: [ ] RenderingIntent: 'perceptual' Chromaticities: [0.3127 0.3290 0.6400 0.3300 0.3000 0.6000 0.1500 0.0600] Gamma: 0.4545 XResolution: 3779 YResolution: 3779
19

Simple Matlab tutorial using matlab inbuilt commands

Jan 15, 2015

Download

Technology

Sarvani Videla

simple matlab tutorial with the following commands imread,
im2bw,
imfinfo,
imadjust,
graythresh,
imcrop,
imrotate,
imtranslate,
imcomplement,
imadd,
imsubtract,
imdivide,
imlincomb,
imnoise,
wiener,
medfilt2,
imfilter,
fspecial,
ordfilt2,
perwitt,
sobel,
canny.
and with outputs
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 1. To read an image into a variable a=imread(C:Usersm12cs018Desktopr1.jpg) To obtain information about a graphics file and its contents: imfinfo('C:Usersm12cs018Desktopr1.jpg') Filename: 'C:Usersm12cs018Desktopr1.jpg' FileModDate: '13-Mar-2013 14:26:31' FileSize: 93228 Format: 'png' FormatVersion: [] Width: 194 Height: 279 BitDepth: 24 ColorType: 'truecolor' FormatSignature: [137 80 78 71 13 10 26 10] Colormap: [ ] Histogram: [ ] InterlaceType: 'none' Transparency: 'none' SimpleTransparencyData: [ ] BackgroundColor: [ ] RenderingIntent: 'perceptual' Chromaticities: [0.3127 0.3290 0.6400 0.3300 0.3000 0.6000 0.1500 0.0600] Gamma: 0.4545 XResolution: 3779 YResolution: 3779 ResolutionUnit: 'meter' XOffset: [ ] YOffset: [ ] OffsetUnit: [ ] SignificantBits: [ ]

2. Convert image to binary image, based on threshold BW = im2bw(I, level) converts the grayscale image I to a binary image. The output image BW replaces all pixels in the input image with luminance greater than level with the value 1 (white) and replaces all other pixels with the value 0 (black).>>b= im2bw(a,0.89) >> imshow(b);Convert an RGB image to a grayscale image>>c=rgb2gray(a) >> imshow(c);Display histogram of image data >> imhist(c) 3. Contrast enhancement imadjust Adjust image intensity values or colormap Syntax: J = imadjust(I)Creating the binary of an image using graythresh Thresholding can be performed by using the graythresh Global image threshold using Otsu's method Syntax level = graythresh(I) level = graythresh(I) computes a global threshold (level) that can be used to convert an intensity image to a binary image with im2bw. level is a normalized intensity value that lies in the range [0, 1]. I=rgb2gray(a) level = graythresh(I); BW = im2bw(I,level); imshow(BW) 4. histeq Enhance contrast using histogram equalization. a=imread('C:fp1.jpg') b=rgb2gray(a) imhist(b)5004003002001000 0c=histeq(b) imhist(c)50100150200250 5. 1000 900 800 700 600 500 400 300 200 100 0 050100150200250Spatial Transformations To resize an image, use the imresize function. When you resize an image, you specify the image to be resized and the magnification factor. To enlarge an image, specify a magnification factor greater than 1. To reduce an image, specify a magnification factor between 0 and 1. For example, the command below reduces the size of an image by 0.75 times. >> I = rgb2gray(a);>> J = imresize(I,.75);>> imshow(I)>>imshow(J) 6. You can specify the size of the output image by passing a vector that contains the number of rows and columns in the output image. If the specified size does not produce the same aspect ratio as the input image, the output image will be distorted. This example creates an output image with 100 rows and 150 columns. >>J = imresize(I,[100 150]); To use bilinear >>Y = imresize(X,[100 150],'bilinear')Using the imcrop function imcrop is used to extract a rectangular portion of an image, You can specify the crop region interactively using the mouse >>J = imcrop(I); or You can specify the crop rectangle as a four-element position vector, [xmin ymin width height] i.e, [60 40 100 90] >>J = imcrop(I,[60 40 100 90]); 7. To rotate an image Use the imrotate function. We specify rotation angle, in degrees. If you specify a positive rotation angle, imrotate rotates the image counterclockwise; if you specify a negative rotation angle, imrotate rotates the image clockwise. To rotate the image 35 counterclockwise using bilinear interpolation >>J = imrotate(I,35,'bilinear'); >>imshow(J) imrotate uses nearest-neighbor interpolation by default to determine the value of pixels in the output image, but you can specify other interpolation methods.To perfrom Translation to (40,40) from origin >> xform = [ 1 0 0 0 1 0 40 40 1 ] xform = 1 0 0 0 1 0 40 40 1 >> tform_translate = maketform('affine',xform); >> k= imtransform(I, tform_translate); >>[cb_trans xdata ydata]= imtransform(I, tform_translate); >> cb_trans2 = imtransform(I, tform_translate,... 'XData', [1 (size(I,2)+ xform(3,1))],... 'YData', [1 (size(I,1)+ xform(3,2))]); >> imshow(cb_trans2)Image Arithmetic 8. Creating the negative of an image using imcomplement >>J=imcomplement(I) >> imshow(J)ImaddAdd two images or add constant to image c=imresize(a,[300,300]) d=imresize(b,[300,300]) imshow(c) K=imadd(c,d) imshow(K)OUTPUT: 9. imsubtract Subtract one image from another or subtract constant from imageK=imsubtract(c,d) imshow(K)imdivideDivides two images or divides image by constant K=imdivide(c,d) imshow(K)immultiplyMultiply two images or multiply image by constant K=immultiply(c,d) imshow(K)imabsdiffabsolute difference of two images K=immultiply(c,d) imshow(K)To display an image in the background and another image on foreground. >> b = imresize(a,[300 300]); >> d = imresize(c,[300 300]); >> e = imlincomb(.5,b,.5,d); 10. >> imshow(b) >> imshow(d) >> imshow(e)Removing Noise By Adaptive Filtering I = rgb2gray(a); >> imshow(I) 11. >> J = imnoise(I,'gaussian',0,0.025); >> imshow(I) >> imshow(J) Output after applying Gaussian noise.Remove the noise, using the wiener2 function. K = wiener2(J,[5 5]); imshow(K)Removing noise using Median filter K=medfilt2(J,[3 3]) imshow(K) 12. Removing noise using Gaussian filter 1. Add Salt and pepper noise J=imnoise(I,'salt & pepper',.02) imshow(J) 2. Creating Gaussian filter h = fspecial(type,parameters) creates a twodimensional filter h of the specified type. fspecial returns h as a correlation kernel, which is the appropriate form to use with imfilter. type is a string having one of these values. h=fspecial('gaussian',[3 3],.7) h= 0.0113 0.0838 0.0113 0.0838 0.6193 0.0838 0.0113 0.0838 0.0113 3. Filter noisy image with Gaussian filter L=imfilter(J,h) imshow(L)Removing noise using 2 dimensionl order-statistics filter L=ordfilt2(J,5,ones(3,3)) imshow(L) 13. Removing noise using Averaging filter 1.Create kernel for average filter with low pass characterisitics k=[ 1 1 1 111 1 1 1]/9 k= 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 2. Plot frequency response characteristics freqz2(k) 3.Apply averaging filter Filter M=imfilter(J,k) imshow(M)Magnitude10.50 1 0 Fy-1-10-0.5 Fx0.51 14. Find the edges of an image using the Prewitt , Sobel and Canny methods. BW1 = edge(I,'prewitt');BW2 = edge(I,'canny'); Imshow(BW2)BW2 = edge(I,'sobel'); Imshow(BW2)