Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- ### MATLAB Code Breakdown
- 1. **Adjust Image Brightness**:
- - `Level_Adj = 120;`: This sets a brightness adjustment level. It normalizes the image brightness to this level.
- - `Img = fitsread(pfilename,'image',1);`: This reads the image data from a file.
- - `Y = Img(:,:,2);`: This extracts the Y channel (luminance) from the image.
- - `Ymax = max(Y(:));`: This finds the maximum luminance value in the image.
- - `fprintf('...');`: This prints the maximum scene luminance.
- ```matlab
- % retain luminance if they can be produced by the monitor - otherwise scale them to max monitor brightness
- if Ymax > Level_Adj
- X = X / (Ymax) * Level_Adj;
- Y = Y / (Ymax) * Level_Adj;
- Z = Z / (Ymax) * Level_Adj;
- end
- ```
- - This block checks if the maximum luminance exceeds the adjustment level and scales the X, Y, and Z channels accordingly.
- 2. **Normalize the Image**:
- - `XYZimg(:,:,1) = X / Level_Adj;`: This normalizes the X channel.
- - `XYZimg(:,:,2) = Y / Level_Adj;`: This normalizes the Y channel.
- - `XYZimg(:,:,3) = Z / Level_Adj;`: This normalizes the Z channel.
- 3. **Check Chromaticities**:
- ```matlab
- ximg = X./(X+Y+Z); x = reshape(ximg,1,[]);
- yimg = Y./(X+Y+Z); y = reshape(yimg,1,[]);
- ```
- - These lines calculate the chromaticity coordinates \( x \) and \( y \) and reshape them into vectors.
- ```matlab
- scatter(x,y,1);
- x_rgb = [0.6400 0.3000 0.1500];
- y_rgb = [0.3300 0.6000 0.0600];
- hold on;
- scatter(x_rgb, y_rgb, 50, 'c', 'filled');
- title('Colour gamut point cloud with sRGB primaries');
- hold off;
- ```
- - This plots the chromaticities and the sRGB primaries for comparison.
- 4. **Convert from XYZ to sRGB**:
- ```matlab
- sRGBimg = xyz2rgb(XYZimg,'WhitePoint','d50'); % convert input scene from selected whitepoint to D65
- ```
- - This converts the XYZ image to sRGB using D50 as the white point.
- 5. **Clip Image for Out-of-Gamut Colors**:
- ```matlab
- sRGBimg = min(sRGBimg, 1); % clip image for out of gamut colours
- sRGBimg = max(sRGBimg, 0); % clip image for out of gamut colours
- sRGB_stretch = imadjust(sRGBimg,[black black black; 1 1 1],[]);
- ```
- - These lines clip the sRGB values to ensure they are within the valid range [0, 1] and then adjust the contrast.
- ### Translation to Python
- Now, let’s translate this understanding into Python code using `numpy` and `colormath`.
- ```python
- import numpy as np
- import matplotlib.pyplot as plt
- from colormath.color_objects import XYZColor, sRGBColor
- from colormath.color_conversions import convert_color
- from colormath.chromatic_adaptation import apply_chromatic_adaptation
- # Load your image data here
- # Assuming `img` is a 3D numpy array with shape (height, width, 3) and contains XYZ values
- # Example: img = np.load('your_image.npy')
- # Adjust Image Brightness
- Level_Adj = 120 # Adjust image brightness
- Y = img[:, :, 1]
- Ymax = np.max(Y)
- print(f"Max scene luminance {Ymax}")
- if Ymax > Level_Adj:
- img[:, :, 0] = img[:, :, 0] / Ymax * Level_Adj
- img[:, :, 1] = img[:, :, 1] / Ymax * Level_Adj
- img[:, :, 2] = img[:, :, 2] / Ymax * Level_Adj
- # Normalize the Image
- img[:, :, 0] /= Level_Adj
- img[:, :, 1] /= Level_Adj
- img[:, :, 2] /= Level_Adj
- # Check Chromaticities
- X = img[:, :, 0] / (img[:, :, 0] + img[:, :, 1] + img[:, :, 2])
- Y = img[:, :, 1] / (img[:, :, 0] + img[:, :, 1] + img[:, :, 2])
- Z = img[:, :, 2] / (img[:, :, 0] + img[:, :, 1] + img[:, :, 2])
- x_img = X.reshape(-1)
- y_img = Y.reshape(-1)
- plt.figure()
- plt.scatter(x_img, y_img, c='b', s=1)
- x_rgb = [0.6400, 0.3000, 0.1500]
- y_rgb = [0.3300, 0.6000, 0.0600]
- plt.scatter(x_rgb, y_rgb, c='r', s=50)
- plt.title('Colour gamut point cloud with sRGB primaries')
- plt.show()
- # Convert from XYZ to sRGB
- def adapt_and_convert(xyz_data, orig_illum, targ_illum):
- adapted_xyz_data = np.zeros_like(xyz_data)
- srgb_data = np.zeros_like(xyz_data)
- for i in range(xyz_data.shape[0]):
- for j in range(xyz_data.shape[1]):
- val_x = xyz_data[i, j, 0]
- val_y = xyz_data[i, j, 1]
- val_z = xyz_data[i, j, 2]
- adapted_values = apply_chromatic_adaptation(
- val_x, val_y, val_z,
- orig_illum=orig_illum, targ_illum=targ_illum,
- observer='2', adaptation='bradford'
- )
- adapted_xyz_data[i, j, 0] = adapted_values[0]
- adapted_xyz_data[i, j, 1] = adapted_values[1]
- adapted_xyz_data[i, j, 2] = adapted_values[2]
- xyz_color = XYZColor(adapted_values[0], adapted_values[1], adapted_values[2], illuminant=targ_illum)
- srgb_color = convert_color(xyz_color, sRGBColor)
- srgb_data[i, j, 0] = srgb_color.clamped_rgb_r
- srgb_data[i, j, 1] = srgb_color.clamped_rgb_g
- srgb_data[i, j, 2] = srgb_color.clamped_rgb_b
- return srgb_data
- # Apply chromatic adaptation from D50 to D65
- adapted_img = adapt_and_convert(img, orig_illum='d50', targ_illum='d65')
- # Clip out-of-gamut colors
- adapted_img = np.clip(adapted_img, 0, 1)
- # Display the result
- plt.figure()
- plt.imshow(adapted_img)
- plt.title('Adapted and converted sRGB Image')
- plt.show()
- ```
- ### Explanation of Python Code
- 1. **Loading Image Data**: You need to load your image data into a numpy array `img`.
- 2. **Adjust Image Brightness**: The brightness is normalized based on the maximum luminance.
- 3. **Normalize the Image**: The image channels are normalized by the adjustment level.
- 4. **Check Chromaticities**: Chromaticity coordinates \( x \) and \( y \) are calculated and plotted.
- 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.
- 6. **Clipping**: The sRGB values are clipped to ensure they are within the valid range [0, 1].
- 7. **Display**: The adapted and converted image is displayed using `matplotlib`.
- 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