我有两张图片:
原始图像
二值化图像
我已通过将256x256图像分成8x8的块,对两个图像应用了离散余弦变换。之后,我想比较它们的DCT系数分布。
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
import numpy as np
import os.path
import scipy
import statistics
from numpy import pi
from numpy import sin
from numpy import zeros
from numpy import r_
from PIL import Image
from scipy.fftpack import fft, dct
from scipy import signal
from scipy import misc
if __name__ == '__main__':
image_counter = 1
#Opens the noisy image.
noise_image_path = 'noise_images/' + str(image_counter) + '.png'
noise_image = Image.open(noise_image_path)
# Opens the binarize image
ground_truth_image_path = 'ground_truth_noise_patches/' + str(image_counter) + '.png'
ground_truth_image = Image.open( ground_truth_image_path)
#Converts the images into Ndarray
noise_image = np.array(noise_image)
ground_truth_image = np.array(ground_truth_image)
#Create variables `noise_dct_data` and `ground_truth_dct_data` where the DCT coefficients of the two images will be stored.
noise_image_size = noise_image.shape
noise_dct_data = np.zeros(noise_image_size)
ground_truth_image_size = ground_truth_image.shape
ground_truth_dct_data = np.zeros(ground_truth_image_size)
for i in r_[:noise_image_size[0]:8]:
for j in r_[:noise_image_size[1]:8]:
# Apply DCT to the two images every 8x8 block of it.
noise_dct_data[i:(i+8),j:(j+8)] = dct(noise_image[i:(i+8),j:(j+8)])
# Apply DCT to the binarize image every 8x8 block of it.
ground_truth_dct_data[i:(i+8),j:(j+8)] = dct(ground_truth_image[i:(i+8),j:(j+8)])
上面的代码获取两个图像的DCT。我要创建其DCT系数分布,如下图所示:
事情是我不知道如何绘制它。以下是我所做的:
#Convert 2D array to 1D array
noise_dct_data = noise_dct_data.ravel()
ground_truth_dct_data = ground_truth_dct_data.ravel()
#I just used a Histogram!
n, bins, patches = plt.hist(ground_truth_dct_data, 2000, facecolor='blue', alpha=0.5)
plt.show()
n, bins, patches = plt.hist(noise_dct_data, 2000, facecolor='blue', alpha=0.5)
plt.show()
image_counter = image_counter + 1
我的问题是:
X
和Y-axis
代表什么?noise_dct_data
和ground_truth_dct_data
中? Y-axis
代表其对应的DCT系数的频率吗? 这个想法是基于论文:Noise Characterization in Ancient Document Images Based on DCT Coefficient Distribution
在我看来,您共享的绘图示例看起来像是内核密度绘图。密度图“直方图的一种变化,它使用内核平滑来绘制值,从而通过平滑噪声来实现更平滑的分布。” (请参见https://datavizcatalogue.com/methods/density_plot.html)
这个基于matplotlib构建的seaborn库具有kdeplot函数,它可以处理两组数据。这是一个玩具示例:
import numpy as np
from scipy.fftpack import dct
import seaborn
sample1 = dct(np.random.rand(100))
sample2 = dct(np.random.rand(30))
seaborn.kdeplot(sample1, color="r")
seaborn.kdeplot(sample2, color="b")
请注意,重新运行此代码会产生稍有不同的图像,因为我使用的是随机生成的数据。