美文网首页
matplotlib灰度图可视化处理

matplotlib灰度图可视化处理

作者: golgotha | 来源:发表于2018-05-20 22:18 被阅读529次

    这里我们利用matplotlib可视化一张图片的灰度图。纵横坐标为图片的像素点位置(x, y),此像素点的灰度值z(x, y)当作z轴上的取值。

    首先利用plot_surface分析某张图片的灰度图
    # -*- coding: utf-8 -*-
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    from skimage import io
    from skimage.color import rgb2gray
    import numpy as np
    from matplotlib import cm
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    img = io.imread("qrcodeTL.jpg")
    gray_img = rgb2gray(img)
    
    X = np.arange(0, gray_img.shape[1], 1)
    Y = np.arange(0, gray_img.shape[0], 1)
    
    X, Y = np.meshgrid(X, Y)
    Z = gray_img * 255
    # Plot the surface.
    surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                           linewidth=0, antialiased=False)
    
    # Customize the z axis.
    # Add a color bar which maps values to colors.
    fig.colorbar(surf, shrink=0.5, aspect=5)
    
    plt.show()
    

    然后上效果图


    image.png

    这个效果图是这张图片可视化的结果

    qrcodeTL.jpg

    是一个二维码的局部。

    然后利用plot_wireframe分析某张图片的灰度图
    # -*- coding: utf-8 -*-
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    from skimage import io
    from skimage.color import rgb2gray
    import numpy as np
    from matplotlib import cm
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    img = io.imread("qrcodeTL.jpg")
    gray_img = rgb2gray(img)
    
    X = np.arange(0, gray_img.shape[1], 1)
    Y = np.arange(0, gray_img.shape[0], 1)
    
    X, Y = np.meshgrid(X, Y)
    
    Z = gray_img * 255
    # Plot the surface.
    surf = ax.plot_wireframe(X, Y, Z, rstride=1, cstride=1)
    plt.show()
    

    看下效果


    image.png

    其实还是刚才那张图片

    然后利用plot_trisurf分析某张图片的灰度图
    # -*- coding: utf-8 -*-
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    from skimage import io
    from skimage.color import rgb2gray
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    img = io.imread("qrcodeTL.jpg")
    gray_img = rgb2gray(img)
    
    x = []
    y = []
    z = []
    for yi in range(0, gray_img.shape[0]):
        for xi in range(0, gray_img.shape[1]):
            y.append(yi)
            x.append(xi)
            z.append(gray_img[yi][xi] * 255)
    
    ax.plot_trisurf(x,y,z)
    plt.show()
    
    image.png

    还是很好玩的

    相关文章

      网友评论

          本文标题:matplotlib灰度图可视化处理

          本文链接:https://www.haomeiwen.com/subject/ogcxjftx.html