
Performs pixelwise updates based on conditional distributions to sample from a Markov random field.
rmrf2d(
init_Z,
mrfi,
theta,
cycles = 60,
sub_region = NULL,
fixed_region = NULL
)
One of two options:
A matrix
object with the initial field configuration. Its
valuesmust be integers in {0,...,C}
.
A length 2 numeric
vector with the lattice dimensions.
A mrfi
object representing the
interaction structure.
A 3-dimensional array describing potentials. Slices represent
interacting positions, rows represent pixel values and columns represent
neighbor values. As an example: theta[1,3,2]
has the potential pairs of
values 0,2 in the second relative position of mrfi
.
The number of updates to be done (for each each pixel).
NULL
if the whole lattice is considered or a logical
matrix
with TRUE
for pixels in the considered region.
NULL
if the whole lattice is to be sampled or a
logical
matrix
with TRUE
for pixels to be considered fixed. Fixed
pixels are not updated in the Gibbs Sampler.
A matrix
with the sampled field.
This function implements a Gibbs Sampling scheme to sample from
a Markov random field by iteratively sampling pixel values from the
conditional distribution
A cycle means exactly one update to each pixel. The order pixels are sampled is randomized within each cycle.
If init_Z
is passed as a length 2 vector with lattice dimensions, the
initial field is sampled from independent discrete uniform distributions in
{0,...,C}
. The value of C is obtained from the number of rows/columns of
theta
.
A MRF can be sampled in a non-rectangular region of the lattice with the use of
the sub_region
argument or by setting pixels to NA
in the initial
configuration init_Z
. Pixels with NA
values in init_Z
are completely
disconsidered from the conditional probabilities and have the same effect as
setting sub_region = is.na(init_Z)
. If init_Z
has NA
values,
sub_region
is ignored and a warning is produced.
A specific region can be kept constant during the Gibbs Sampler by using the
fixed_region
argument. Keeping a subset of pixels constant is useful when
you want to sample in a specific region of the image conditional to the
rest, for example, in texture synthesis problems.
A paper with detailed description of the package can be found at https://arxiv.org/abs/2006.00383
# NOT RUN {
# Sample using specified lattice dimension
Z <- rmrf2d(c(150,150), mrfi(1), theta_potts)
#Sample using itial configuration
# }
# NOT RUN {
Z2 <- rmrf2d(Z, mrfi(1), theta_potts)
# View results
dplot(Z)
dplot(Z2)
# Using sub-regions
subreg <- matrix(TRUE, 150, 150)
subreg <- abs(row(subreg) - 75) + abs(col(subreg) - 75) <= 80
# view the sub-region
dplot(subreg)
Z3 <- rmrf2d(c(150,150), mrfi(1), theta_potts, sub_region = subreg)
dplot(Z3)
# Using fixed regions
fixreg <- matrix(as.logical(diag(150)), 150, 150)
# Set initial configuration: diagonal values are 0.
init_Z4 <- Z
init_Z4[fixreg] <- 0
Z4 <- rmrf2d(init_Z4, mrfi(1), theta_potts, fixed_region = fixreg)
dplot(Z4)
# Combine fixed regions and sub-regions
Z5 <- rmrf2d(init_Z4, mrfi(1), theta_potts,
fixed_region = fixreg, sub_region = subreg)
dplot(Z5)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab