图像频谱图-直方图三维可视化 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直方图可视化: