4

Interactive Applications, Image Processing and More

In this chapter we explore the use of dynamic functions for creating interactive simulations (demonstrations). We also introduce image processing, graphs and networks and show some of the commands added to the most recent Mathematica versions for advanced calculus. We will use examples throughout the chapter to illustrate the functionality of all the commands.

4.1 Manipulate

One of the most interesting functions in Mathematica is Manipulate. We can easily build dynamic applications with it. This function has numerous options, and in this section we show some of them. Normally we will use more than one option at a time; if you prefer you can try them separately and then combine them. Remember that if you don’t know the syntax of a function you can always use the free-form input first and later modify the Mathematica output to suit your needs.

Manipulate [Plot [Sin [x *a ], {x, 0, 1}], {{a, 1}, 0, 2}]

Images

The output area containing Manipulate changes dynamically depending on the input. However, sometimes we may be interested in giving it a fixed size.

In some of the examples that come next we use ContentSize to reduce the print size. To replicate them in a computer you can remove the option.

Another interesting option is ControlType. For example, we can use Checkbox, an option not only available in Manipulate, to include check boxes. The syntax is: {a,{a1,a2}}, where {a1,a2} refers to the alternatives available, a1 is the default option. When there are more than two choices you can use RadioButtonBar.

Once we execute the previous command we can change the color and/or choose not to display the axes.

If we use Locator with Manipulate we will be able to move objects in the output with the mouse. Additionally, all the information associated to those objects will be dynamically updated. The syntax of Locator is: {{parameter, {x, y}}, Locator}.

In some of the examples that follow we have added the option ImageSize → Tiny or ImageSize → Medium to reduce the print size. To replicate them in a computer remove the option.

We are going to extend the functionality of the previous command to calculate and display the area of the triangle, but first let’s introduce a way to call a function from within Manipulate.

If we are going to use PlotPoints with a large number of points, we can save computation time by using ControlActive.

4.2 Creating Demonstrations

