Commit bd682414 authored by Martinez-Carvajal German's avatar Martinez-Carvajal German
Browse files

I am deleting this python script because contrast

enhacement in FIJI is already well parallelized,
so it is much faster.

Though in FIJI, YOU MUST BE VERY CAREFUL WHEN YOU DO
!!SLICE!! OR !!STACK!! CONTRAST ENHANCEMENT
NORMALIZATION because sometimes stack based
normalization will not work... I don't know why
parent b004479b
from os import chdir, getcwd
import tifffile
import scipy
import numpy as np
import skimage.exposure
import imageio
import matplotlib.pyplot as plt
def Contrast_Stretching (Dir, FileName, p):
"""
Dir = Directory
# Tol = Tolerance (between 0-1)
"""
# Directory
chdir(Dir)
# Reading Image
print ('Reading Image')
global Image
Image = tifffile.imread(FileName)
# Defining limits
global v_min, v_max
v_min, v_max = np.percentile(Image, (p, 100 - p))
# Stretching
print('Stretching')
global Stretched
Stretched = skimage.exposure.rescale_intensity(Image, in_range = (v_min, v_max))
# Computing Histograms
global Bins, Hist_Orig, Hist_Bett
print('Computing Histograms V1')
if Image.dtype == np.uint8 :
Bins = 2**8
elif Image.dtype == np.uint16:
Bins = 2**16
else:
raise ValueError('Image type not yet considered')
Bins = np.arange(0,Bins,step = 1)
Hist_Orig = np.histogram(Image , bins = Bins)
Hist_Bett = np.histogram(Stretched, bins = Bins)
# Plotting
print('Plotting')
plt.close('all')
plt.figure()
#plt.plot(Bins[1:], Hist_Bett[0], lw = 0.5, label = 'Enhanced', color = "gray")
plt.plot(Bins[1:], Hist_Orig[0], lw = 0.5, label = 'Original', color = "red")
plt.xlabel('Gray Value')
plt.ylabel('Frequency ')
plt.legend()
plt.xlabel('Gray Value')
plt.ylabel('Frequency ')
Figure_Name = 'Hist_Norm_perc_{}.png'.format(p)
plt.savefig(Figure_Name, dpi = 300, set_size_inches = (1,1))
# Saving
print('Saving final result')
Final_Name = '{}_Norm_perc_{}.tif'.format(FileName, p)
tifffile.imsave(Final_Name, Stretched)
####################################################################################################################################
# MAIN
####################################################################################################################################
print('Main')
# Directory
Dir = '/home/german.martinez-carvajal/Desktop/These/Hashemi_segmentation/Tests/Test9_Rapide'
FileName = 'Image_Filtered.tif'
# Tolerance in (percentiles) (between 0-1)
percentile = 20
# Executing function
Contrast_Stretching(Dir, FileName, percentile)
print('Finished!')
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment