Search code examples
pythonimage-processingsignal-processingdct

Nice Way to Visualize DCT Coefficients as an Image


I took the image below:

lizzards

Divided it into 8X8 tiles, did a 2D DCT transform on each tile and chopped them to only the first 30 coefficients on each axis. Now I'm trying to visualize the result as an image that will help my intuition on the DCT plane.

Problems:

  • The DCT coefficient of (0,0) is way larger than all the others
  • I want to see a difference between positive and negative coefficients.

So far, the best transform I found is below:

def visualize_dct(d):
    d = np.log(abs(d).clip(0.1))
    maxi, mini = d.max(), d.min()
    d = 255*(d - mini)/(maxi-mini)
    return d

Which gave me the image below:

dct_visualized

Full code here: http://nbviewer.ipython.org/github/ihadanny/my-py-notebooks/blob/master/img_processing_04.ipynb

Any better ideas?


Solution

  • Found it: what I was looking for is histogram equalization. The implementation is quite straight forward:

    def visualize_dct(d):
        d = d + abs(d.min())
        h = np.histogram(d, bins=1000, range=(0, d.max()))
        c = 255.0*np.cumsum(h[0])/sum(h[0])
        new_img = np.zeros(d.shape)
        for index,value in np.ndenumerate( d ):
            new_img[index] = c[999.0*value/d.max()]    
        return new_img  
    

    result for a single tile:

    tile tile_dct

    and for the whole image:

    whole_image whole_image_dct

    (notice the difference between the simple tiles and the ones with lots of details)