matplotlib灰度图可视化处理

这里我们利用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()

然后上效果图


matplotlib灰度图可视化处理_第1张图片
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()

看下效果


matplotlib灰度图可视化处理_第2张图片
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()
matplotlib灰度图可视化处理_第3张图片
image.png

还是很好玩的

你可能感兴趣的:(matplotlib灰度图可视化处理)