imagedist: Wasserstein Distance between Two Images

View source: R/image_dist.R

imagedistR Documentation

Wasserstein Distance between Two Images

Description

Given 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.

Usage

imagedist(x, y, p = 2)

Arguments

x

a grayscale image matrix of size (m\times n) with nonnegative entries.

y

a grayscale image matrix of size (m\times n) with nonnegative entries.

p

an exponent for the order of the distance (default: 2).

Value

a list containing

distance

the Wasserstein distance W_p(x,y).

plan

the optimal transport plan matrix of size (mn\times mn).

Examples


#----------------------------------------------------------------------
#                       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)))
print(paste0("Wasserstein-2 distance: ", round(W2$distance,4)))



T4transport documentation built on Jan. 11, 2026, 9:07 a.m.