Advertisement
ajith_97

sRGB white point

May 15th, 2024
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. Sure, I'll go through the MATLAB code step by step, explaining what each part does, and then show how this understanding translates to the Python code.
  2.  
  3. ### MATLAB Code Breakdown
  4.  
  5. 1. **Adjust Image Brightness**:
  6. - `Level_Adj = 120;`: This sets a brightness adjustment level. It normalizes the image brightness to this level.
  7. - `Img = fitsread(pfilename,'image',1);`: This reads the image data from a file.
  8. - `Y = Img(:,:,2);`: This extracts the Y channel (luminance) from the image.
  9. - `Ymax = max(Y(:));`: This finds the maximum luminance value in the image.
  10. - `fprintf('...');`: This prints the maximum scene luminance.
  11.  
  12. ```matlab
  13. % retain luminance if they can be produced by the monitor - otherwise scale them to max monitor brightness
  14. if Ymax > Level_Adj
  15. X = X / (Ymax) * Level_Adj;
  16. Y = Y / (Ymax) * Level_Adj;
  17. Z = Z / (Ymax) * Level_Adj;
  18. end
  19. ```
  20. - This block checks if the maximum luminance exceeds the adjustment level and scales the X, Y, and Z channels accordingly.
  21.  
  22. 2. **Normalize the Image**:
  23. - `XYZimg(:,:,1) = X / Level_Adj;`: This normalizes the X channel.
  24. - `XYZimg(:,:,2) = Y / Level_Adj;`: This normalizes the Y channel.
  25. - `XYZimg(:,:,3) = Z / Level_Adj;`: This normalizes the Z channel.
  26.  
  27. 3. **Check Chromaticities**:
  28. ```matlab
  29. ximg = X./(X+Y+Z); x = reshape(ximg,1,[]);
  30. yimg = Y./(X+Y+Z); y = reshape(yimg,1,[]);
  31. ```
  32. - These lines calculate the chromaticity coordinates \( x \) and \( y \) and reshape them into vectors.
  33.  
  34. ```matlab
  35. scatter(x,y,1);
  36. x_rgb = [0.6400 0.3000 0.1500];
  37. y_rgb = [0.3300 0.6000 0.0600];
  38. hold on;
  39. scatter(x_rgb, y_rgb, 50, 'c', 'filled');
  40. title('Colour gamut point cloud with sRGB primaries');
  41. hold off;
  42. ```
  43. - This plots the chromaticities and the sRGB primaries for comparison.
  44.  
  45. 4. **Convert from XYZ to sRGB**:
  46. ```matlab
  47. sRGBimg = xyz2rgb(XYZimg,'WhitePoint','d50'); % convert input scene from selected whitepoint to D65
  48. ```
  49. - This converts the XYZ image to sRGB using D50 as the white point.
  50.  
  51. 5. **Clip Image for Out-of-Gamut Colors**:
  52. ```matlab
  53. sRGBimg = min(sRGBimg, 1); % clip image for out of gamut colours
  54. sRGBimg = max(sRGBimg, 0); % clip image for out of gamut colours
  55. sRGB_stretch = imadjust(sRGBimg,[black black black; 1 1 1],[]);
  56. ```
  57. - These lines clip the sRGB values to ensure they are within the valid range [0, 1] and then adjust the contrast.
  58.  
  59. ### Translation to Python
  60.  
  61. Now, letโ€™s translate this understanding into Python code using `numpy` and `colormath`.
  62.  
  63. ```python
  64. import numpy as np
  65. import matplotlib.pyplot as plt
  66. from colormath.color_objects import XYZColor, sRGBColor
  67. from colormath.color_conversions import convert_color
  68. from colormath.chromatic_adaptation import apply_chromatic_adaptation
  69.  
  70. # Load your image data here
  71. # Assuming `img` is a 3D numpy array with shape (height, width, 3) and contains XYZ values
  72. # Example: img = np.load('your_image.npy')
  73.  
  74. # Adjust Image Brightness
  75. Level_Adj = 120 # Adjust image brightness
  76. Y = img[:, :, 1]
  77. Ymax = np.max(Y)
  78. print(f"Max scene luminance {Ymax}")
  79.  
  80. if Ymax > Level_Adj:
  81. img[:, :, 0] = img[:, :, 0] / Ymax * Level_Adj
  82. img[:, :, 1] = img[:, :, 1] / Ymax * Level_Adj
  83. img[:, :, 2] = img[:, :, 2] / Ymax * Level_Adj
  84.  
  85. # Normalize the Image
  86. img[:, :, 0] /= Level_Adj
  87. img[:, :, 1] /= Level_Adj
  88. img[:, :, 2] /= Level_Adj
  89.  
  90. # Check Chromaticities
  91. X = img[:, :, 0] / (img[:, :, 0] + img[:, :, 1] + img[:, :, 2])
  92. Y = img[:, :, 1] / (img[:, :, 0] + img[:, :, 1] + img[:, :, 2])
  93. Z = img[:, :, 2] / (img[:, :, 0] + img[:, :, 1] + img[:, :, 2])
  94.  
  95. x_img = X.reshape(-1)
  96. y_img = Y.reshape(-1)
  97.  
  98. plt.figure()
  99. plt.scatter(x_img, y_img, c='b', s=1)
  100. x_rgb = [0.6400, 0.3000, 0.1500]
  101. y_rgb = [0.3300, 0.6000, 0.0600]
  102. plt.scatter(x_rgb, y_rgb, c='r', s=50)
  103. plt.title('Colour gamut point cloud with sRGB primaries')
  104. plt.show()
  105.  
  106. # Convert from XYZ to sRGB
  107. def adapt_and_convert(xyz_data, orig_illum, targ_illum):
  108. adapted_xyz_data = np.zeros_like(xyz_data)
  109. srgb_data = np.zeros_like(xyz_data)
  110.  
  111. for i in range(xyz_data.shape[0]):
  112. for j in range(xyz_data.shape[1]):
  113. val_x = xyz_data[i, j, 0]
  114. val_y = xyz_data[i, j, 1]
  115. val_z = xyz_data[i, j, 2]
  116.  
  117. adapted_values = apply_chromatic_adaptation(
  118. val_x, val_y, val_z,
  119. orig_illum=orig_illum, targ_illum=targ_illum,
  120. observer='2', adaptation='bradford'
  121. )
  122.  
  123. adapted_xyz_data[i, j, 0] = adapted_values[0]
  124. adapted_xyz_data[i, j, 1] = adapted_values[1]
  125. adapted_xyz_data[i, j, 2] = adapted_values[2]
  126.  
  127. xyz_color = XYZColor(adapted_values[0], adapted_values[1], adapted_values[2], illuminant=targ_illum)
  128. srgb_color = convert_color(xyz_color, sRGBColor)
  129.  
  130. srgb_data[i, j, 0] = srgb_color.clamped_rgb_r
  131. srgb_data[i, j, 1] = srgb_color.clamped_rgb_g
  132. srgb_data[i, j, 2] = srgb_color.clamped_rgb_b
  133.  
  134. return srgb_data
  135.  
  136. # Apply chromatic adaptation from D50 to D65
  137. adapted_img = adapt_and_convert(img, orig_illum='d50', targ_illum='d65')
  138.  
  139. # Clip out-of-gamut colors
  140. adapted_img = np.clip(adapted_img, 0, 1)
  141.  
  142. # Display the result
  143. plt.figure()
  144. plt.imshow(adapted_img)
  145. plt.title('Adapted and converted sRGB Image')
  146. plt.show()
  147. ```
  148.  
  149. ### Explanation of Python Code
  150.  
  151. 1. **Loading Image Data**: You need to load your image data into a numpy array `img`.
  152. 2. **Adjust Image Brightness**: The brightness is normalized based on the maximum luminance.
  153. 3. **Normalize the Image**: The image channels are normalized by the adjustment level.
  154. 4. **Check Chromaticities**: Chromaticity coordinates \( x \) and \( y \) are calculated and plotted.
  155. 5. **Chromatic Adaptation and Conversion**: Adaptation from D50 to D65 is applied using the Bradford method, and the adapted XYZ colors are converted to sRGB.
  156. 6. **Clipping**: The sRGB values are clipped to ensure they are within the valid range [0, 1].
  157. 7. **Display**: The adapted and converted image is displayed using `matplotlib`.
  158.  
  159. This Python code should provide similar functionality to your MATLAB code using the `colormath` library for chromatic adaptation and color conversion.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement