圖像頻譜圖-直方圖三維可視化 python代碼
目錄
1.條紋噪聲圖像-頻譜圖3D可視化
#頻譜圖三維可視化思路:將圖像經過傅里葉變換,中心化,取log,再3D可視化
代碼:
import cv2
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def fft_plot3d(img_bgr):
img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
fft = np.fft.fft2(img_gray)
fft_shift = np.fft.fftshift(fft)
abs_fft = np.abs(fft_shift)
'''必須取log,因爲最大值包含着太大的能量了,導致直接歸一化,其它數值爲0'''
abs_fft = np.log(1 + abs_fft)
fft_aug_norm = cv2.normalize(abs_fft, 0, 255, norm_type=cv2.NORM_MINMAX).astype(np.uint8)
h,w = img_gray.shape
# 3D可視化,需要3維座標,np.meshgrid將重複的複製簡單化
c = np.meshgrid(np.arange(0,w), np.arange(0,h))
fig = plt.figure()
ax1 = Axes3D(fig)
print(c[0].shape,c[1].shape)
ax1.plot_surface(c[0], c[1], fft_aug_norm, cmap='rainbow') # 這種顏色比較好一點
ax1.set_xlabel('X')
ax1.set_ylabel('Y')
ax1.set_zlabel('Z')
plt.show()
return
if __name__=="__main__":
img_path = r"F:\DataSets\Security_VQD\color_cast\IMG_20200513_103921.jpg"
img_bgr = cv2.imread(img_path, -1)
fft_plot3d(img_bgr)
3D頻譜圖可視化:(右條紋噪聲圖,左爲其頻譜圖3D可視化)
#####################################################################################
2. 圖像二維直方圖3D可視化
# 圖像二維直方圖3D可視化
def hist2d_plot3d(img_bgr):
img_lab = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2LAB) # -128->128
img_lab = np.float64(img_lab)-128
hist, xbins, ybins = np.histogram2d(img_lab[...,1].flatten(), img_lab[...,2].flatten(), bins=[256,256], range=[[-128,128], [-128,128]])
hist = np.log(1+hist)
hist = cv2.normalize(hist, 0, 255, norm_type=cv2.NORM_MINMAX).astype(np.uint8)
print(hist.max(), hist.min())
print(hist.shape)
print(len(xbins), len(ybins))
# 開始繪製3D直方圖
fig = plt.figure()
# ax1 = Axes3D(fig)
ax1 = fig.add_subplot(1, 1, 1, projection='3d')
c = np.meshgrid(np.arange(-128,128), np.arange(-128,128))
ax1.plot_surface(c[0], c[1], hist, cmap='rainbow')
ax1.set_xlabel('a')
ax1.set_ylabel('b')
plt.show()
return
if __name__=="__main__":
img_path = r"F:\DataSets\Security_VQD\color_cast\IMG_20200513_103921.jpg"
img_bgr = cv2.imread(img_path, -1)
hist2d_plot3d(img_bgr)
3D直方圖可視化: