输出:
<matplotlib.image.AxesImage at 0x7f9e6cd20048> def vertical_gradient_line(image, reverse=False): """ 我们创建一个垂直梯度线。形状 (1, image.shape[1], 3)) 如果reverse为False,则值从0增加到1, 否则,值将从1递减到0。 """ number_of_columns = image.shape[1] if reverse: C = np.linspace(1, 0, number_of_columns) else: C = np.linspace(0, 1, number_of_columns) C = np.dstack((C, C, C)) return C horizontal_brush = vertical_gradient_line(windmills) tinted_windmills = windmills * horizontal_brush plt.axis("off") plt.imshow(tinted_windmills)输出:
<matplotlib.image.AxesImage at 0x7f9e6ccb3d68>
现在,我们将通过将Python函数的reverse参数设置为“True”来从右向左着色图像:
def vertical_gradient_line(image, reverse=False): """ 我们创建一个水平梯度线。形状 (1, image.shape[1], 3)) 如果reverse为False,则值从0增加到1, 否则,值将从1递减到0。 """ number_of_columns = image.shape[1] if reverse: C = np.linspace(1, 0, number_of_columns) else: C = np.linspace(0, 1, number_of_columns) C = np.dstack((C, C, C)) return C horizontal_brush = vertical_gradient_line(windmills, reverse=True) tinted_windmills = windmills * horizontal_brush plt.axis("off") plt.imshow(tinted_windmills)输出:
<matplotlib.image.AxesImage at 0x7f9e6cbc82b0> def horizontal_gradient_line(image, reverse=False): """ 我们创建一个垂直梯度线。形状(image.shape[0], 1, 3)) 如果reverse为False,则值从0增加到1, 否则,值将从1递减到0。 """ number_of_rows, number_of_columns = image.shape[:2] C = np.linspace(1, 0, number_of_rows) C = C[np.newaxis,:] C = np.concatenate((C, C, C)).transpose() C = C[:, np.newaxis] return C vertical_brush = horizontal_gradient_line(windmills) tinted_windmills = windmills plt.imshow(tinted_windmills)输出:
<matplotlib.image.AxesImage at 0x7f9e6cb52390>
色调是由一种颜色与灰色的混合产生的,或由着色和阴影产生的。
charlie = plt.imread(\'Chaplin.png\') plt.gray() print(charlie) plt.imshow(charlie) [[ 0.16470589 0.16862746 0.17647059 ..., 0. 0. 0. ] [ 0.16078432 0.16078432 0.16470589 ..., 0. 0. 0. ] [ 0.15686275 0.15686275 0.16078432 ..., 0. 0. 0. ] ..., [ 0. 0. 0. ..., 0. 0. 0. ] [ 0. 0. 0. ..., 0. 0. 0. ] [ 0. 0. 0. ..., 0. 0. 0. ]]输出:
<matplotlib.image.AxesImage at 0x7f9e70047668>
给灰度图像着色:
在下面的示例中,我们将使用不同的颜色映射。颜色映射可以在matplotlib.pyplot.cm.datad中找到:
plt.cm.datad.keys()输出:
dict_keys([\'afmhot\', \'autumn\', \'bone\', \'binary\', \'bwr\', \'brg\', \'CMRmap\', \'cool\', \'copper\', \'cubehelix\', \'flag\', \'gnuplot\', \'gnuplot2\', \'gray\', \'hot\', \'hsv\', \'jet\', \'ocean\', \'pink\', \'prism\', \'rainbow\', \'seismic\', \'spring\', \'summer\', \'terrain\', \'winter\', \'nipy_spectral\', \'spectral\', \'Blues\', \'BrBG\', \'BuGn\', \'BuPu\', \'GnBu\', \'Greens\', \'Greys\', \'Oranges\', \'OrRd\', \'PiYG\', \'PRGn\', \'PuBu\', \'PuBuGn\', \'PuOr\', \'PuRd\', \'Purples\', \'RdBu\', \'RdGy\', \'RdPu\', \'RdYlBu\', \'RdYlGn\', \'Reds\', \'Spectral\', \'YlGn\', \'YlGnBu\', \'YlOrBr\', \'YlOrRd\', \'gist_earth\', \'gist_gray\', \'gist_heat\', \'gist_ncar\', \'gist_rainbow\', \'gist_stern\', \'gist_yarg\', \'coolwarm\', \'Wistia\', \'Accent\', \'Dark2\', \'Paired\', \'Pastel1\', \'Pastel2\', \'Set1\', \'Set2\', \'Set3\', \'tab10\', \'tab20\', \'tab20b\', \'tab20c\', \'Vega10\', \'Vega20\', \'Vega20b\', \'Vega20c\', \'afmhot_r\', \'autumn_r\', \'bone_r\', \'binary_r\', \'bwr_r\', \'brg_r\', \'CMRmap_r\', \'cool_r\', \'copper_r\', \'cubehelix_r\', \'flag_r\', \'gnuplot_r\', \'gnuplot2_r\', \'gray_r\', \'hot_r\', \'hsv_r\', \'jet_r\', \'ocean_r\', \'pink_r\', \'prism_r\', \'rainbow_r\', \'seismic_r\', \'spring_r\', \'summer_r\', \'terrain_r\', \'winter_r\', \'nipy_spectral_r\', \'spectral_r\', \'Blues_r\', \'BrBG_r\', \'BuGn_r\', \'BuPu_r\', \'GnBu_r\', \'Greens_r\', \'Greys_r\', \'Oranges_r\', \'OrRd_r\', \'PiYG_r\', \'PRGn_r\', \'PuBu_r\', \'PuBuGn_r\', \'PuOr_r\', \'PuRd_r\', \'Purples_r\', \'RdBu_r\', \'RdGy_r\', \'RdPu_r\', \'RdYlBu_r\', \'RdYlGn_r\', \'Reds_r\', \'Spectral_r\', \'YlGn_r\', \'YlGnBu_r\', \'YlOrBr_r\', \'YlOrRd_r\', \'gist_earth_r\', \'gist_gray_r\', \'gist_heat_r\', \'gist_ncar_r\', \'gist_rainbow_r\', \'gist_stern_r\', \'gist_yarg_r\', \'coolwarm_r\', \'Wistia_r\', \'Accent_r\', \'Dark2_r\', \'Paired_r\', \'Pastel1_r\', \'Pastel2_r\', \'Set1_r\', \'Set2_r\', \'Set3_r\', \'tab10_r\', \'tab20_r\', \'tab20b_r\', \'tab20c_r\', \'Vega10_r\', \'Vega20_r\', \'Vega20b_r\', \'Vega20c_r\']) import numpy as np import matplotlib.pyplot as plt charlie = plt.imread(\'Chaplin.png\') # colormaps plt.cm.datad # cmaps = set(plt.cm.datad.keys()) cmaps = {\'afmhot\', \'autumn\', \'bone\', \'binary\', \'bwr\', \'brg\', \'CMRmap\', \'cool\', \'copper\', \'cubehelix\', \'Greens\'} X = [ (4,3,1, (1, 0, 0)), (4,3,2, (0.5, 0.5, 0)), (4,3,3, (0, 1, 0)), (4,3,4, (0, 0.5, 0.5)), (4,3,(5,8), (0, 0, 1)), (4,3,6, (1, 1, 0)), (4,3,7, (0.5, 1, 0) ), (4,3,9, (0, 0.5, 0.5)), (4,3,10, (0, 0.5, 1)), (4,3,11, (0, 1, 1)), (4,3,12, (0.5, 1, 1))] fig = plt.figure(figsize=(6, 5)) #fig.subplots_adjust(bottom=0, left=0, top = 0.975, right=1) for nrows, ncols, plot_number, factor in X: sub = fig.add_subplot(nrows, ncols, plot_number) sub.set_xticks([]) plt.colors() sub.imshow(charlie*0.0002, cmap=cmaps.pop()) sub.set_yticks([]) #fig.show()原文链接:https://levelup.gitconnected.com/image-processing-in-python-b5e3e11e1413
欢迎关注磐创AI博客站:
sklearn机器学习中文官方文档:
欢迎关注磐创博客资源汇总站: