Use Gibbs sampling and variational inference to denoise the image and use EM to segment the image
python 3-6
scipy
cv2
where the 𝑛𝑏𝑟(𝑖) means all connected nodes (neighbors) of node 𝑥𝑖. Due to use the Ising model, the pairwise potential term 𝛹s𝑖 (𝑥𝑖, 𝑥s)=exp(𝐽𝑥𝑖𝑥s), and the local evidence term 𝛹𝑖(𝑥𝑖)=𝒩(𝑦𝑖|𝑥𝑖, 𝜎^2) where the 𝑦𝑖 is the observed state.
So the final term of p in the pseudo-code is:
And the 𝑁𝑒𝑥𝑡𝑆𝑖𝑡𝑒(𝑗) function can just return j. But this may cause artifacts as the pixels left and above of 𝑥𝑖 will change before it does, while the pixels right and below will not have changed. Instead, first pass over all of the "even" pixels (i is even) and then make a pass over all of the "odd" pixels will help a lot, which can also simplify the iterations into two matrix operations(instead of n times iterations).
And when meet the boundary pixels, can try to "wrap" the boundary to deal with this problem, which means that the neighbors wrap around to the other side of the image.
where the 𝑛𝑏𝑟(𝑖) means all connected nodes (neighbors) of node 𝑥𝑖. 𝜇𝑖 is mean value of node i. And the main update rule is:
Based on the 𝜇, it will be iterated until convergence.
The 𝑁𝑒𝑥𝑡𝑆𝑖𝑡𝑒(𝑗) function and boundary problem are the same as the Gibbs sampling algorithm.