這里我們利用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
這個效果圖是這張圖片可視化的結(jié)果
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
還是很好玩的