image_processing.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from PIL import Image
  2. import numpy as np
  3. import ipfml.metrics as metrics
  4. def fig2data(fig):
  5. """
  6. @brief Convert a Matplotlib figure to a 3D numpy array with RGB channels and return it
  7. @param fig a matplotlib figure
  8. @return a numpy 3D array of RGB values
  9. """
  10. # draw the renderer
  11. fig.canvas.draw()
  12. # Get the RGBA buffer from the figure
  13. w,h = fig.canvas.get_width_height()
  14. buf = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8)
  15. buf.shape = (w, h, 3)
  16. # canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA mode
  17. buf = np.roll(buf, 3, axis=2)
  18. return buf
  19. def fig2img(fig):
  20. """
  21. @brief Convert a Matplotlib figure to a PIL Image in RGB format and return it
  22. @param fig a matplotlib figure
  23. @return a Python Imaging Library (PIL) image : default size (480,640,3)
  24. """
  25. # put the figure pixmap into a numpy array
  26. buf = fig2data(fig)
  27. w, h, d = buf.shape
  28. return Image.frombytes("RGB", (w, h), buf.tostring())
  29. def get_LAB_L_SVD(image):
  30. """
  31. @brief Returns Singular values from LAB L Image information
  32. @param fig a matplotlib figure
  33. @return a Python Imaging Library (PIL) image : default size (480,640,3)
  34. """
  35. L = metrics.get_LAB_L(image)
  36. return metrics.get_SVD(L)
  37. def get_LAB_L_SVD_s(image):
  38. """
  39. @brief Returns s (Singular values) SVD from L of LAB Image information
  40. @param PIL Image
  41. @return vector of singular values
  42. """
  43. L = metrics.get_LAB_L(image)
  44. return metrics.get_SVD_s(L)
  45. def get_LAB_L_SVD_U(image):
  46. """
  47. @brief Returns U SVD from L of LAB Image information
  48. @param PIL Image
  49. @return vector of singular values
  50. """
  51. L = metrics.get_LAB_L(image)
  52. return metrics.get_SVD_U(L)
  53. def get_LAB_L_SVD_V(image):
  54. """
  55. @brief Returns V SVD from L of LAB Image information
  56. @param PIL Image
  57. @return vector of singular values
  58. """
  59. L = metrics.get_LAB_L(image)
  60. return metrics.get_SVD_V(L)