Skip to content

Commit 4c6568b

Browse files
committed
current image bleach correction
1 parent a5f5783 commit 4c6568b

File tree

4 files changed

+73
-83
lines changed

4 files changed

+73
-83
lines changed

imagepy/menus/Image/Adjust/bleachCorrection_plg.py

Lines changed: 71 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,83 +6,86 @@
66
import numpy as np
77
from imagepy import IPy
88
from scipy.optimize import curve_fit
9-
# from .histogram_plg import like
109
from skimage.exposure import histogram_matching
1110
import matplotlib.pyplot as plt
1211
import pandas as pd
1312

14-
def simple_ratio(imgs):
15-
x,y,z = imgs.shape
16-
ref = imgs[:,:,1].sum()/x/y
17-
ratio=np.zeros([z,2])
18-
ratio[0,0]=ref
19-
ratio[0,1]=1
20-
for i in range(z-1):
21-
ratio[i+1,0]=(imgs[:,:,i+1].sum()/x/y)
22-
ratio[i+1,1]=ref/(imgs[:,:,i+1].sum()/x/y)
23-
imgs[:,:,i+1] *= ratio[i+1,1].astype(imgs.dtype)
24-
return imgs, ratio
25-
26-
def exponential_fit(imgs):
27-
imgs_=imgs.astype('float32')
28-
x,y,z = imgs.shape
29-
t = np.linspace(0, z-1, z)
30-
intensity = (imgs.sum(1).sum(0)/x/y)
31-
popt, pcov = curve_fit(exponential_func, t, intensity)
32-
for i in range(z-1):
33-
ratio=exponential_func(0, popt[0], popt[1], popt[2])/ \
34-
exponential_func(i+1, popt[0], popt[1], popt[2])
35-
imgs_[:,:,i+1] *= ratio
36-
return imgs_.astype(imgs.dtype), popt, intensity
37-
38-
def histogram_match(imgs):
39-
x,y,z = imgs.shape
40-
for i in range(z-1):
41-
imgs[:,:,i+1] = histogram_matching.match_histograms(
42-
imgs[:,:,i+1],imgs[:,:,0])
43-
print(imgs.dtype)
44-
return imgs
13+
def copy(imgs):
14+
if isinstance(imgs, list):
15+
return [np.zeros_like(imgs[0])]
16+
else: return np.zeros_like(imgs)
4517

4618
def exponential_func(t, ref, k, offset):
4719
return ref * np.exp(- k * t) + offset
4820

49-
class Plugin(Simple):
50-
title = 'Bleach Correction'
51-
asyn = False
52-
note = ['8-bit','16-bit','stack']
53-
para = {'Method':'Simple ratio', 'Background':0}
54-
view = [(list, 'Method',
55-
['Simple ratio','Exponential fit','Histogram match'],
56-
str, 'Correction Method',''),
57-
(int,'Background',(0,65535),0,'Background intensity','')]
21+
def simple_ratio(imgs, back=0, inplace=True, out=print):
22+
if isinstance(back, int): back=imgs[back]
23+
buf = imgs if inplace else copy(imgs)
24+
z, (x, y) = len(imgs), imgs[0].shape
25+
values, k0 = np.zeros(z), back.sum()/x/y
26+
lim = 255 if imgs[0].dtype.type==np.uint8 else 65535
27+
for i in range(z):
28+
values[i] = imgs[i].sum()/x/y
29+
np.clip(imgs[i], 0, lim/(k0/values[i]), out=buf[i])
30+
np.multiply(buf[i], k0/values[i], out=buf[i], casting='unsafe')
31+
out(i, z)
32+
return buf, values, k0/values
5833

59-
def run(self, ips, imgs, para = None):
60-
Bconstant = para['Background']
61-
imgs_= np.array(imgs).transpose(1,2,0)
62-
if Bconstant != 0:
63-
for i in range (z):
64-
imgs_[:,:,i] -= Bconstant
65-
imgs_ = np.maximum(imgs_, 0)
66-
print (para['Method'])
67-
if para['Method'] == 'Simple ratio':
68-
imgs_,ratio = simple_ratio(imgs_)
69-
index = ['Frame%s'%i for i in range(1,imgs_.shape[2]+1)]
70-
columns = ['Mean value', 'Ratio']
71-
IPy.show_table(pd.DataFrame(ratio, index, columns), 'Log of simple ratio')
72-
if para['Method'] == 'Exponential fit':
73-
[imgs_, popt, intensity] = exponential_fit(imgs_)
74-
t = np.linspace(0, imgs_.shape[2]-1, imgs_.shape[2])
75-
fitresult = exponential_func(t, popt[0], popt[1], popt[2])
76-
plt.plot(t,intensity,'r.',label='Experiment')
77-
plt.plot(t,fitresult,'k',label=
78-
'Exponential fitted curve\n y=a*exp(-bx)+c\n a=%f\n b=%f\n c=%f'
79-
%(popt[0],popt[1],popt[2]))
80-
plt.title('Exponential fitted result')
81-
plt.legend()
82-
plt.show()
83-
if para['Method'] == 'Histogram match':
84-
imgs_=histogram_match(imgs_)
85-
IPy.show_img(imgs_.transpose(2,0,1),'Corrected %s'%ips.title)
34+
def exponential_fit(imgs, inplace=True, out=print):
35+
buf = imgs if inplace else copy(imgs)
36+
z, (x, y) = len(imgs), imgs[0].shape
37+
intensity = [i.sum()/x/y for i in imgs]
38+
popt, pcov = curve_fit(exponential_func, np.arange(z), intensity)
39+
k0 = exponential_func(0, popt[0], popt[1], popt[2])
40+
rst = exponential_func(np.arange(z), popt[0], popt[1], popt[2])
41+
lim = 255 if imgs[0].dtype.type==np.uint8 else 65535
42+
for i in range(z):
43+
np.clip(imgs[i], 0, lim/(rst[i]/k0), out=buf[i])
44+
np.multiply(buf[i], rst[i]/k0, out=buf[i], casting='unsafe')
45+
out(i, z)
46+
return buf, popt, intensity, rst
8647

