-
Notifications
You must be signed in to change notification settings - Fork 0
/
mrf.py
187 lines (121 loc) · 4.04 KB
/
mrf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Author: Eleftherios Garyfallidis
Description: Implementation of different methods using Markov Random Fields.
'''
import form
#from draw import pyplot as plt
import pylab as plt
import scipy as sp
def denoise2DICM():
'''
Example taken from Bishop's book p.389
Iterated Conditional Modes (ICM) Discrete Case
Just a cross neighbourhood is allowed here. Other configurations are easy to apply as well.
'''
im=form.loadpic('mrf2.png')
im=sp.asarray(im,dtype='float64')
im[im>127]=255
im[im<=127]=0
print im.min(),im.max()
plt.matshow(im)
plt.show()
#observed noisy image
Y=sp.asarray(im,dtype='float64')
Y[Y==255]=1
Y[Y==0]=-1
#state of pixel
#X=sp.ones(Y.shape)
X=Y
beta,eta,h=1,1,0
E=sp.zeros(Y.shape)
converge=False
while(not converge):
Eprev=sp.sum(E)
for r in xrange(1,Y.shape[0]-1):
for c in xrange(1,Y.shape[1]-1):
#calculate energy for positive
x=1
Ep=-eta*x*Y[r,c]-beta*x*X[r,c+1]-beta*x*X[r,c-1] - beta*x*X[r-1,c]-beta*x*X[r+1,c]
#calculate energy for negative
x=-1
En=- eta*x*Y[r,c] - beta*x*X[r,c+1] - beta*x*X[r,c-1] - beta*x*X[r-1,c] - beta*x*X[r+1,c]
if En<Ep:
X[r,c]=-1
E[r,c]=En
else:
X[r,c]=1
E[r,c]=Ep
#E[r,c]=-eta*X[r,c]*Y[r,c]-beta*X[r,c]*X[r-1,c]-beta*X[r,c]*X[r+1,c]
#plt.matshow(E)
if Eprev-sp.sum(E)==0:
converge=True
plt.matshow(X)
return E,Y,X
def denoise1Dicm(noise='poisson',prior='quadratic',lambd=1,noise_amp=.5):
'''
Iterated Conditional Modes (ICM) Continuous Case
From Komodakis presentation use
gaussian & quadratic
1/2 sum((y_i-x_i)^2) + lambd*sum(x_i-x_i-1)**2
poisson & quadratic
1/2 sum((x_i-y_i*log(x_i))^2) + lambd*sum(x_i-x_i-1)**2
gaussian & linear prior
1/2 sum((y_i-x_i)^2) + lambd*abs(x_i-x_i-1)
'''
N=4
bins=1000
l=lambd
u=sp.linspace(0,N-1,bins)
print u
y=sp.cos(2*sp.pi*u)
print y
plt.plot(u,y,'.')
plt.show()
if noise=='gaussian':
y=y+ noise_amp*sp.random.normal(size=len(y))+2
if noise=='poisson':
y=y+noise_amp*sp.random.poisson(size=len(y))+2
#plt.figure()
line,=plt.plot(u,y,'.')
#plt.show()
#return
x=sp.zeros(len(y))
#x[0]=
x=y
#plt.ion()
count=0
while(True):
tmpx=x
if noise=='gaussian' and prior=='quadratic':
print noise, prior
for i in xrange(1,len(x)-1):
x[i]=(y[i]+2*l*(x[i-1]+x[i+1]))/(1+4*l)
x[0]=(y[0]+2*l*x[1]) /(1+2*l)
x[-1]=(y[-1]+2*l*x[-1]) /(1+2*l)
if noise=='poisson' and prior=='quadratic':
print noise, prior
for i in xrange(1,len(x)-1):
x[i]=(-1+l*x[i]*(x[i-1]+x[i+1])+sp.sqrt((x[i-1]+x[i+1])**2+16*l*y[i]))/(8*l)
x[0]=(-1+l*x[0]*x[1]+sp.sqrt(x[1]**2+8*l*y[0]))/(4*l)
x[-1]=(-1+l*x[-1]*x[-2]+sp.sqrt(x[-2]**2+8*l*y[-1]))/(4*l)
if noise=='gaussian' and prior=='linear':
print 'not implemented yet'
pass
plt.plot(u,x)
#line.set_ydata(x)
#plt.draw()
#'''
count=count+1
if count==100:
break
#'''
#if sum(x)-sum(tmpx) < 0.0001:
# break
plt.figure()
plt.plot(u,x)
if __name__ == "__main__":
#experICM()
#denoise1D()
pass