Step 1: Change path and import necessary packages


In [5]:
import os
PATH="/Users/albertlee/NeuroCV/PCV/2Dhisteq/3D" # use your own path
os.chdir(PATH)

################

import matplotlib.pyplot as plt
import csv,gc
import matplotlib
import numpy as np
import nibabel as nb

Step 2: Setup Data


In [3]:
BINS=32 # histogram bins
RANGE=(10.0,300.0)

im = nb.load('Fear197.nii')
im = im.get_data()
img = im[:,:,:,0]

Step 3: Basic Exploratory Analysis


In [11]:
print img.shape

## print img.res -> memmap has no attribute 'res'
## print img.numbins -> memmap has no attribute 'numbins'


(896, 576, 1087)

XDimension = 896 YDimension = 576 ZDimension = 1087


In [ ]:
for z in range(1087):
    for y in range(576):
        for x in range(896):
        
            data = img
            
            # compute the histogram and store it
            (hist, bins) = np.histogram(data[data > 0], BINS, range=(0,BINS))
            hist_sum = np.add(hist_sum, hist)
            print "Processed cube {} {} {}".format(x,y,z)

Perform general and adaptive histogram equalization


In [ ]:
from PCV import tools
from PIL import Image

def histeq(im,nbr_bins=256):
    """    Histogram equalization of a grayscale image. """
    
    # get image histogram
    imhist,bins = histogram(im.flatten(),nbr_bins,normed=True)
    cdf = imhist.cumsum() # cumulative distribution function
    cdf = 255 * cdf / cdf[-1] # normalize
    
    # use linear interpolation of cdf to find new pixel values
    im2 = interp(im.flatten(),bins[:-1],cdf)
    
    return im2.reshape(im.shape), cdf