Wasserstein Distance between Two Images
imagedist.RdGiven two grayscale images represented as numeric matrices, compute their Wasserstein distance using an exact balanced optimal transport solver. Each image is interpreted as a discrete probability distribution on a common \((m\times n)\) grid. The ground cost is defined using the Euclidean distance between grid locations.
Value
a list containing
- distance
the Wasserstein distance \(W_p(x,y)\).
- plan
the optimal transport plan matrix of size \((mn\times mn)\).
Examples
# \donttest{
#----------------------------------------------------------------------
# Small MNIST-like Example
#----------------------------------------------------------------------
# DATA
data(digit3)
x <- digit3[[1]]
y <- digit3[[2]]
# COMPUTE
W1 <- imagedist(x, y, p=1)
W2 <- imagedist(x, y, p=2)
# SHOW RESULTS
print(paste0("Wasserstein-1 distance: ", round(W1$distance,4)))
#> [1] "Wasserstein-1 distance: 0.0998"
print(paste0("Wasserstein-2 distance: ", round(W2$distance,4)))
#> [1] "Wasserstein-2 distance: 0.1267"
# }