The main use of Manipulate is to create eye-catching interactive visualizations with just a few lines of code. In the Demonstrations site (http://demonstrations.wolfram.com) you can find thousands of them. Some of the demonstrations that we will be showing here are adaptations of existing ones available on the website.

4.2.1 A Random Walk

  • We use the function RandomWalkProcess to represent a random walk on a line with the probability of a positive unit step p and the probability of a negative unit step 1-p. By default we assume n steps = 500 and set the interval from 100 to 1000 and p = 0.5. Each time we execute the command, even though the number of steps is the same, the distance covered will be different since we are assuming random walks.

    Manipulate[
      ListPlot[RandomFunction[RandomWalkProcess[p], {1, n}], Filling → Axis],
      {{n, 500, "Steps"}, 100, 1000, 1, Appearance → "Labeled"},
      {{p, 0.5, "Probability"}, 0, 1, Appearance → "Labeled"}]

Images

4.2.2 Zooming and the Rosetta Stone

In this example we show how we can zoom on an image and scroll through it. In this case the image is a picture of the Rosetta Stone but any other picture would do as well.

  • We can import it directly with:

    Clear["Global`*"] (* Remove all the variables from memory *)

    rosettaimage =
       Import["http://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/
          Rosetta_Stone_BW.jpeg/800px-Rosetta_Stone_BW.jpeg"];

    rosetta = Image[rosettaimage, ImageSize → 300]

    Images

  • Now we can copy it, as shown below, or type rosetta instead.

    Manipulate[
      ImageResize[ImageCrop[ Images, {300, 300}/ zoom, - pts],
        {300, 300}, Resampling → ControlActive[ "Nearest", "Lanczos"]],
      {{pts, {0, 0}, "pts"}, {- 1, - 1}, {1, 1}},
      {{zoom, 1, "Zoom"}, 1, 10}]

    Images

  • We can generate a similar effect using DynamicImage (New in Mathematica 11.0).

    DynamicImage[ Images, ZoomFactor → 2, ImageSize →{300, 300}]

Images

4.2.3 Zooming and Google Maps

  • In this example we access https://maps.google.com using URLFetch and we move through the chosen area by zooming on it. We use functions not yet covered in the chapter that can be found in the help system. InputField can be especially useful. It enables us to open a window for inputting the data interactively, in this case the city that we are interested in.

    Manipulate[ImportString[
      ExportString[URLFetch["http://maps.googleapis.com/maps/api/staticmap",
        "ContentData", "Parameters" → {"center" → c, "zoom" → ToString[z],
         "size" → "512x256", "maptype" → ToLowerCase["Map"], "sensor" → "false",
         "style" → "inverse_lightness:true", "format" → "png"}], "Byte",
        "DataFormat" → {"Bytes"}], "PNG", "DataFormat" → {"Byte"}],
      {{c, "Salamanca", "Location"}, "",
       InputField[#1, String, FieldSize → 35] &},
      {{z, 11, "Zoom"}, 1, 20, 1}, ContinuousAction → False]

    Images

  • An easier way of doing it is with DynamicGeoGraphics (available since Mathematica 11.0).

    DynamicGeoGraphics[ Images, GeoRange → Quantity[10, "km"]]

Images

4.2.4 Derivatives in Action

Clear["Global`*"]

  • Here is a derivative evaluation using the definition:

    Panel[DynamicModule[{f = Sin[x]}, lhs = DifferenceQuotient[f, {x, h}];
       Column[{InputField[Dynamic[f]], Dynamic[Block[{e = Inactivate[
               Limit[lhs, h -> 0]]}, e ? Activate[e]]]}]] // TraditionalForm]

    Images

  • In this example we use derivatives to show the tangent at the selected point for different curves. This type of visualization is useful to represent the tangent to a curve and introduce the concept of the derivative.

    Manipulate[Plot[{f[x], f[x1] + f'[x1] * (x - x1)}, {x, -2 Pi, 2 Pi},
       PlotRange -> {-2, 2}, Epilog -> {PointSize[0.025], Point[{x1, f[x1]}]}],
      {x1, -2 Pi, 2 Pi}, { f, {Sin, Cos, Tan, Sec, Csc, Cot}}]

Images

4.2.5 Tsunamis

We can model very complex natural phenomena. The following demonstration simulates a tsunami using the following system of partial differential equations:

2u(t,x)t2=2u(t,x)x2+a u(t,x)3+b u(t,x)2+c u(t,x)+d,with u(0,x)=ex2,u(t,x)t=0 u(t,x0)=u(t,x0)

(http://mathworld.wolfram.com/news/2005-01-14/tsunamis)

  • We solve it with NDSolve using a, b, c, d, x0 as parameters. We can show dynamically the behavior of the system by modifying the parameter values.

    Manipulate[
     Plot3D[Evaluate[u[t, x] /. Quiet[NDSolve[Evaluate[{D[u[t, x], t, t] ]
              D[u[t, x], x, x] + a u[t, x]^3 + b u[t, x]^2 + c u[t, x] + d,
            u[0, x] == e^-x^2, D[u[t, x], t] == 0 /. t → 0, u[t, -x0] == u[t, x0]}],
          u, {t, 0, x0}, {x, -x0, x0}, Method → {"MethodOfLines",
            "SpatialDiscretization" → {"TensorProductGrid",
              "DifferenceOrder" → "Pseudospectral", "MinStepSize" → 0.2}}]]],
      {x, -x0, x0}, {t, 0, x0}, MeshFunctions → {#3 &},
      Mesh → None,
      ColorFunction → "Rainbow",
      PlotPoints → 20,
      MaxRecursion → 1],
     {{a, -0.3}, -4, 0}, {{b, -0.6},
      -4, 0},
      {{c, 0.8}, -4, 1}, {{d, 0}, -1, 1},
      {{x0, 16, "Solution Range"}, 5, 20},
      ControlPlacement → Left,
      ContentSize → {400, 300}]

    Images

  • A more realistic simulation is available at:

    http://demonstrations.wolfram.com/TsunamiStrikingALandscape

4.2.6 Bouncing Balls

  • This function combines Manipulate with DynamicModule to show bouncing balls. Each time you click inside the area, a new ball will be generated.

    Manipulate[
     DynamicModule[{contents = {}, lastpt = {0, 1}}, Graphics[{PointSize[0.1],
       Point[Dynamic[If[pt =!= lastpt, AppendTo[contents, {pt, {0, 0}}];
          lastpt = pt];
         contents = Map[If[#[[1, 2]] ≥ 0, {#[[1]] - #[[2]], #[[2]] +
               {0, 0.001}}, {{#[[1, 1]], 0}, {1, -0.8} #[[2]]}] &, contents];
         Map[First, contents]]], Line[{{0, -.05}, {1, -.05}}]},
       PlotRange → {{0, 1}, {-.1, 1}}]], {{pt, {0, 1}}, {0, 0},
       {1, 1}, Locator, Appearance → None}, SynchronousUpdating → True]

    Images

  • In this example we simulate the bounce of a ball falling down stairs. You can modify the impulse given to the ball, its rebound and the step size. Mathematica can solve the problem both analytically and numerically, by replacing DSolve with NDSolve (numeric solution).

    Manipulate[
      sol = DSolve[{y''[t] ? -9.8, y[0] == 13.5, y'[0] == 5, a[0] == 13,
         WhenEvent[y[t] - a[t] == 0, y'[t] → - c y'[t]],
         WhenEvent[Mod[t, n], a[t] → a[t] - 1]}, {y, a}, {t, 0, 8},
       DiscreteVariables → {a}];
      Plot[Evaluate[{y[t], a[t]} /. sol], {t, 0, 8}, Filling → {2 → 0}],
      {c, 0.6, 1},
      {n, 0.6, 1.75}]

    Images

4.2.7 Language Detection

  • One of Mathematica’s latest features as mentioned earlier is Classify . Here we use it to identify the language of a sentence.

    Classify["Language", {"the house is blue",
       "la maison est bleu", "la casa es azul", "das Haus ist blau",
       Images

    {English, French, Spanish, German, Chinese, Arabic, Ukrainian}

    Manipulate [Classify ["Language", sentence],
     {{sentence, "la casa es azul", "Sentence"}, {"the house is blue",
       "la maison est bleu", "la casa es azul", "das Haus ist blau",
       Images

    Images

4.3 Image Processing

The image processing functionality of Mathematica has greatly improved in the latest versions of the program. We are going to give a brief overview of it. You can extend your knowledge by going through this tutorial: tutorial/ImageProcessing. At the end of this chapter there are several links in case you may be interested in learning more about the topic. In the first chapter we saw that when you click on an image, the Image Assistant, a toolbar providing several options to modify the image will appear right below it. As a matter of fact, many of the operations that we will show here can be executed directly using this toolbar. However, for clarity purposes we will show the Mathematica commands instead of describing how to use the toolbar. Besides, if we were interested in creating a program we would have to type the required instructions.

4.3.1 Playing with The Giralda

  • We import a JPG image (in this example, The Giralda of Seville, although with a search engine you can find many images similar to the one below or even use other images as well). The resolution of the image was preserved when downloaded. You can also cut and paste from a website.

    Clear["Global` *" ] (* Remove all the variables from memory*)

  • Images
  • We can check the image size in pixels. It consists of a n×m grid of triplets, each one representing one pixel.

    ImageDimensions[giralda]

    {635, 635}

  • We can see the RGB value of the individual pixels:

    ImageData[giralda]

    Images

  • We adjust the contrast and the brightness (if you prefer, instead of copying and pasting the image, just type giralda).

    Images

    {{a, -1.5, "Contrast"}, -2, 2}, {{b, 0.5, "Brightness"}, 0, 1}]

    Images

  • The following command detects the edges of an image.

    edges = EdgeDetect[giralda]

    Images

  • Here we compare both images.

    GraphicsRow[{giralda, edges}]

    Images

  • ImagePartition splits an image into a collection of subimages of a certain size. (image downloaded from http://noticias-24.net/wp-content/uploads/2011/07/Machu-Picchu.jpg).

    Images

    Execute the function to see the output.

4.3.2 Background Removal and Image Composition

  • In this example we remove the background of an image and create a new one by combining the result with an image of the Andromeda Galaxy.

    foreground = RemoveBackground[ Images, {"Background", Green}];
    ImageCompose[ Images, foreground]

    Images

  • In the example below, available in the documentation, we eliminate small image components to extract a flock of flamingos.

    DeleteSmallComponents[
    RemoveBackground[Images, {"Background", {"Uniform", 0.08}}], 2]

Images

4.3.3 3D Images

Since Mathematica 9 there are many more functions for 3D image processing as this example shows:


ctHead = ExampleData[{"TestImage3D", "CThead"}];

head = Image3D[ctHead, BoxRatios → {1, 1, 1}];

Manipulate[
  Image3D[head, ClipRange → {{x, 256}, {0, y}, {z, 100}}],
  {{x, 100}, 0, 256, 1}, {{y, 150}, 0, 256, 1}, {{z, 50}, 0, 100, 1}]

Images

http://www.wolfram.com/mathematica/new-in-10/enhanced-3d-image-processing/index.html contains examples that may be useful for those readers interested in 3D image processing.

4.3.4 Eyesight Recovery

Next we show how to improve image resolution.

  • Here’s an example of an image with fuzzy text (you can find the picture in the ImageDeconvolve help file):

    Images

  • With a deconvolution we can substantially improve the image quality.

    ImageDeconvolve[ fuzzy, BoxMatrix[3] /49, Method → "TotalVariation"]

    Images

  • Here is simulate a signal with noise.

    data = Table[Sin[i^2 + i] + RandomReal[{-.3, .3}], {i, 0, Pi, 0.001}];
    ListLinePlot[data]

    Images

  • The next function uses a filter to remove the noise component of the signal.

    Manipulate[With[{res = LowpassFilter[data, ω]},
       GraphicsColumn[{Spectrogram[res], ListLinePlot[res]}]],
      {ω, Pi, .01, Appearance → "Labeled"}]

    Images

4.3.5 Edge Detection

Mathematica has several functions for detecting edges and borders that are very useful for image processing.

  • In the next example we capture the image of the author while writing this book and apply the FindFaces command to detect his face.

    picture = CurrentImage[]

    Images

    faces = FindFaces[picture, {100, ∞}]

    {{{145.5, 36.5}, {258.5, 149.5}}}

    Show[picture,
      Graphics[{EdgeForm[{Red, Thick}], Opacity[0], Rectangle @@@ faces}]]

    Images

4.3.6 Barcode Recognition

  • This example shows the use of BarcodeRecognize to identify barcodes.

    Images

    Images

    BarcodeRecognize[boardingPass, "Data", "PDF417"]

    M2VANALMSICK/MARKUS ECSEZLJ HNDDXBEK 0313 271Y018K0254
      136>20B1WW3271BEK 251762174509010 1 EK 116054035 CSEZLJ
      DXBDUSEK 0055 271Y019K0249 127251762174509011 1 EK 116054035

4.3.7 A Painting with a Surprise

The next example shows how to visualize an image from a certain perspective and how to select a part of it. The image was imported from a Wolfram blog post (http://blog.wolfram.com/2010/10/27/the-ambassadors).

ambassadors = Image[Import[
"http://blog.wolfram.com/data/uploads/2010/10/TheAmbassadorsBlogPost01
  .jpg"], ImageSize -> 150]

Images

The image above corresponds to the painting “The Ambassadors” by Holbein. There’s a skull in it. Can you see it?

  • With ImagePerspectiveTransformation we can apply a transformation to see the image from a different perspective and, combined with TransformationFunction, display the part of the image that we want to visualize.

    ImagePerspectiveTransformation[ambassadors,
    TransformationFunction[ (0.50.870.051.342.70.430.620.20.525)]]

    Images

4.3.8 Where Are the Matching Points?

Can you identify the location of the common points in these two images? (Images available in ImageCorrespondingPoints).

Images

  • The output displays the matching positions between the two images.

    matches = ImageCorrespondingPoints @@ imgs;
    MapThread[
      Show[#1, Graphics[{Yellow, MapIndexed[Inset[#2[[1]], #1] &, #2]}]] &,
      {imgs, matches}]

    Images

4.3.9 Image Identify

A new area of functionality available since Mathematica 10.1 is image identification: https://www.imageidentify.com/

  • The function ImageIdentify is used to identify the image in a picture. If you have a camera connected to your computer, you can use the command below to identify the captured image in real time.

    {#, ImageIdentify[#]} &[ CurrentImage[]]

    Images

4.3.10 Metadata

As we have previously seen, data from image files usually carry additional information about the images themselves. In this case we download a file in the ARC Grid format, used in topography for representing terrain elevation.

  • We import the following ARCGrid file.

    Import[ "http:// exampledata.wolfram.com/ ArcGRID.zip", "ARCGrid"]

    Images

  • Notice that this is a compressed file. We can decompress it to find out what files are included.

    Import["http://exampledata.wolfram.com/ArcGRID.zip", "ZIP"]

    {NED_93751926\.DS_Store, NED_93751926\info\arc.dir,
     NED_93751926\info\arc0000.dat, NED_93751926\info\arc0000.nit,
     NED_93751926\info\arc0001.dat, NED_93751926\info\arc0001.nit,
     NED_93751926\meta1.html, NED_93751926\Metadata.xml,
     NED_93751926\ned_93751926\dblbnd.adf, NED_93751926\ned_93751926\hdr.adf,
     NED_93751926\ned_93751926\prj.adf, NED_93751926\ned_93751926\sta.adf,
     NED_93751926\ned_93751926\w001001.adf,
     NED_93751926\ned_93751926\w001001x.adf, NED_93751926\NED_93751926.aux}

  • Next, we represent the terrain elevations in 3D (we use MaxPlotPoints→100 to speed up the computations).

    ListPlot3D[Import["http://exampledata.wolfram.com/ArcGRID.zip",
       {"ArcGRID", "Data"}], MaxPlotPoints → 100,
      ColorFunction → "SouthwestColors", Mesh → None]

    Images

  • Let’s take a look at the metadata inside.

    Import["http://exampledata.wolfram.com/ArcGRID.zip",
     {"ArcGRID", "Elements"}]

    {Centering, CentralScaleFactor, CoordinateSystem,
     CoordinateSystemInformation, Data, DataFormat, Datum, ElevationRange,
     Graphics, GridOrigin, Image, InverseFlattening, LinearUnits, Projection,
     ProjectionName, RasterSize, ReferenceModel, ReliefImage, SemimajorAxis,
     SemiminorAxis, SpatialRange, SpatialResolution, StandardParallels}

  • Now, we extract the information related to the coordinates system.

    Import[ "http://exampledata.wolfram.com/ArcGRID.zip",
     {"ArcGRID", "CoordinateSystemInformation"}]

    GEOGCS → {NAD83, DATUM → {North_American_Datum_1983,
         SPHEROID → {GRS 1980, 6 378 137, 298.257, AUTHORITY → {EPSG, 7019}},
         TOWGS84 → {0, 0, 0, 0, 0, 0, 0}, AUTHORITY → {EPSG, 6269}},
       PRIMEM → {Greenwich, 0, AUTHORITY → {EPSG, 8901}},
       UNIT → {degree, 0.0174533, AUTHORITY → {EPSG, 9108}},
       AXIS → {Lat, NORTH}, AXIS → {Long, EAST}, AUTHORITY → {EPSG, 4269}}

  • The following command enables us to create a link to http://maps.google.com using the latitude and the longitude.

    CoordinatesToMapLink[{lat_, long_}] := Hyperlink[
      StringJoin["http://maps.google.com/maps?q=+",
       ToString@lat, "+", ToString@long, "&z=12&t=h"]
      ]

  • We can now get the coordinates of the previous file and set up a link to Google Maps.

    coords = Import[ "http://exampledata.wolfram.com/ArcGRID.zip",
       {"ArcGRID", "SpatialRange"}]
    {{40.0617, 40.1447}, {-88.3158, -88.1619}}

  • Since we are establishing the link with the center of the imported grid, we need to calculate the average of the latitude and longitude.

    CoordinatesToMapLink[Map[Mean, coords]]

    http://maps.google.com/maps?q=+40.1032+-88.2389&z=12&t=h

4.4 Graphs and Networks

Graphs and networks is another area where the latest Mathematica versions incorporate new functionality: guide/GraphsAndNetworks. A graph can be represented using Graph, a function with many options. Let’s see a basic example.

4.5 Mazes

The function Thinning looks for the skeleton of an image through morphological analysis.

4.6 Application: Finding the Period of a Pendulum

Clear["Global` *" ]

The example that follows is significantly more difficult than the previous ones (obtained from http://www.wolfram.com/services/education/seminars/s51.html).

Our objective is to find out the period of a pendulum by analyzing a sequence of video images. The images were obtained with CurrentImage (if your computer has a webcam you can use this function). CurrentImage adds a time stamp to the image, information that we will use later on in the example:

Images

With ImageDisplacements, you can use an optical flow to interpolate between existing frames (https://www.wolfram.com/language/11/image-and-signal-processing/slow-motion-using-optical-flow.html). In the following example we generate a video sequence twice as long as the original.

4.7 Advanced Calculus

Clear["Global`*"]

So far in this chapter we have seen some of the new functionality included in the latest versions of Mathematica. In this section we continue with our exploration of some of the latest features and their applications for solving advanced calculus problems. It’s important to remember that some of the options that we use are common to other functions.

4.8 Additional Resources

To access the following resources, if they refer to the help files, write them down in a notebook, select them and press <F1>. In the case of external links, copy them in a browser.

About demonstrations

Manipulate: tutorial/IntroductionToManipulate

Advanced Manipulate functionality tutorial: tutorial/AdvancedManipulateFunctionality

Introduction to Dynamic tutorial: tutorial/IntroductionToDynamic

The Wolfram Demonstrations Project: http://demonstrations.wolfram.com

About images

Image processing tutorial: tutorial/ImageProcessing

Image Processing & Analysis guide page: guide/ImageProcessing;

Image and Signal Processing: http://www.wolfram.com/language/11/image-and-signal-processing/

Computational Photography: http://www.wolfram.com/language/11/computational-photography/

A collection of video presentations and seminars available in: http://www.wolfram.com/training/courses/image-processing/

An interesting demonstration about image processing: The Incredible Convenience of Mathematica Image Processing (http://blog.wolfram.com/2008/12/01/the-incredible-convenience-of-mathematica-image-processing)

The Wolfram Language Image identification Project: https://www.imageidentify.com/

About Advanced Calculus

Symbolic & Numeric Calculus: http://www.wolfram.com/language/11/symbolic-and-numeric-calculus/

Differential Eigensystems: http://www.wolfram.com/language/11/differential-eigensystems/ Algebra and Number Theory: http://www.wolfram.com/language/11/algebra-and-number-theory/

Partial Differential Equations: http://www.wolfram.com/language/11/partial-differential-equations/