Mathematica and Matlab Scripts
Total Page:16
File Type:pdf, Size:1020Kb
Appendix A Mathematica and Matlab Scripts This Appendix contains Mathematica and Matlab scripts referenced in the chapters. Wolfram Mathematica® 10 is used to write the MScripts (Math scripts) and Matlab® 7.10.0 (R2010a) is also used to write the Matlab scripts. A.1 Scripts from Chap. 1 Mscript 16 Convex 2D region centroid. (* 2D Region centroid *) R = ConvexHullMesh[RandomReal[1, {8, 2}]]; c = RegionCentroid[R] Show[HighlightMesh[R, Style[{2, Black}, Opacity[0.2]]], Graphics[{Red, PointSize[0.02], Point[RegionCentroid[R]]}]] © Springer International Publishing Switzerland 2016 355 J.F. Peters, Computational Proximity, Intelligent Systems Reference Library 102, DOI 10.1007/978-3-319-30262-1 356 Appendix A: Mathematica and Matlab Scripts A.1.1: Corner A.1.2: Intensity A.1.3: Keypoint Fig. A.1 Three Dirichlet tessellations Mscript 17 StanfordBunny centroid. (* 3D Region centroids *) gr = ExampleData[{“Geometry3D”, “StanfordBunny”}]; gm = DiscretizeGraphics[gr]; c2 = RegionCentroid[gm] Show[Graphics3D[Prepend[First[gr], Opacity[0.5]]], Graphics3D[{Red, PointSize[0.03], Point@c2}]] Mscript 18 UNISA coin centroid. (* Digital Image Region Centroid *) img =; c = ComponentMeasurements[img, “Centroid”][[All, 2]][[1]]; Show[img, Graphics[{Black, PointSize[0.02], Point[c]}]] See, e.g., (Fig. A.1) for three types tessellations obtained using Mscript 19. Mscript 19 Tessellations from Three Different Types of Seed Points. (*Corner, intensity, keypoint − based Dirichlet Tessellations*) img =; ImageDimensions[img]; corncoords = ImageCorners[img, MaxFeatures → 50]; HighlightImage[img, corncoords] ListPlot[corncoords, PlotStyle →{Red, PointSize[.01]}, PlotLegends →{“corner”}, AxesLabel →{wpixels, ht(pixels)}, LabelStyle → Directive[Blue, Bold]] (* Give the Voronoï mesh and display on the image *) Appendix A: Mathematica and Matlab Scripts 357 vm = VoronoiMesh[corncoords]; t12 = Graphics[{Red, {PointSize[0.02], Point[corncoords]}, Blue, MeshPrimitives[vm, 1], {Opacity[0.1], EdgeForm[{Thick, Yellow}], FaceForm[Yellow]}}] Show[img, t12] (*p = ImageValue[img, {300, 200}]*) pts = ImageValuePositions[img, White, 0.45]; vm2 = VoronoiMesh[pts]; t13 = Graphics[{Red, {PointSize[0.02], Point[pts]}, Blue, MeshPrimitives[vm2, 1], {Opacity[0.1], EdgeForm[{Thick, Yellow}], FaceForm[Yellow]}}] Show[img, t13] corncoords2 = ImageKeypoints[img, MaxFeatures → 60]; vm3 = VoronoiMesh[corncoords2]; t15 = Graphics[{Red, {PointSize[Large], Point[corncoords2]}, Blue, MeshPrimitives[vm3, 1], {Opacity[0.1], EdgeForm[{Thick, Yellow}], FaceForm[Yellow]}}] Show[img, t15] Mscript 20 Conventional near sets. (*Conventional near sets : Cech, Efremovich, Lodato, ...*) Plot[1/x, {x, −10, 10}, AxesLabel →{x, 1/x}, PlotRange → {{−10, 10}, {−1, 1}}, LabelStyle → Directive[Blue, Bold]] 358 Appendix A: Mathematica and Matlab Scripts Mscript 21 Strongly near sets. (*Strongly near sets : intersection of A, B > 1*) Plot[Sin[8/x], {x, 0.1, 10}, AxesLabel →{x, Sin[8/x]}, PlotRange →{{0, 2}, {−1.1, 1.1}}, LabelStyle → Directive[Blue, Bold]] Mscript 22 Mesh on a Hooke Needle Point Image. (* Mesh Containing Multiple Nearves *) img = ; corncoords = ImageCorners[img, MaxFeatures → 60]; HighlightImage[img, corncoords] (* Give the Voronoï mesh and display on the image *) vm = VoronoiMesh[corncoords]; t11 = Graphics[{Blue, {PointSize[Large], Point[corncoords]}, Black, MeshPrimitives[vm, 1]}] t12 = Graphics[{Red, {PointSize[Large], Point[corncoords]}, Green, MeshPrimitives[vm, 1], {Opacity[0.1], EdgeForm[{Thick, Yellow}], FaceForm[Yellow]}}] Show[img, t12] Mscript 23 Image Raster Mesh. (* Nearness of image pixel keypoint mesh parts *) img = ; Rasterize[img, RasterSize → 32](*32pixelwiderasterization.*) x1 = ImageKeypoints[img]; HighlightImage[img, x1, “HighlightColor” → White]; P1 = ImageKeypoints[img, {“Position”, “Scale”, “Orientation”, “ContrastSign”}, MaxFeatures → 100]; Show[img, Graphics[ Table[{{If[p[[4]] == 1, Red, Green], Circle[p[[1]], p[[2]] ∗ 3.5], Line[{p[[1]], p[[1]] + p[[2]] ∗ 1.5 ∗{Cos[p[[3]]], Sin[p[[3]]]}}]}}, {p, P1}]]] Mscript 24 Image Keypoint Mesh Nerves. (* Mesh Containing Multiple Keypoint Nearves *) img = ; Appendix A: Mathematica and Matlab Scripts 359 corncoords = ImageKeypoints[img, MaxFeatures → 60]; HighlightImage[img, corncoords] (* Give the Voronoï mesh and display on the image *) vm = VoronoiMesh[corncoords]; t11 = Graphics[{Blue, {PointSize[Large], Point[corncoords]}, Black, MeshPrimitives[vm, 1]}] t12 = Graphics[{Red, {PointSize[Large], Point[corncoords]}, Green, MeshPrimitives[vm, 1], {Opacity[0.1], EdgeForm[{Thick, Yellow}], FaceForm[Yellow]}}] Show[img, t12] Mscript 25 Mesh on an Annulus. (*Set up a boundedness that is amesh circular region. Each inner circular region is aproximal nerve.*) annulus[x_, y_]:=(9/10)∧2 ≤ x∧2 + y∧2 ≤ 1∧2 holes[{x0_, y0_}, r_]:=((x + x0)∧2 + (y + y0)∧2 ≤ (r)∧2) crds = {{−1/2, 0}, {1/2, 0}, {0, −1/2}, {0, 1/2}, {2/5, 2/5}, {−2/5, −2/5}, {2/5, −2/5}, {−2/5, 2/5}}; sd = Or@@(holes[#, 1/64]&/@crds); Ω2 = ImplicitRegion[Or[annulus[x, y], sd], {x, y}]; ToElementMesh[Ω2, “BoundaryMeshGenerator” →{“Continuation”}, “RegionHoles” → crds][ “Wireframe”[“MeshElementStyle” →{Directive[FaceForm[Green], EdgeForm[{Red}]]}]] Mscript 26 Four-Dimensional Array of Cubes. (*Four − dimensional array of cubes.*) Graphics3D[Raster3D[RandomReal[1, {8, 5, 5, 3}], ColorFunction → (RGBColor[#[[1]], #[[2]], #[[3]], 0.299#[[1]] + 0.587#[[2]] + 0.114#[[3]]]&)]] 360 Appendix A: Mathematica and Matlab Scripts A.2 Scripts from Chap. 2 Mscript 27 Delaunay and Voronoï Mesh on Hand Image CornerDelaunay.nb. (*Corner − based Delaunay triangulation*) img = ; ImageDimensions[img] corncoords = ImageCorners[img, MaxFeatures → 50] HighlightImage[img, corncoords] ListPlot[corncoords, PlotStyle →{Black, PointSize[.01]}, PlotLegends →{“corner”}, AxesLabel →{wpixels, ht(pixels)}, LabelStyle → Directive[Blue, Bold]] (* Give the Delaunay mesh and display on the image *) vm = DelaunayMesh[corncoords]; dm = VoronoiMesh[corncoords]; t11 = Graphics[{Blue, {PointSize[0.02], Point[corncoords]}, Black, MeshPrimitives[vm, 1]}] t12 = Graphics[{Red, {PointSize[0.02], Point[corncoords]}, Blue, MeshPrimitives[vm, 1], {Opacity[0.1], EdgeForm[{Thick, Yellow}], FaceForm[Yellow]}}] t13 = Graphics[{Red, {PointSize[0.02], Point[corncoords]}, Yellow, MeshPrimitives[dm, 1], {Opacity[0.1], EdgeForm[{Thick, Yellow}], FaceForm[Yellow]}}] t14 = Graphics[{Red, {PointSize[0.02], Point[corncoords]}, Blue, MeshPrimitives[dm, 1], {Opacity[0.1], EdgeForm[{Thick, Yellow}], FaceForm[Yellow]}}] Show[img, t12] Show[img, t13] Show[img, t12, t13] Appendix A: Mathematica and Matlab Scripts 361 % centroid−based image Delaunay and Voronoi mesh clc, clear all, close all im = imread(’redcarFrame.jpg’ ); % if size(im,3)==3 % g=rgb2gray(im) ; %end [m,n]=size(im); bw = im2bw(im,0.5); % threshold at 50% bw = bwareaopen(bw,2); % remove objects less 2 than pixels stats = regionprops(bw,’Centroid’ ); % centroid coordinates centroids = cat(1,stats.Centroid); fc=[1 1;n 1; 1 m; nm]; % identify image corners centroids=[centroids;fc]; imshow(im),hold on plot(centroids(:,1) ,centroids(:,2) ,’y+’ ) hold on; X=centroids(:,1); Y=centroids(:,2); %find delaunay triangulation TRI = delaunay(X,Y); triplot(TRI,X,Y,’y’ ); %find Voronoi tessellation [vx,vy]=voronoi(X,Y); plot(vx,vy,’g-’); Listing A.1 Matlab script in findCentroidalMesh.m to construct a centroidal-based mesh. Mscript 28 Centroid-based Voronoï mesh: ch2centroidalMesh.nb. (*Mesh experiment with centroids.*) pts = RandomReal[1, {100, 2}]; R = VoronoiMesh[pts, {{0, 1}, {0, 1}}]; 362 Appendix A: Mathematica and Matlab Scripts vcf[pts_]:= RegionCentroid/@MeshPrimitives[VoronoiMesh[pts, {{0, 1}, {0, 1}}], “Faces”]; centroids = NestList[vcf, pts, 3]; vm = VoronoiMesh[#, {{0, 1}, {0, 1}}]&/@centroids MapThread[Show[#1, Graphics[{Black, Point[#2], Red, Point[vcf[#2]]}]]&, {vm, centroids}] Show[R, Graphics[{Red, Point[pts]}]] Mscript 29 Centroid-based Voronoï mesh: centroidCornerVoronoiMesh.nb. (*Centroids N.B. position of PointSize[0.01]*) Im1 = ; im1corners = ImageCorners[Im1, 3, 0.01, 5]; im1vm = VoronoiMesh[im1corners]; im1centroids = RegionCentroid/@MeshPrimitives[im1vm, “Faces”]; centroids = Graphics[{PointSize[0.01], Red, Point[im1centroids]}]; corners = Graphics[{PointSize[0.01], Yellow, Point[im1corners]}]; ptsCorners = MeshCells[im1vm, 1]; mesh = VoronoiMesh[im1centroids]; mesh2 = VoronoiMesh[centroids]; pts = MeshCoordinates[mesh]; lines = MeshCells[mesh, 1]; vm = MeshRegion[pts, lines] mesh3 = HighlightMesh[vm, Style[1, Green]] Show[Im1, vm] Show[Im1, centroids] Show[Im1, vm, centroids] Show[Im1, mesh3, centroids] Show[Im1, mesh3, centroids, corners] Appendix A: Mathematica and Matlab Scripts 363 A.3 Scripts from Chap. 3 Mscript 30 Unbounded descriptive Nbd: UnboundedDescriptiveNbd.nb. (* Unbounded Descriptive Neighbourhood of a Point *) x = ; ImageDimensions[x] p = ImageValue[x, {300, 190}] im = Show[x, Frame → True] pts = ImageValuePositions[im, p, 0.022]; HighlightImage[im, pts, “HighlightColor” → Red] Note The following picture neighbourhood GUI is a minor revision of a script written by Binglin Li. A sample bounded descriptive nhdb using the neigbourhood GUI is shown in Fig.A.2. function varargout = Nbds(varargin) % NBDS MATLAB c o d e f o r Nbds. fig % NBDS, by itself , creates a newNBDSor raises the existing % singleton∗. % % H = NBDS returns the handle to a new NBDS or the handle to Fig. A.2 Bounded descriptive Nbd at (347,320), r = 100, ε = 50 364 Appendix A: Mathematica and Matlab Scripts % the existing singleton∗. % % NBDS(’CALLBACK’,hObject,eventData,handles