Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. # %matplotlib inline
  2. %matplotlib notebook
  3.  
  4. from ipywidgets import interact, widgets, Layout
  5. import matplotlib.pyplot as plt
  6. from matplotlib.image import AxesImage
  7. from IPython.display import display
  8. from numpy.fft import fftshift, fft2, ifft2, ifftshift
  9. import time
  10. from skimage import transform
  11.  
  12. def makeGaussian(size, fwhm = 3, center=None):
  13. if isinstance(size, int):
  14. size = (size, size)
  15. if center:
  16. assert isinstance(center, tuple)
  17. """
  18. if size is tuple - (y_size, x_size)
  19. if center is tuple - (y_size, x_size)
  20.  
  21. """
  22.  
  23. y, x = np.meshgrid(np.arange(size[1]), np.arange(size[0]))
  24.  
  25. if center is None:
  26. x0 = size[1] // 2
  27. y0 = size[0] // 2
  28. else:
  29. x0 = center[1]
  30. y0 = center[0]
  31. notnorm_gaus = np.exp(-((x-x0)**2 + (y-y0)**2) / fwhm**2)
  32. return notnorm_gaus/notnorm_gaus.max()
  33.  
  34. class Image_analizer(object):
  35. '''
  36. Allow to make fast analysis of image among ferquences and dynamic range.
  37. Image must be grayscale, nunmpy array, 2D or 3D(time domain)
  38. '''
  39. def __init__(self, figsize=(6,6), imgsize=(512,512)):
  40. self.dynamic_range = None
  41. self.seq_frame = None
  42. self.hight_freq_pass = None
  43. self.low_freq_pass = None
  44. self.figsize = figsize
  45. self.imgsize = imgsize
  46.  
  47. def process_frame(self, frame):
  48. '''
  49. produce 2D image processed according with selected parameters
  50. '''
  51. if not self.hight_freq_pass == 0:
  52. gaussian = makeGaussian(frame.shape, self.hight_freq_pass)
  53. fft_data = fftshift(fft2(ifftshift(frame)))
  54. filtered_fft = fft_data*gaussian
  55. frame = np.abs(fftshift(ifft2(ifftshift(filtered_fft))))
  56. if not self.low_freq_pass == 0:
  57. gaussian = makeGaussian(frame.shape, self.low_freq_pass)
  58. fft_data = fftshift(fft2(ifftshift(frame)))
  59. filtered_fft = fft_data*(1-gaussian)
  60. frame = np.abs(fftshift(ifft2(ifftshift(filtered_fft))))
  61. frame = np.clip(frame,*bounds)
  62. return frame
  63.  
  64. def show(self, data):
  65. '''
  66. call iteractive tool, for frequnces and dynamic range selection
  67. '''
  68. if len(data.shape) == 2:
  69. data = data[np.newaxis, ...]
  70. if self.dynamic_range is None:
  71. self.dynamic_range = (0, data.max())
  72. else:
  73. self.dynamic_range = tuple(np.clip(self.dynamic_range, 0, data.max()))
  74. if self.seq_frame is None:
  75. self.seq_frame = 0
  76. else:
  77. self.seq_frame = np.clip(self.seq_frame, 0, len(data))
  78. if self.hight_freq_pass is None:
  79. self.hight_freq_pass = 0
  80. if self.low_freq_pass is None:
  81. self.low_freq_pass = 0
  82. print(data.min(), data.max())
  83. print(self.dynamic_range)
  84. data = transform.resize(data, (data.shape[0],*self.imgsize), preserve_range=True)
  85.  
  86. def update(bounds, step, hp, lp):
  87. '''
  88. called each widget position update
  89. '''
  90. self.hight_freq_pass = hp
  91. self.low_freq_pass = lp
  92. self.seq_frame = step
  93. self.dynamic_range = bounds
  94. tic = time.time()
  95. d = data[step]
  96. if not hp==0:
  97. gaussian = makeGaussian(data.shape[1:], hp)
  98. fft_data = fftshift(fft2(ifftshift(d)))
  99. filtered = fft_data*gaussian
  100. d = np.abs(fftshift(ifft2(ifftshift(filtered))))
  101. if not lp==0:
  102. gaussian = makeGaussian(data.shape[1:], lp)
  103. fft_data = fftshift(fft2(ifftshift(d)))
  104. filtered = fft_data*(1-gaussian)
  105. d = np.abs(fftshift(ifft2(ifftshift(filtered))))
  106. d = d - d.min()
  107. img_obj.set_data(d)
  108. img_obj_init.set_data(data[step])
  109. img_obj.set_clim(*bounds)
  110. fig.canvas.draw()
  111. # display(fig)
  112. print('Processing time took {}'.format(time.time() - tic), end='\r')
  113.  
  114. slider_r = widgets.FloatRangeSlider(
  115. value=[self.dynamic_range[0], self.dynamic_range[1]],
  116. min=0,#data.min(),
  117. max=data.max(),
  118. step=(data.max()-data.min())/1000,
  119. description='range:',
  120. disabled=False,
  121. continuous_update=False,
  122. orientation='horizontal',
  123. readout=True,
  124. readout_format='.1f',
  125. layout=Layout(width='80%')
  126. )
  127.  
  128. slider_s = widgets.IntSlider(
  129. value=self.seq_frame,
  130. min=0,
  131. max=len(data)-1,
  132. step=1,
  133. description='step',
  134. disabled=False,
  135. continuous_update=False,
  136. orientation='horizontal',
  137. readout=True,
  138. readout_format='d',
  139. layout=Layout(width='80%')
  140. )
  141.  
  142. slider_hp = widgets.FloatSlider(
  143. value=self.hight_freq_pass,
  144. min=0,
  145. max=data.shape[1],
  146. step=data.shape[1]/1000,
  147. description='hight_pass',
  148. disabled=False,
  149. continuous_update=False,
  150. orientation='horizontal',
  151. readout=True,
  152. readout_format='f',
  153. layout=Layout(width='80%')
  154. )
  155.  
  156. slider_lp = widgets.FloatSlider(
  157. value=self.low_freq_pass,
  158. min=0,
  159. max=data.shape[1],
  160. step=data.shape[1]/1000,
  161. description='low_pass',
  162. disabled=False,
  163. continuous_update=False,
  164. orientation='horizontal',
  165. readout=True,
  166. readout_format='f',
  167. layout=Layout(width='80%')
  168. )
  169. fig, ax = plt.subplots(1,2, figsize=self.figsize)
  170. img_obj = ax[0].imshow(data[0], cmap='gray')
  171. img_obj_init = ax[1].imshow(data[0], cmap='gray')
  172. # plt.close()
  173. interact(update, bounds=slider_r, step=slider_s, hp=slider_hp, lp=slider_lp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement