User:Dan Bron/Temp/JanJacobs

From J Wiki
Jump to navigation Jump to search

This page is supporting material for Jan Jacobs' selection thread of 2006.

a sample BMP file

require 'viewmat ~addons\image3\image3.ijs'
imPath=:'c:\temp\'
echo=:1!:2&2

T0=:		4
n=:		33
c=:		(<:n)%:1e_2%T0
allTs=:	T0*c^i.n
alpha=:	0.1
beta=:		10

R=:2
NR=:(((>&0 *. <:&R) @ | @ j./"1)#]) ,/ ,"0/~ i: R

Fid=:		3 : '*: gamma - y.{mu'
Reg=:		3 : '(#NR)-+:+/y.="2 |.&g NR'
E=:		3 : '(Fid y.) + beta * Reg y.'
f0=:		3 : 0						NB. functional architecture
		'g T'	=: y.                           NB. (compact version)
    		gHat	=.? ($ gamma) $ L
    		g	=:(((E gHat) - E g)<: - T * ^. alpha)}g ,: gHat
 		g;T*c
)

f1=: 3 : 0							NB. implementation
  g =. g0							NB. (transformed version)
  for_T. allTs do.
    gHat=.  ? ($ gamma) $ L
    fid=.   *: gamma - g { mu
    reg=.   (#NR)-+:+/g="2 |.&g NR
    e=.     fid + beta * reg
    nfid=.   *: gamma - gHat { mu
    nreg=.   (#NR)-+:+/gHat="2 |.&g NR
    ne=.    nfid + beta * nreg
    de=.    ne - e
    accept=.de <: - T * ^. alpha
    g=.     (gHat*accept) + g*-.accept
  end.
  g
)

echo'START'
gamma=: {."1 read_image imPath,'mbra_22','.bmp'	NB. read in image
viewmat gamma						NB. display
mu=:30 33 208 213						NB. by inspection of histogram
L=:#mu
g0=: ({."1 @ /:"1)&.|: | mu -/"0 2 gamma		NB. init state from shortest distance gamma to mu
mean=:+/ % #
]mu=: /:~ (<.@+&0.5) g0 (mean/.)&, gamma		NB. improved estimate

NB.======functional architecture==================
echo'    computing arch0 of mmd...'
(9!:1) 7^5							NB. fix seed for random number generator
arch0=: <. mu {~ >{. f0^:n (g0;T0)			NB. quantise (MRF & SA)
viewmat arch0						NB. display
NB.======implementation===========================
echo'    computing arch1 of mmd...'
(9!:1) 7^5							NB. fix seed for random number generator
gn=:f1''							NB. prepare array of random states
arch1=: <. mu {~ gn 					NB. quantise (MRF & SA)
viewmat arch1						NB. display
NB.======verification=============================
echo'    check arch0==arch1: ',;('NOK';'OK'){~arch0-:arch1
echo'READY'