Advertisement
ajith_97

White point

May 29th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import numpy as np
  2.  
  3. # Assuming `xyz` is your input ndarray of shape (600, 800, 3)
  4. xyz = np.array(xyz, dtype=np.float32) # Ensure the array is in float
  5.  
  6. # Normalize if not already normalized
  7. xyz /= np.max(xyz)
  8.  
  9. # White point adaptation matrix from D65 to D50
  10. M_D65_to_D50 = np.array([
  11. [ 0.9555766, -0.0230393, 0.0631636],
  12. [-0.0282895, 1.0099416, 0.0210077],
  13. [ 0.0122982, -0.0204830, 1.3299098]
  14. ])
  15.  
  16. # Reshape xyz to (n, 3) for matrix multiplication
  17. xyz_flat = xyz.reshape(-1, 3)
  18.  
  19. # Apply the white point adaptation
  20. xyz_d50_flat = np.dot(xyz_flat, M_D65_to_D50.T)
  21.  
  22. # Reshape back to original shape
  23. xyz_d50 = xyz_d50_flat.reshape(xyz.shape)
  24.  
  25. # Transformation matrix from XYZ (D50) to linear RGB
  26. M_XYZ_to_RGB = np.array([
  27. [ 3.2404542, -1.5371385, -0.4985314],
  28. [-0.9692660, 1.8760108, 0.0415560],
  29. [ 0.0556434, -0.2040259, 1.0572252]
  30. ])
  31.  
  32. # Apply the transformation to get linear RGB
  33. rgb_linear_flat = np.dot(xyz_d50_flat, M_XYZ_to_RGB.T)
  34.  
  35. # Reshape back to original shape
  36. rgb_linear = rgb_linear_flat.reshape(xyz_d50.shape)
  37.  
  38. # Gamma correction function
  39. def gamma_correction(channel):
  40. return np.where(channel <= 0.0031308,
  41. 12.92 * channel,
  42. 1.055 * np.power(channel, 1/2.4) - 0.055)
  43.  
  44. # Apply gamma correction
  45. rgb_srgb = gamma_correction(rgb_linear)
  46.  
  47. # Clip values to the range [0, 1]
  48. rgb_srgb = np.clip(rgb_srgb, 0, 1)
  49.  
  50. # rgb_srgb now contains the sRGB color data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement