Commit 56f4c184 authored by Chris Evenhuis's avatar Chris Evenhuis
Browse files

Add README

parents
from __future__ import print_function
from ij import IJ, ImagePlus, WindowManager
from ij.process import ImageProcessor, LUT
from java.lang import Byte
from java.awt import Color
import math
from ij.gui import Plot
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def int2byte( ints ):
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
return [ int(i) if i<128 else int(i-256) for i in ints ]
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def create_LUT(fi=None):
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
''' Creates a traffic light LUT that ranges from
black -> green -> orange -> red
the transitions are controlled by array fi
length 9
0<=1 fi <=1
fi[i]<fi[i+1]
'''
if( fi==None):
fs = [0,0.1,0.2,0.3,.35,0.4,0.6,0.8,1.0]
else:
fs = fi
#black green orange
# | | | red
# | | | |
rs = [0, 0, 0, 0, 0, 0,175,255,255]
gs = [0, 78,150,240,255,240,175,125, 0]
bs = [0, 0, 0, 0, 0, 0, 0, 0, 0]
ip = [ int(f*256) for f in fs]
r = []
g = []
b = []
for i in range(1,len(fs)):
i0=ip[i-1]; di=ip[i]-ip[i-1]
r0=rs[i-1]; dr=rs[i]-rs[i-1]
g0=gs[i-1]; dg=gs[i]-gs[i-1]
b0=bs[i-1]; db=bs[i]-bs[i-1]
r = r + [ r0+dr*j/di for j in range(di) ]
g = g + [ g0+dg*j/di for j in range(di) ]
b = b + [ b0+db*j/di for j in range(di) ]
return r,g,b
imp = IJ.getImage()
lut = imp.getLuts()[0]
bytes = lut.getBytes()
if( False ):
print(type(lut))
print(type(bytes))
# how the bytw arrays are packed
print("- - - - - - ")
for i in range(0,256,8):
print(bytes[i], bytes[256+i], bytes[2*256+i])
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Jasby plat functions
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def plat( x, Pm, a, b ):
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if( isinstance(x,list) ):
#print("here")
return [ Pm*(1.-math.exp(-I*a))*math.exp(-I*b) for I in x ]
else:
return Pm*(1.-math.exp(-x*a))*math.exp(-x*b)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def plat_max(Pm, a, b ):
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Iop = -math.log( b/(a+b))/a
return [Iop,plat(Iop,Pm,a,b),]
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def find_root( f, x0s, v, *args ):
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
''' Simple secant root finder'''
[x0,x1] = x0s
[f0,f1] = [f( x,*args)-v for x in x0s]
for i in range(20):
x2 = x1-f1*(x1-x0)/(f1-f0)
[x0,x1] = [x1,x2]
[f0,f1] = [f1,f( x2,*args)-v ]
print("pp",f0,f1)
if( abs(f0-f1)<1e-6 ):
break
return 0.5*(x0+x1)
if( True):
rs,gs,bs = create_LUT()
lut = LUT( int2byte(rs),int2byte(gs),int2byte(bs) )
# create the ETR plot
I = [ 10000*i/200 for i in range(201)]
a = 1./1000.
b = 1./2000.
Imax = 10000.
pmax = plat_max(1,a,b)
I2 = pmax[0]
I1= find_root(plat, [0.,I2 ],0.5*pmax[1], 1.,a,b)
I3= find_root(plat, [I2,I2*2 ],0.8*pmax[1], 1.,a,b)
I4= find_root(plat, [I3,I3*2 ],0.5*pmax[1], 1.,a,b)
print(I1,I2,I3,I4)
fs = [0., 0.5*I1/I4, I1/I4,0.5*(I1+I2)/I4, I2/I4, 0.5*(I2+I3)/I4, I3/I4, 0.5*(I3+I4)/I4, 1.]
print(fs)
rs,gs,bs = create_LUT(fi=fs)
lut = LUT( int2byte(rs),int2byte(gs),int2byte(bs) )
xs = [I1,I2,I3,I4]
Vs = plat(I,1.,a,b)
plot = Plot("Plat","Light","ETR",I,Vs)
for j in range(1,len(I)):
i =I[j]
n = int((i-0)/I4*256)
n = min(max(0,n),255)
#print(n)
plot.setColor(Color( rs[n],gs[n],bs[n],255))
#plot.add("line",[i,i],[0,Vs[j]])
plot.add("filled",[I[j-1],I[j]],[Vs[j-1],Vs[j]])
print(xs)
plot.setColor(Color(0,0,0))
plot.add('circles', xs, plat( xs,1,a,b) )
plot.show()
imp.setLut(lut)
\ 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