wavelet.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import numpy as np
  2. def wavelet(image, width=None):
  3. shape = image.shape
  4. if len(shape) != 2 or shape[0] != shape[1]:
  5. raise(f"Expected quadratic image but got data with shape: {shape}")
  6. if width is None:
  7. width = shape[0]
  8. if shape[0] < width:
  9. raise(f"Image with shape {shape} is smaller then the requested size {width}.")
  10. result = image.copy()
  11. half = width // 2
  12. for y in range(half):
  13. for x in range(half):
  14. px = 2 * x
  15. py = 2 * y
  16. result[y, x] = image[py, px]
  17. result[y, x + half] = image[py, px + 1] - image[py, px]
  18. result[y + half, x] = image[py + 1, px] - image[py, px]
  19. result[y + half, x + half] = image[py + 1, px + 1] - image[py, px]
  20. return result
  21. def refine(image):
  22. shape = image.shape
  23. if len(shape) != 2 or shape[0] != shape[1]:
  24. raise(f"Expected quadratic image but got data with shape: {shape}")
  25. width = shape[0]
  26. while width > 1:
  27. image = wavelet(image, width)
  28. width = width // 2
  29. return image
  30. def rotate(image):
  31. shape = image.shape
  32. if len(shape) != 2 or shape[0] != shape[1]:
  33. raise(f"Expected quadratic image but got data with shape: {shape}")
  34. result = image.copy()
  35. w = shape[0]
  36. for y in range(w):
  37. for x in range(w):
  38. result[x,y] = image[y,x]
  39. return result
  40. def mirror(image):
  41. shape = image.shape
  42. if len(shape) != 2 or shape[0] != shape[1]:
  43. raise(f"Expected quadratic image but got data with shape: {shape}")
  44. result = image.copy()
  45. w = shape[0]
  46. for y in range(w):
  47. for x in range(w):
  48. result[w - y - 1, x] = image[y,x]
  49. return result