generate_fringe.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import os
  2. import argparse
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. def parse_args():
  6. parser = argparse.ArgumentParser(description="")
  7. parser.add_argument("--shifts", type=int, default=4, help="相移步数")
  8. parser.add_argument("--img_w", type=int, default=2360, help="图片宽度")
  9. parser.add_argument("--img_h", type=int, default=1640, help="图片高度")
  10. parser.add_argument("--freq_x", type=int, default=32, help="x轴相移频率")
  11. parser.add_argument("--freq_y", type=int, default=1, help="y轴相移频率")
  12. args = parser.parse_args()
  13. return args
  14. def generate_phase_shifted_patterns(width, height, shifts, orientation='horizontal', frequency=1):
  15. """
  16. Generates multiple phase-shifted fringe patterns, either horizontal or vertical, with phase shifting along the stripes.
  17. :param width: image width
  18. :param height: image height
  19. :param shifts: number of phase shifts
  20. :param orientation: 'horizontal' or 'vertical' indicating the direction of the stripes
  21. :param frequency: number of full wave cycles across the dimension
  22. :return: array of generated fringe patterns
  23. """
  24. patterns = []
  25. for i in range(shifts):
  26. shift = (i * 2 * np.pi) / shifts # Calculating the phase shift for each pattern
  27. if orientation == 'horizontal':
  28. x = np.linspace(0, 2 * np.pi * frequency, width) + shift # Apply frequency scaling on horizontal stripes
  29. pattern = np.sin(x)
  30. pattern = np.tile(pattern, (height, 1)) # Extend the pattern vertically across the height
  31. elif orientation == 'vertical':
  32. y = np.linspace(0, 2 * np.pi * frequency, height) + shift # Apply frequency scaling on vertical stripes
  33. pattern = np.sin(y)
  34. pattern = np.tile(pattern.reshape(-1, 1), (1, width)) # Extend the pattern horizontally across the width
  35. patterns.append(pattern)
  36. return patterns
  37. if __name__ == "__main__":
  38. args = parse_args()
  39. width = args.img_w
  40. height = args.img_h
  41. shifts = args.shifts # Number of phase shifts
  42. frequencies = [args.freq_x, args.freq_y] # Two different frequencies
  43. # Ensure the directory exists
  44. directory = f'imgs/patterns_{width}x{height}_{shifts}_fx{args.freq_x}_fy{args.freq_y}'
  45. os.makedirs(directory, exist_ok=True)
  46. # Pattern naming and saving
  47. pattern_index = 0 # Start indexing from 0
  48. for freq in frequencies:
  49. # Generate horizontal patterns first, then vertical for each frequency
  50. horizontal_patterns = generate_phase_shifted_patterns(width, height, shifts, 'horizontal', freq)
  51. vertical_patterns = generate_phase_shifted_patterns(width, height, shifts, 'vertical', freq)
  52. all_patterns = horizontal_patterns + vertical_patterns # Combine lists to maintain the order
  53. # Save the patterns with the new naming convention in the specified directory
  54. for pattern in all_patterns:
  55. file_path = os.path.join(directory, f'pat{pattern_index:02}.bmp')
  56. plt.imsave(file_path, pattern, cmap='gray')
  57. pattern_index += 1
  58. print(f"Fringe pattern with shift = {shifts}, freqs = [{args.freq_x}, {args.freq_x}] has been saved to {directory}, with number {pattern_index}.")