48+
def histogram_match(imgs, back=0, inplace=True, out=print):
49+
if isinstance(back, int): back=imgs[back]
50+
buf = imgs if inplace else copy(imgs)
51+
z, (x, y) = len(imgs), imgs[0].shape
52+
for i in range(z):
53+
buf[i] = histogram_matching.match_histograms(imgs[i], back)
54+
out(i, z)
55+
return buf
8756

57+
def plot(popt, intensity, fitresult):
58+
t = np.arange(len(intensity))
59+
plt.plot(t, intensity,'r.',label='Experiment')
60+
plt.plot(t, fitresult,'k',label=
61+
'Exponential fitted curve\n y=a*exp(-bx)+c\n a=%f\n b=%f\n c=%f'%tuple(popt))
62+
plt.title('Exponential fitted result')
63+
plt.legend()
64+
plt.show()
8865

66+
def plot_after(popt, intensity, fitresult):
67+
import wx
68+
wx.CallAfter(plot, popt, intensity, fitresult)
69+
70+
class Plugin(Simple):
71+
title = 'Bleach Correction'
72+
note = ['8-bit','16-bit','stack']
73+
para = {'method':'Simple Ratio', 'new':True}
74+
view = [(list, 'method', ['Simple Ratio','Exponential Fit','Histogram Match'],
75+
str, 'Correction Method',''),
76+
(bool, 'new', 'show new window'),
77+
('lab', 'lab', 'Correct intensity based on your current slice!')]
78+
79+
def run(self, ips, imgs, para = None):
80+
if para['method'] == 'Simple Ratio':
81+
rst, value, ratio = simple_ratio(imgs, ips.img, not para['new'], self.progress)
82+
body = pd.DataFrame({'Mean value': value, 'Ratio':ratio})
83+
IPy.show_table(body, '%s-simple ratio'%ips.title)
84+
if para['method'] == 'Exponential Fit':
85+
rst, popt, intensity, fitrst = exponential_fit(imgs, not para['new'], self.progress)
86+
plot_after(popt, intensity, fitrst)
87+
body = {'Intensity':intensity, 'Fit':fitrst}
88+
IPy.show_table(pd.DataFrame(body), '%s-exp fit'%ips.title)
89+
if para['method'] == 'Histogram Match':
90+
rst = histogram_match(imgs, ips.img, not para['new'], self.progress)
91+
if para['new']: IPy.show_img(rst, '%s-corrected'%ips.title)

imagepy/menus/Image/Adjust/enhanceContrast_plg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class Plugin(Filter):
1010
title = 'Enhance contrast'
1111
note = ['all', 'auto_msk', 'auto_snap','preview']
1212
para = {'percentage': 0.3}
13-
view = [(float, 'percentage', (0,100), 2, 'Saturated pixels', '%')]
13+
view = [(float, 'percentage', (0,100), 4, 'Saturated pixels', '%')]
1414

1515
def run(self, ips, snap, img, para = None):
16-
up, down = np.percentile(snap, (0, 100 - para['percentage']/100))
16+
up, down = np.percentile(snap, (0, 100 - para['percentage']))
1717
return exposure.rescale_intensity(snap, in_range=(up, down))

imagepy/menus/Image/Adjust/normalize_plg.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class Plugin(Simple):
1515

1616
def run(self, ips, imgs, para = None):
1717
imgs_= np.array(imgs).astype('float64').transpose(1,2,0)
18-
x, y, z = imgs_.shape
1918
if para['if3d']:
2019
if para['Sb']:
2120
imgs_ -= imgs_.min()

imagepy/menus/Plugins/temporal_plg.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,6 @@
77
from imagepy.core.manager import ColorManager
88
from imagepy import IPy
99

10-
def color_code(imgs,cmap,start,end):
11-
x,y,z = imgs.shape
12-
z = end - start
13-
imgLUT = np.zeros([x,y,3],'float32')
14-
for i in range(start, end):
15-
for rgb in range (2):
16-
imgLUT[:,:,rgb] += imgs[:,:,i] * \
17-
cmap[np.floor((i+1)*256/z).astype(np.int)-1,rgb]
18-
for rgb in range(2):
19-
imgLUT[:,:,rgb] = 255 * imgLUT[:,:,rgb] / imgLUT[:,:,rgb].max()
20-
return imgLUT
21-
2210
def color_code(img, lut):
2311
idx = np.linspace(0,255,len(img)).astype(int)
2412
cs = lut[idx].astype(np.uint32)

0 commit comments

Comments
 (0)