matplotlib灰度圖可視化處理

這里我們利用matplotlib可視化一張圖片的灰度圖??v橫坐標(biāo)為圖片的像素點位置(x, y),此像素點的灰度值z(x, y)當(dāng)作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

還是很好玩的

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容