import colour from colour.appearance.hke import* import matplotlib.pyplot as plt import matplotlib.patches as mpathes import numpy as np import cv2 as cv def plot_patches(FileName, PatchesXYZ, BackgroundColour): #dont know what this does fig,ax = plt.subplots() #background grey background_rect = mpathes.Rectangle([0.0,0.0],0.3+len(patches)*0.3,0.32,color=BackgroundColour) ax.add_patch(background_rect) #Draw all patches patch_count = 0 print('RGBs of patches:') for patch in PatchesXYZ: rgb = colour.XYZ_to_sRGB(patch) print(rgb) ax.add_patch(mpathes.Rectangle([0.2+patch_count*0.3, 0.06], 0.2, 0.2, color=colour.notation.RGB_to_HEX(rgb))) patch_count += 1 # Save image plt.axis('scaled') plt.axis('off') plt.tight_layout() plt.savefig(FileName, dpi=500,bbox_inches='tight',pad_inches=0,transparent=True) #Luminance of background, ref: https://github.com/ilia3101/HKE/blob/main/hke.py Y_value = 0.74 Y_sRGB = colour.models.eotf_inverse_sRGB(Y_value) L_adapting = 65 #Inspired by the image on https://en.wikipedia.org/wiki/Helmholtz%E2%80%93Kohlrausch_effect # ***Patch colours specified in sRGB here patches = [ [248, 0, 0], [118, 131, 0], [0, 142, 92], [0, 130, 197], [212, 0, 234], ] #normalise to 0-1 and linearise for p in patches: for i in range (0,3): p[i] /= 255.0 patches_XYZ = [] for patch in patches: in_XYZ = colour.sRGB_to_XYZ(patch, apply_cctf_decoding=True) Y_mul = (L_adapting/in_XYZ[1]) for i in range(0,3): in_XYZ[i] *= Y_mul #Normalise Y value patches_XYZ.append(in_XYZ) white = colour.xy_to_Luv_uv(colour.temperature.CCT_to_xy_CIE_D(6504)) colours = colour.XYZ_to_xy(patches_XYZ) adaptation_factors = HelmholtzKohlrausch_effect_object_Nayatani1997( # doctest: +ELLIPSIS colour.xy_to_Luv_uv(colours), white, L_adapting ) colours_xyY = np.column_stack((colours, np.full((colours.shape[0], 1), L_adapting))) # Divide Y by adaptation factors colours_Y = L_adapting / adaptation_factors # Create xyY array with adjusted Y values colours_xyY_adapted = np.column_stack((colours,colours_Y[:, None])) colours_XYZ_adapted = colour.xyY_to_XYZ(colours_xyY_adapted) grey_colour = colour.notation.RGB_to_HEX([Y_sRGB,Y_sRGB,Y_sRGB]) print('Grey:',[Y_sRGB,Y_sRGB,Y_sRGB]) plot_patches("images/equal_Y1.png", patches_XYZ, grey_colour) plot_patches("images/equal_Y2.png", colours_XYZ_adapted, grey_colour) #Plot the Difference: # Load the images image1 = cv.imread("images/equal_Y1.png") image2 = cv.imread("images/equal_Y2.png") # Convert images to grayscale (assuming they're color images) gray1 = cv.cvtColor(image1, cv.COLOR_BGR2GRAY) gray2 = cv.cvtColor(image2, cv.COLOR_BGR2GRAY) # Compute the absolute difference diff = cv.absdiff(gray1, gray2) # Plot the difference plt.figure(figsize=(10, 5)) plt.subplot(1, 3, 1) plt.imshow(gray1, cmap='gray') plt.title('Image 1') plt.subplot(1, 3, 2) plt.imshow(gray2, cmap='gray') plt.title('Image 2') plt.subplot(1, 3, 3) plt.imshow(diff, cmap='gray') plt.title('Difference')