我試圖通過繪制 3D 圖形來可視化照片的像素強度。在下面的代碼中,lab是我要分析的影像。該代碼將查看影像中每個像素的像素強度并繪制一個圖形,其中高度表示像素強度。
這是我的代碼的一部分:
import cv2
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from skimage import io, color
import glob
from PIL import Image
plt.figure(dpi=1200)
ax = plt.axes(projection='3d')
y = range(lab.shape[0])
x = range(lab.shape[1])
X, Y = np.meshgrid(x, y)
ax.view_init(elev=60., azim=60)
thickness = ax.plot_surface(
X,
Y,
lab[:, :, 0], # change value here to adjust the height
cmap=cm.coolwarm,
antialiased=False)
# Add a color bar which maps values to colors.
fig.colorbar(thickness, shrink=0.5, aspect=5)
它輸出:

正如您所看到的,盡管圖形具有許多精細的細節和高度的輕微波動,但顏色漸變并不是連續的,而顏色圖沒有表現出來。是否可以使用 matplotlib 實作具有表面圖的連續顏色漸變,如下圖所示?

謝謝你。
uj5u.com熱心網友回復:
您可以使用顏色圖 hsv 來獲得相同的結果。
import cv2
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from skimage import io, color
import glob
from PIL import Image
lab = cv2.imread('Lenna.png')
lab = cv2.cvtColor(lab, cv2.COLOR_BGR2LAB)
fig = plt.figure()
ax = plt.axes(projection='3d')
y = range(lab.shape[0])
x = range(lab.shape[1])
X, Y = np.meshgrid(x, y)
ax.view_init(elev=60., azim=60)
thickness = ax.plot_surface(
X,
Y,
lab[:, :, 0], # change value here to adjust the height
cmap=plt.get_cmap('hsv'),
antialiased=False)
# Add a color bar which maps values to colors.
fig.colorbar(thickness, shrink=0.5, aspect=5)
plt.show()
輸出 :

查看檔案以獲取更多顏色圖。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/386436.html
標籤:matplotlib 阴谋 颜色
上一篇:如何繪制聚類中心?
