Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
4.3 KiB

  1. This post will extend my last post on image [quadtrees](https://jrtechs.net/photography/segmenting-images-with-quadtrees) to create an animation that varies the quadtree splitting threshold.
  2. Like all recursively dividing algorithms, as you relax the splitting parameter, more partitions get generated.
  3. Although this principle makes intuitive sense, seeing animation tells a fuller story.
  4. This post will be using the matplotlib's animation functionality and the quadtree code I wrote in my [previous post](https://jrtechs.net/photography/segmenting-images-with-quadtrees). The following code snippet illustrates a simple animation using matplolib:
  5. ```python
  6. import numpy as np
  7. from matplotlib import pyplot as plt
  8. from matplotlib.animation import FuncAnimation
  9. fig = plt.figure()
  10. ax = plt.axes(xlim=(0, 4), ylim=(-2, 2))
  11. line, = ax.plot([], [], lw=3)
  12. def init():
  13. line.set_data([], [])
  14. return line,
  15. def animate(i):
  16. x = np.linspace(0, 4, 1000)
  17. y = np.sin(2 * np.pi * (x - 0.01 * i))
  18. line.set_data(x, y)
  19. return line,
  20. anim = FuncAnimation(fig, animate, init_func=init,
  21. frames=200, interval=20, blit=True)
  22. anim.save('Wave.gif')
  23. ```
  24. ![Simple Animation](media/quad-tree/Wave2.gif)
  25. In this post, I will be using the quadtree on a picture of a street light at night.
  26. Note that the details in this image are on the telephone pole and the moon.
  27. This detail is vital because when discretizing it with a quadtree, we expect that those two areas would receive smaller cells than the rest of the image.
  28. ![Sample Image](media/quad-tree/night2.jpg)
  29. After applying some minor altercations to our original animation code, we can use it to animate our quadtree wireframe. This code will create 30 frames in our Gif where each frame is 500ms-- half a second.
  30. Additionally, the loop starts at zero and increments by one each frame. I added some normalization to our loop to ensure that the first frame contains the largest value and the last frame receives the smallest splitting value-- starts the animation with fewer cells and finishes with more cells.
  31. ```python
  32. fig = plt.figure(figsize=(10, 10))
  33. plt.title("Quadtree")
  34. MAX_FRAMES=30
  35. def animate(i):
  36. fig.clear(True)
  37. qtTemp = QTree((MAX_FRAMES-i)/3, 3, img) #contrast threshold, min cell size, img
  38. qtTemp.subdivide() # recursively generates quad tree
  39. qtTemp.graph_tree(fig=fig, display=False)
  40. return fig,
  41. anim = FuncAnimation(fig, animate,
  42. frames=MAX_FRAMES, interval=500, blit=True)
  43. anim.save('quad_tree_wire_frame.gif')
  44. ```
  45. ![Wire Frame Animation](media/quad-tree/quad_tree_wire_frame.gif)
  46. Modifying the code further, we can animate the quadtree while discretizing the image.
  47. ```python
  48. fig = plt.figure(figsize=(10, 10))
  49. plt.title("Quadtree")
  50. MAX_FRAMES=30
  51. cell_count=[]
  52. dividing_threshold=[]
  53. def animate(i):
  54. fig.clear(True)
  55. qtTemp = QTree((MAX_FRAMES-i)/3, 3, img) #contrast threshold, min cell size, img
  56. qtTemp.subdivide() # recursively generates quad tree
  57. #qtTemp.graph_tree(fig=fig, display=False)
  58. img_render = qtTemp.render_img()
  59. rgb = cv2.cvtColor(img_render, cv2.COLOR_BGR2RGB)
  60. plt.gca().imshow(rgb)
  61. cell_count.append(len(find_children(qtTemp.root)))
  62. dividing_threshold.append((MAX_FRAMES-i)/3)
  63. return fig,
  64. anim = FuncAnimation(fig, animate,
  65. frames=MAX_FRAMES, interval=500, blit=True)
  66. anim.save('quad_tree_animation_image.gif', writer='imagemagick')
  67. ```
  68. ![Quad Tree Animation](media/quad-tree/quad_tree_animation.gif)
  69. Observing the image, we can see that cells don't get added to the quadtree linearly. There are a few frames in the animation where the number of cells added makes a sudden jump.
  70. We can further investigate this by plotting the error threshold against the number of cells produced.
  71. ```python
  72. plt.title("Error Threshold vs. Number of Cells")
  73. plt.plot(dividing_threshold, cell_count)
  74. plt.xlabel("Error Threshold")
  75. plt.ylabel("Number of Cells")
  76. plt.savefig("error_graph.png")
  77. ```
  78. ![Error Graph](media/quad-tree/error_graph.png)
  79. This graph shows that the most significant changes in the number of cells occur when you initially start increasing the threshold and then again around 4.5.
  80. But, that is only for this image; different images will react differently to varying the error threshold. Moreover, the image's resolution will also affect the number of cells generated at a particular threshold.