Discrete Cosine Transform (DCT) Coefficient Distribution

160 Views Asked by At

I have two images :

Original Image

enter image description here

Binarized Image

enter image description here

I have applied Discrete Cosine Transform to the two images by dividing the 256x256 image into 8x8 blocks. After, I want to compare their DCT Coefficient Distributions.

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)])

The above code gets the DCT of the two images. I want to create their DCT Coefficient Distribution just like the image below:

enter image description here

The thing is I dont know how to plot it. Below is what I did:

#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

My questions are :

  1. What does the X and Y-axis in the figure represents?
  2. Are the value stored in noise_dct_data and ground_truth_dct_data, the DCT coefficients?
  3. Does the Y-axis represents the frequency of its corresponding DCT coefficients?
  4. Is the histogram appropriate to represent the DCT coefficient distribution?
  5. The DCT coefficients are normally classified into three sub-bands based on their frequencies, namely low, middle and high frequency-bands. What is the threshold value we can use to classify a DCT Coefficient in low, middle or high frequency band? In other words, how can we classify the DCT coefficient frequency bands radially? Below is an example of the radial classification of the DCT coefficient frequency bands.

enter image description here

The idea is based from the paper : Noise Characterization in Ancient Document Images Based on DCT Coefficient Distribution