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]:
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]:
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]:
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)
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')
In [17]:
plt.imshow(test, cmap = 'viridis', origin = 'lower' )
Out[17]:
In [ ]: