Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. #
  5. # The purpose of this script is to get a feeling what the transformation from spatial to fourier domain does.
  6. #
  7. # The user can set white or black pixels in the spatial domain and the fourier domain will be updated accordingly.
  8. # The left mouse button creates white pixels (value = 1), the right mouse button black pixels (value = 0).
  9. # Both one-time clicks work as well as dragging (moving while button is pressed down) the mouse.
  10. # The drawing radius can be changed with the + and - keys, also during dragging.
  11. # The "i"-key inverts the spatial data (0s become 1s and 1s become 0s), the "r"-key resets it to 0s.
  12. #
  13.  
  14.  
  15. n = 100
  16. drawradius = 5 # pixels the user edits with one click, the clicked cell is in the middle
  17. # drawradius 1 means 1 pixel, 2 means 9 pixels (wrapping the clicked cell) and so on
  18.  
  19.  
  20. def spatial_to_fourier():
  21. return np.real(np.fft.fftshift(np.fft.fft2(data_spatial)))
  22.  
  23.  
  24. def fourier_to_spatial(fourier_data): # TODO
  25. return # allow pixel-manipulation in the fourier domain and change the spatial domain accordingly
  26. # but what editing operations make sense?
  27.  
  28.  
  29. fig_spatial, ax_spatial = plt.subplots()
  30. ax_spatial.set(title='Spatial domain')
  31. data_spatial = np.zeros((n, n)) # np.random.random((n, n))
  32. im_spatial = ax_spatial.imshow(data_spatial, cmap='gray', origin='lower')
  33.  
  34. fig_fourier, ax_fourier = plt.subplots()
  35. ax_fourier.set(title='Fourier domain')
  36. data_fourier = spatial_to_fourier()
  37. im_fourier = ax_fourier.imshow(data_fourier, origin='lower')
  38.  
  39. row, col = 0, 0
  40.  
  41.  
  42. def edit_values(value):
  43. for r in range(row - drawradius + 1, row + drawradius):
  44. for c in range(col - drawradius + 1, col + drawradius):
  45. if 0 <= r < n and 0 <= c < n:
  46. data_spatial[r, c] = value
  47.  
  48.  
  49. def update_figures():
  50. # update spatial
  51. im_spatial.set_data(data_spatial)
  52. im_spatial.autoscale()
  53. fig_spatial.canvas.draw()
  54. # update fourier
  55. im_fourier.set_data(spatial_to_fourier())
  56. im_fourier.autoscale() # adjust vmin/vmax automatically to new set of values
  57. fig_fourier.canvas.draw()
  58.  
  59.  
  60. def handle_mouse_event(event):
  61. global row, col
  62. if (event.inaxes is None) or (event.button is not 1 and event.button is not 3): # 1: left, 3: right
  63. return
  64. current_col = int(event.xdata + 0.5)
  65. current_row = int(event.ydata + 0.5)
  66. if current_col == col and current_row == row: # still in same cell
  67. return
  68. col = current_col
  69. row = current_row
  70. edit_values(1 if event.button == 1 else 0)
  71. update_figures()
  72.  
  73.  
  74. def handle_key_released(event):
  75. global data_spatial, drawradius
  76. if event.key == 'i': # invert
  77. for r in range(0, n):
  78. for c in range(0, n):
  79. data_spatial[r, c] = 1 - data_spatial[r, c] # toggle 1s and 0s
  80. update_figures()
  81. print 'inverted spatial data'
  82. if event.key == 'r': # reset
  83. data_spatial = np.zeros((n, n))
  84. update_figures()
  85. print 'reset spatial data'
  86. if event.key == '+':
  87. drawradius += 1
  88. print 'drawradius: ' + str(drawradius)
  89. if event.key == '-':
  90. if drawradius > 1:
  91. drawradius -= 1
  92. print 'drawradius: ' + str(drawradius)
  93.  
  94.  
  95. fig_spatial.canvas.mpl_connect('key_release_event', handle_key_released)
  96. # have mouse release events as well as mouse motion events trigger handle_mouse_event(), to enable editing by both click and dragging
  97. fig_spatial.canvas.mpl_connect('button_release_event', handle_mouse_event)
  98. fig_spatial.canvas.mpl_connect('motion_notify_event', handle_mouse_event)
  99.  
  100. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement