1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| from matplotlib import colors
mandrill_rgb = plt.imread("USC_SIPI_Mandrill.tiff")/256 ruler = plt.imread("USC_SIPI_Ruler.512.tiff")/256
def rgb2gray(rgb): return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
mandrill_grayscale = rgb2gray(mandrill_rgb) mandrill_bw = np.where(mandrill_grayscale >= 0.5, 1, 0)
mandrill_a = np.zeros(mandrill_grayscale.shape) for i in range(mandrill_a.shape[0]): mandrill_a[:,i] = i/mandrill_a.shape[0] mandrill_alpha = np.insert(mandrill_rgb, 3, mandrill_a, axis=2)
fig, axes = plt.subplots(ncols=4, nrows=2, figsize=(10, 5), gridspec_kw={"height_ratios":[3,1]})
for ax, img, title in zip(axes[0], [mandrill_bw, mandrill_grayscale, mandrill_rgb, mandrill_alpha], ["Black and White\n(1 bit, 1 channel)", "Grayscale\n(32 bit, 1 channel)", "RGB\n(32 bit, 3 channels)", "RGBA\n(32 bit, 4 channels)"]): if "RGB" in title: ax.imshow(img) else: ax.imshow(img, cmap="gist_gray") ax.set_title(title, fontweight="bold", pad=8) ax.axis(False)
sns.histplot(mandrill_bw.ravel(), bins=20, ax=axes[1, 0]) for p in axes[1, 0].patches: p.set_facecolor(str(p.get_x()))
sns.kdeplot(mandrill_grayscale.ravel(), bw_adjust=0.5, cut=0, fill=True, color="k", alpha=0, ax=axes[1, 1]) x = np.linspace(0, 1, 100) im = axes[1, 1].imshow(np.vstack([x, x]), cmap="gist_gray", aspect="auto", extent=[*axes[1,1].get_xlim(), *axes[1,1].get_ylim()]) path = axes[1, 1].collections[0].get_paths()[0] patch = mpl.patches.PathPatch(path, transform=axes[1, 1].transData) im.set_clip_path(patch)
sns.kdeplot(mandrill_rgb[:, :, 0].ravel(), bw_adjust=0.5, cut=1, fill=True, alpha=0, color="red", label="R", ax=axes[1, 2]) sns.kdeplot(mandrill_rgb[:, :, 1].ravel(), bw_adjust=0.5, cut=1, fill=True, alpha=0, color="green", label="G", ax=axes[1, 2]) sns.kdeplot(mandrill_rgb[:, :, 2].ravel(), bw_adjust=0.5, cut=1, fill=True, alpha=0, color="blue", label="B", ax=axes[1, 2]) for i, color in enumerate(["red", "green", "blue"]): im = axes[1, 2].imshow(np.vstack([x, x]), cmap=f"{color.capitalize()}s_r", aspect="auto", extent=[*axes[1, 2].get_xlim(), *axes[1, 2].get_ylim()]) path = axes[1, 2].collections[i].get_paths()[0] patch = mpl.patches.PathPatch(path, transform=axes[1, 2].transData) im.set_clip_path(patch) axes[1, 2].set_ylim(top=axes[1, 2].get_ylim()[1] * 1.4) axes[1, 2].legend(ncol=3, fontsize=9)
axes[0, -1].imshow(ruler, zorder=-1, cmap="gist_gray") sns.kdeplot(mandrill_alpha[:, :, 0].ravel(), bw_adjust=0.5, cut=1, fill=True, alpha=0, color="red", label="R", ax=axes[1, 3]) sns.kdeplot(mandrill_alpha[:, :, 1].ravel(), bw_adjust=0.5, cut=1, fill=True, alpha=0, color="green", label="G", ax=axes[1, 3]) sns.kdeplot(mandrill_alpha[:, :, 2].ravel(), bw_adjust=0.5, cut=1, fill=True, alpha=0, color="blue", label="B", ax=axes[1, 3]) sns.kdeplot(mandrill_alpha[:, :, 3].ravel(), bw_adjust=0.5, cut=1, fill=True, alpha=0, color="k", label="alpha", ax=axes[1, 3]) for i, color in enumerate(["red", "green", "blue"]): im = axes[1, 3].imshow(np.vstack([x, x]), cmap=f"{color.capitalize()}s_r", aspect="auto", extent=[*axes[1, 3].get_xlim(), *axes[1, 3].get_ylim()]) path = axes[1, 3].collections[i].get_paths()[0] patch = mpl.patches.PathPatch(path, transform=axes[1, 3].transData) im.set_clip_path(patch) axes[1, 3].set_ylim(top=axes[1, 3].get_ylim()[1] * 1.4) handles, labels = axes[1, 3].get_legend_handles_labels() axes[1, 3].legend(handles=handles[-1:], labels=labels[-1:], fontsize=9) for ax in axes[1]: ax.set_xlim(-0.1, 1.1) ax.set_yticklabels([]) ax.set_xlabel('intensity', fontsize=12, labelpad=8) ax.set_ylabel('')
fig.tight_layout()
|