In [1]:
from photutils import Background2D, SigmaClip, MedianBackground

from skimage import color
from skimage import exposure

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

In [2]:
afmdata = np.genfromtxt('../Data/UnbackgroundedTXT/500nmGood-0')
afmdata= afmdata*(10**9)
height, width = afmdata.shape

In [3]:
plt.matshow(afmdata, origin='lower', cmap = 'viridis')


Out[3]:
<matplotlib.image.AxesImage at 0x11338e390>

In [4]:
sigma_clip = SigmaClip(sigma=3., iters=10)
bkg_estimator = MedianBackground()
bkg = Background2D(afmdata, (50, 50), filter_size=(3, 3), sigma_clip=sigma_clip, bkg_estimator=bkg_estimator)

In [5]:
plt.matshow(bkg.background, origin='lower', cmap = 'viridis')


Out[5]:
<matplotlib.image.AxesImage at 0x1134ceac8>

In [ ]:
plt.matshow(afmdata - bkg.background, origin='lower', cmap = 'viridis')

In [7]:
### This is the Backgrounded image shown two frames up. The frame below is super pale because I'm working
### on the histogram normalization

backgrounded = afmdata - bkg.background
plt.matshow(backgrounded, origin = 'lower', cmap = 'viridis')


Out[7]:
<matplotlib.image.AxesImage at 0x110ab2da0>

In [ ]:
bkguint8=np.zeros((height, width, 3))

factor=(255)/(backgrounded.max()-backgrounded.min())
for i in range(height):
    for j in range(width):
        intensity=np.int((backgrounded[i][j]-backgrounded.min())*factor)
        #afmimg[i][j]=np.array([np.int((afmdata[i][j]-afmdata.min())*factor),0,0])
        bkguint8[i][j]=np.array([intensity, intensity, intensity])
        
bkgguint8 = np.uint8(bkguint8)

In [19]:
plt.matshow(bkgguint8)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-19-546cd7f1ab75> in <module>()
----> 1 plt.matshow(bkgguint8)

NameError: name 'bkgguint8' is not defined

In [ ]:
eqlizd = exposure.equalize_adapthist(bkguint8, clip_limit = 0.03)
plt.matshow(eqlizd)

In [8]:
def backgroundremoval(source):
    """
    A function to remove gradients in the background of AFM micrographs. Takes in a .txt source file and returns the
    image as a np.uint8 data array for use by other image analysis software.
    """
    #Generate a numpy array from the .txt file and convert values from m to nm
    afmdata = np.genfromtxt(source)
    afmdata= afmdata*(10**9)
    height, width = afmdata.shape
    
    #Remove any background that is greater than 3 stddevs from the mean. Removes gradients in the image.
    sigma_clip = SigmaClip(sigma=3., iters=10)
    bkg_estimator = MedianBackground()
    bkg = Background2D(afmdata, (50, 50), filter_size=(3, 3), sigma_clip=sigma_clip, bkg_estimator=bkg_estimator)
    backgrounded = afmdata - bkg.background
    
    #Convert data to uint8 datatype
    bkguint8 = np.zeros((height, width, 3))
    
    factor = (255)/(backgrounded.max()-backgrounded.min())
    
    for i in range(height):
        for j in range(width):
            intensity = np.int((backgrounded[i][j]-backgrounded.min())*factor)
            bkguint8[i][j] = np.array([intensity, intensity, intensity])

    bkguint8 = np.uint8(bkguint8)
    
    return bkguint8

In [9]:
test = backgroundremoval('../Data/UnbackgroundedTXT/500nmGood-0')


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-5b79974451de> in <module>()
      1 test = backgroundremoval('../Data/UnbackgroundedTXT/500nmGood-0')
----> 2 plt.matshow(test, cmap = 'viridis')

/Users/wesleytatum/miniconda3/lib/python3.5/site-packages/matplotlib/pyplot.py in matshow(A, fignum, **kw)
   2338         ax  = fig.add_axes([0.15, 0.09, 0.775, 0.775])
   2339 
-> 2340     im = ax.matshow(A, **kw)
   2341     sci(im)
   2342 

/Users/wesleytatum/miniconda3/lib/python3.5/site-packages/matplotlib/axes/_axes.py in matshow(self, Z, **kwargs)
   7175         """
   7176         Z = np.asanyarray(Z)
-> 7177         nr, nc = Z.shape
   7178         kw = {'origin': 'upper',
   7179               'interpolation': 'nearest',

ValueError: too many values to unpack (expected 2)

In [17]:
plt.imshow(test, cmap = 'viridis', origin = 'lower' )


Out[17]:
<matplotlib.image.AxesImage at 0x11b865588>

In [ ]: