up [pdf]
from rsf.proj import *

Flow('rect',None,
     '''
     spike d1=1 d2=1 n1=20 n2=20 k1=10 k2=5 l2=16 mag=25 
     label1= unit1= label2= unit2= |
     smooth rect1=5 
     ''')
Flow('rect2','rect','reverse which=1 opt=i | cat axis=3 $SOURCE | max axis=3')
Flow('image','rect2','transp               | cat axis=3 $SOURCE | min axis=3')

Flow('noise','image','noise seed=2014 range=0.2')
Flow('median','noise','despike2 wide1=3 wide2=3')

Result('median','grey allpos=y screenratio=1 wanttitle=n')

# Red channel of an astronomical image

Fetch('m32.jpg','master',
      server='https://raw.githubusercontent.com',
      top='kwinkunks/timefreak')

Flow('m32','m32.jpg','jpg2byte | window n1=1 | dd type=float')

# median filter

Flow('m32-med','m32','despike2 wide1=5 wide2=5')

# gaussian filter

Flow('m32-gss','m32','gaussmooth rect=5 | transp | gaussmooth rect=5 | transp')

for case in ('','-med','-gss','-noise'):
    Result('m32'+case,'grey allpos=y screenratio=1 wanttitle=n')

# Another image

Fetch('M31_Wide.jpg','a010485',
      server='http://svs.gsfc.nasa.gov',
      top='vis/a010000/a010400')

Flow('m31','M31_Wide.jpg',
     '''
     jpg2byte | window n1=1 | dd type=float
     ''')

Result('m31','grey allpos=y screenratio=1 title=M31 transp=n')

Flow('m31-zoom','m31','window f2=5199 n2=800 f1=3699 n1=800')

Result('m31-zoom','grey allpos=y screenratio=1 title=M31 transp=n')

Flow('m31-slice','m31-zoom','window n2=1 f2=399')

Result('m31-slice','graph title="Image Slice" ')

# Quantize image values

Flow('m31-8','m31-zoom','math output="0.5+input/32" | dd type=int | dd type=float')

Plot('m31-8','grey allpos=y screenratio=1 wanttitle=n transp=n')

Flow('m31-8-slice','m31-8','window n2=1 f2=399')

Plot('m31-8-slice','graph wanttitle=n')

Result('m31-8','m31-8 m31-8-slice','SideBySideIso')

# Add noise

Flow('m32-noise','m32',
     '''
     noise seed=2015 type=n range=127.5 mean=127.5 |
     dd type=int | dd type=float
     ''')

for image in ('bricks','photomic','clouds'):
    jpg = image+'.jpg'
    Fetch(jpg,'master',
      server='https://raw.githubusercontent.com',
      top='kwinkunks/timefreak')
    Flow(image,jpg,'jpg2byte | window n1=1 | dd type=float')

def Impair(image,lines=False,plots=False,rect=6,offset=0):
    titles={}
    titles[image] = 'Original'
    low = image+'-low'
    titles[low] = 'Subsampled'
    Flow(low,image,'window j1=4 j2=4')
    gss = image+'-gss'
    titles[gss] = 'Smoothed'
    Flow(gss,image,'smooth rect1=%d rect2=%d' % (rect,rect))
    bit = image+'-bit'
    titles[bit] = 'Bit reduced'
    Flow(bit,image,'math output="0.5+input*4/256" | dd type=int | dd type=float')
    nse = image+'-nse'
    titles[nse] = 'Noisified'
    Flow(nse,image,'noise seed=2015 rep=y type=n mean=1 | mul $SOURCE')
    crp = image+'-crp'
    titles[crp] = 'Cropped'
    Flow(crp,image,'window f1=66 n1=68 f2=66 n2=68')

    images = [image,low,gss,bit,nse,crp]
    line = image+'-line'
    Plot(line,None,
         '''
         spike n1=2 mag=%d |
         graph min2=0 max2=200 yreverse=y
         plotcol=3 plotfat=3 wanttitle=n wantaxis=n pad=n 
         ''' % (100+offset))
    for case in images:
        if plots:
            if case==low:
                f2=25+offset
            elif case==crp:
                f2=34+offset
            else:
                f2=100+offset
            Plot(case,
                 '''
                 window n2=1 f2=%d | graph wanttitle=n labelsz=15 plotcol=3 plotfat=3
                 ''' % f2)
        elif lines:
            Plot(case+'0',case,
                 '''
                 grey wantaxis=n title="%s" transp=n mean=y wheretitle=t titlesz=30
                 ''' % titles[case])
            Plot(case,[case+'0',line],'Overlay')
        else:
            Plot(case,
                 '''
                 grey wantaxis=n title="%s" transp=n mean=y wheretitle=t titlesz=30
                 ''' % titles[case])
    Result(image,images,'SideBySideAniso',vppen='yscale=%g' % (10.24/7.5))

Flow('bricks2','bricks','window n1=200 n2=200 j1=2 j2=2')
Impair('bricks2')

Flow('photomic2','photomic',
     '''
     window n1=1738 |
     spline n1=200 o1=0 d1=%g | transp |
     spline n1=200 o1=0 d1=%g | transp
     ''' % (1738.0/200,1738.0/200))
Impair('photomic2')

Flow('m32-zoom','m32',
     '''
     spline n1=200 o1=0 d1=%g | transp |
     spline n1=200 o1=0 d1=%g | transp
     ''' % (866.0/200,866.0/200))

Impair('m32-zoom',lines=True,rect=3,offset=5)

Flow('m32-zoom2','m32-zoom','cp')

Impair('m32-zoom2',plots=True,rect=3,offset=5)

Flow('clouds2','clouds','window j1=4 j2=4')

Impair('clouds2')

End()

sfspike
sfsmooth
sfreverse
sfcat
sfmax
sftransp
sfmin
sfnoise
sfdespike2
sfgrey
sfjpg2byte
sfwindow
sfdd
sfgaussmooth
sfgraph
sfmath
sfmul
sfspline
sfcp

https://raw.githubusercontent.com/kwinkunks/timefreak/master/m32.jpg
http://svs.gsfc.nasa.gov/vis/a010000/a010400/a010485/M31_Wide.jpg
https://raw.githubusercontent.com/kwinkunks/timefreak/master/bricks.jpg
https://raw.githubusercontent.com/kwinkunks/timefreak/master/photomic.jpg
https://raw.githubusercontent.com/kwinkunks/timefreak/master/clouds.jpg