Advertisement
Matthen

River Meandariness

May 7th, 2014
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. frames0 = Import["http://i.imgur.com/Uak4YU3.gif"];
  2. w = 600; h = 470;
  3. frames = Map[ImageCrop[#, {w, h}, {Right, Bottom}] &, frames0];
  4. frames = Delete[frames, 4];
  5. frames = Delete[frames, 18];
  6. frames = Delete[frames, 18];
  7. solutions = {};
  8. i = 6;
  9. Do[
  10. path = Thinning[
  11. DeleteSmallComponents[Binarize[frames[[i]], 0.36], 1000]];
  12. pathCoords = Position[ImageData[path], 1];
  13. f[p_] := {p[[2]], h - p[[1]]};
  14. g[p_] := {h - p[[2]], p[[1]]};
  15. pathCoords = f /@ pathCoords;
  16. pathCoords = SortBy[pathCoords, w #[[2]] + #[[1]] &];
  17. start = First[pathCoords];
  18. end = Last[pathCoords];
  19. pointNeighbours[{x_, y_}] := (
  20. Intersection[pathCoords,
  21. {{x + 1, y}, {x, y + 1}, {x - 1, y}, {x, y - 1},
  22. {x + 1, y + 1}, {x + 1, y - 1}, {x - 1, y + 1}, {x - 1, y - 1}
  23. }]
  24. );
  25. graphEdges[{x_, y_}] :=
  26. UndirectedEdge[{x, y}, #] & /@ pointNeighbours[{x, y}];
  27. riverGraph =
  28. Graph[Union[Map[Sort, Flatten[graphEdges /@ pathCoords]]]];
  29. solution = FindShortestPath[riverGraph, start, end];
  30. AppendTo[solutions, solution];
  31. ,
  32. {i, Length@frames}];
  33. frameI[i_] := Show[
  34. frames[[i]],
  35. Graphics[{Thick, Lighter@Blue, Opacity[1.0],
  36. Arrowheads[{-.05, .05}], Line[solutions[[i]]], Lighter@Red,
  37. Arrow[{solutions[[i, 1]], solutions[[i, -1]]}],
  38. Inset[
  39. ListPlot[s[[;; i]], Joined -> True,
  40. PlotRange -> {{0, 35}, {0, Pi}}, Axes -> {False, True},
  41. AxesStyle -> White, PlotStyle -> Directive[Thick, White]],
  42. {10, 10}, {Left, Bottom}, 300
  43. ]
  44. }]
  45. , ImageSize -> 300];
  46. Manipulate[
  47. frameI[i]
  48. , {i, 1, Length@frames, 1}]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement