66import numpy as np
77from imagepy import IPy
88from scipy .optimize import curve_fit
9- # from .histogram_plg import like
109from skimage .exposure import histogram_matching
1110import matplotlib .pyplot as plt
1211import 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
4618def 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 )
0 commit comments