matlab learn lesson

Upload: georgixpie

Post on 02-Jun-2018

235 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Matlab Learn Lesson

    1/22

    Images in MATLAB:What is a pixel? A pixel is the smallest unit of the picture that can bemanipulated. Images in any computer are represented by many smallpoints of color. When the points are small enough, we can representalmost any amount of detail. See example below:

    The simplest way to represent an image is using !bit monochrome. Imagine

    ta"ing an nxn matrix. #ach element in the matrix is a or $, where is whiteand $ is blac". %y placing the white and blac" in specific positions of a largeenough matrix, you can get the following picture.

    A slightly more complicated way to represent an image is using an &!bit '!byte( grayscale. In this case, each element of your nxn matrix actually ta"es& bits 'or eight )s and $)s(, and represents the intensity of light from whiteto blac". Since eight )s or $)s can represent a total of *+& *- di/erentnumbers, there is a total of *- di/erent intensities you can ha0e betweentotally blac" '$$$$$$$$( and totally white '(. 1sually, we use &bits to represent the numbers from $ to *--. This allows you to create morecomplicated images as seen in the example below:

  • 8/10/2019 Matlab Learn Lesson

    2/22

    To store colors in computers, we use color channels. 1sually we use threecolors: red, green, and blue. When the colors are mixed properly, your eyepercei0es any color in the rainbow. Imagine your screen is a matrix of pixels.%ehind each pixel are three color channels. #ach channel will output a

    specified intensity of red, green, or blue light into the pixels. The mixedcolors will allowyou to see some color on that pixel. Thus, we need a total ofthree bytes of info for each pixel, one byte for each color channel. Toproperly represent a colored picture in 2AT3A%, we need to use a 4!5 array6specifically an nxnx4 array. #ach separate page of the array is li"e agrayscale image, where each pixel specifies some intensity. 7owe0er, eachintensity specified this time is for a color channel. The first page is red, thesecond page is green,and the third page is blue. See the example below:

    To read an image into 2AT3A%, use the imread() function. Sa0e the followingimage as a 8peg to your 2AT3A% folder and name it93ow;3ibrary;> x imread'3ow;3ibrary;

  • 8/10/2019 Matlab Learn Lesson

    3/22

    When you type the abo0e into 2AT3A% after sa0ing the file, the picture willbe stored a 4!5matrix in the 0ariable x. If you chec" on the sie of x, you will get thefollowing:

    sie'x(

    ans

    ** *& 4

    To show the matrix x as a picture in 2AT3A% use the image() function.

    >> image'x( @ should display the picture as a figure in 2AT3A%.

    We can access the 4!5 image matrix as we ha0e before. Try

    image'x':,:,((. This should show you what the picture loo"s li"e whenonly the red channel is shown. To call on the other channels simply call onthe *nd

    and 4

    rdpages of the 4!5 matrix.

    >> x',,(

    ans

    @ 7ere we called on the intensity of the red channel for the pixel in row oneand column one of the picture.

    ou can directly change pictures by changing the intensity of eachchannel of the picture. Bor example:

    >> x':$$,:$$,( *--6>> x':$$,:$$,*( *--6>> x':$$,:$$,4( *--6>> image'x(@ 7ere we ha0e set reset the intensities in the upper left corner of the

    pictures by simply changing the 0alue of the intensity. When you image'x(,you should see a white sCuare in the upper left corner of your picture. Seeexample below:

  • 8/10/2019 Matlab Learn Lesson

    4/22

  • 8/10/2019 Matlab Learn Lesson

    5/22

    Histograms in MATLAB:The histogram function in 2AT3A% comes in the form of 9D hist'array,bins(=. The function ta"es two inputs, the array of numbers that you want tograph and the number of binsEgroups you want to separate your numbersinto. The hist function then gi0es you the freCuency of the numbers that

    appears in each binEgroup. If you do not specify an output, the function willautomatically plot the histogram. If you do specify an output, the output willstore the freCuency data in a row 0ector, where each element representsthe freCuency of numbers that appears in a specific binEgroup of numbers.

    >> hist'randn',$$$$(,$$(6

    350

    300

    250

    200

    150

    100

    50

    $!4 !3 !2 !1 0 1 2 3 4 5

    @ Typing the line abo0e in 2AT3A% should pro0ide the graph abo0e.

    Fandn'( is a function that randomly gi0es 0alues following the Gaussiandistribution. Thus the graph should appear as the commonly seen Gaussianbell cur0e.

    >> D

    hist'randn',$$$$(,$$(

    D

  • 8/10/2019 Matlab Learn Lesson

    6/22

    through -H

  • 8/10/2019 Matlab Learn Lesson

    7/22

    *-J *J 4*$ * 4$* *&H 4$ *J 4 4$& *J4 *JJ *&- *J-*H- * *-- **

    imhist'x':,:,((

  • 8/10/2019 Matlab Learn Lesson

    8/22

    @ 7ere we ha0e graphed the red channel of the picture using the imhist()function. We see a large freCuency of high intensity red in the picture.

    Image Matching Using Histograms:Imagine a robot mo0ing through Dew or"

  • 8/10/2019 Matlab Learn Lesson

    9/22

    sum. See example below:

  • 8/10/2019 Matlab Learn Lesson

    10/22

    -.8pg J.8pg

    >> x imread'-.8pg(6>> y imread'J.8pg(6>> D imhist'x':,:,((6>> 2 imhist'y':,:,((6>> sum'D!2(

    ans

    $

    @ Since the pictures ha0e the same number of pixels, e0en if they arecompletely di/erent pictures the sum of the di/erence of the histogramswill always be ero.

    >> sum''D!2(.+*(

    ans

    $&H-H

    >> sum'abs'D!2((

    ans

    **-

    @ To get a number that actually compares how close two picturesare, use the sCuared di/erence or the absolute di/erence.

  • 8/10/2019 Matlab Learn Lesson

    11/22

  • 8/10/2019 Matlab Learn Lesson

    12/22

    Dote: The reason that we set x* to ha0e one fewer 0alue thanx is that y* is the result of diff on y and x, which produces onefewer 0alue than x.

    Similarly, let try to find and plot the deri0ati0e of sin'x(:

    >> x $: piE*$:*Ppi6>> y sin'x(6>> x* $:piE*$:*Ppi!piE*$6

    >> y* di/'y(.Edi/'x(6>> plot'x,y,g!,x*,y*,rQ(>> legend'sin'x(,deri0ati0e of sin'x((

  • 8/10/2019 Matlab Learn Lesson

    13/22

    Numerical Integration:

    We can also use 2AT3A% to calculate the integral of a function. Fecallthe trapeoid method to find the area under a cur0e:

    To find approximately the area under this cur0e, we can di0idethe cur0e into segments, try to place a trapeoid under eachsegment, and sum up all the trapeoidal contributions.

    2AT3A% pro0ides a function called trapz that approximates theintegral of a function 0ia the trapeoid method.

    The trapz function has the form of

    >>trapz(x,y)

    #xample:

    3et)s try to approximate the exact0alue of

    sin(x)dx .$

    We "now that sin(x) dx = cos(x)

    $= cos() (cos($)) = (1) (1) = 2

    $

    >> x $: piE$$:pi6

  • 8/10/2019 Matlab Learn Lesson

    14/22

    >> y sin'x(6>> trap'x,y(

    ans

    .JJJ&

    To compute the numerical integral of a function, we can also usethe Cuadrature function, quad(). The trapeoid method abo0e is aspecific type of Cuadrature method. The way Cuadrature wor"s isby recursi0ely calling a function, e0aluating it at certain points, andfinding the sum. The exact mechanism of how Cuadrature wor"s isbeyond the scope of this lecture.

    The quad method ta"es the form of

    >>quad(function, a, b)

    where function is the function handle of the function that you aretrying to integrate and a and b specify the range of the x 0alues'the limits of integration(.

    %efore we can continue with the discussion of the quad'( function, wefirst need to learn about function handles.

    Function Handles

    We can create a function handle by prepending an R sign to an#MISTIDG function. The function can either be built!in to 2AT3A% orcreated by you and sa0ed in an m! file. The function handle can bethought of as a reference to the actual function. The most importantuse of function handle is that we can treat the handle as a 0ariableand pass it into other functions so they can use them too.

    #xample:

    sCuaref.m

    function y = squaref(x)

    % This function take a value and return the square of it.

    y = x .^ 2;

    We can create a function handle for squaref by entering

    >> s RsCuaref6

    What the function handle does is that it pro0ides a meansof calling a function indirectly.

  • 8/10/2019 Matlab Learn Lesson

    15/22

    Dow you may use the function handle as though it were thesquareffunction itself by passing in an appropriate input argument.

    >> s'$(

    ans

    $$

    To use the quad() function, we need to pass the function handle ofthe function which we are trying to integrate to it.

    #xample:

    3et)s try to integrate the y =x2

    function from $ to The first thing we need is WFIT# the y =x

    2function L1FS#3#S

    Lur squaref

    function does exactly this. So let)s 8ust create a function handlefor it:

    >> s RsCuaref6

    Dext, let us call the quad function with a $ and b

    >> Cuad's, $, (

    ans

    $.4444

    3et)s calculate the integral by hand and 0erify the results:

    x2 dx =1x

    31

    =1

    (1)3

    1($) =

    1

    $ 3$ 3 3 3

    As we can see, the quad function wor"s as intended.

    Dote:Instead of actually creating the function handleseparately and then passing it into Cuad, we could ha0edone it directly, as shown below:

    >>Cuad'RsCuaref, $, (

    And the same result would ha0e been produced.

    1

  • 8/10/2019 Matlab Learn Lesson

    16/22

    olving !rdinar"Di#erential $%uations &!D$s'

    A simpleUrst orderdi/erential Cuestion hasthegeneral formdy

    =f (t,y)

    wheredy

    dt dt

    isthechangein ywithrespect totime'the deri0ati0e of y(, and f (t,y) isany

    function oft and y.

    7ere)san extremelysimpledi/erential eCuation:dy

    =y . We can sol0e

    this L5# bydt

    separation of 0ariables and then integrate. The resulting eCuation is y =

    Cet

    , where < is a constant. In order to sol0e for a speciUc solution, wemust pro0ide an initial condition, such as y'$(, so that we can sol0e

    for

  • 8/10/2019 Matlab Learn Lesson

    17/22

    Klease note, that theorderoftheinputs't,y(matters. Ifthefunction isinsteaddeclared as simleode(y!t), you will get an incorrect result.

    Thenext step istocall theodeH-function. Fememberthat theodeH-

    prod

    uces

    *outputs.

    >>Nt,yO odeH-'Rsimpleode, N$,-O,NO(6

  • 8/10/2019 Matlab Learn Lesson

    18/22

    Binally, let)s plot our y 0s. t graph:

    >> plot't,y(

    Dote:

    This graph should ma"e sense, as the solution to the L5# is y = et

    ,

    which is an exponentially decaying function.

  • 8/10/2019 Matlab Learn Lesson

    19/22

    and a = 9.81 into the

  • 8/10/2019 Matlab Learn Lesson

    20/22

    eCuation v = a * t , we will get the following di/erential eCuation:dy

    =

    9.81*t . If wedt

    were to sol0e this eCuation using separation of 0ariables and thenintegrate, we

    would get the eCuation y = 4.9$t 2 +C,where < is a constant. Gi0en our

    initial condition, where at t$, y-$$ meters, we ha0e 500 = 4.90 * ($)2

    +C , and sol0ing for< would gi0e us C = 500 . So the eCuation that gi0es

    us the position of the ob8ect as a function of time is y = 4.9$t2+500.

    Dow let)s try to sol0edy

    = 9.81*t in 2AT3A%.dt

    The Urst thing we need to do is create our L5# function,

    0elocity;function.mfunction dy_dt = velocity_function(t,y)% $& function that models the equation dy_dy=a'tdy_dt = #.*.'t;

    The next step is to call the odeH- function. Femember that theodeH- produces * outputs.

    >>Nt,yO odeH-'R0elocity;function, N$,

    $O, -$$(6 Binally, let)s plot our y 0s. t

    graph:

    >> plot't,y(

  • 8/10/2019 Matlab Learn Lesson

    21/22

  • 8/10/2019 Matlab Learn Lesson

    22/22