The total variation of a vector, matrix, or list of matrices. Uses L1 norm of discrete gradients for vectors and L2 norm of discrete gradients for matrices.

tv(value, ...)

Arguments

value

An Expression, vector, or matrix.

...

(Optional) Expression objects or numeric constants that extend the third dimension of value.

Value

An Expression representing the total variation of the input.

Examples

rows <- 10
cols <- 10
Uorig <- matrix(sample(0:255, size = rows * cols, replace = TRUE), nrow = rows, ncol = cols)

# Known is 1 if the pixel is known, 0 if the pixel was corrupted
Known <- matrix(0, nrow = rows, ncol = cols)
for(i in 1:rows) {
   for(j in 1:cols) {
      if(stats::runif(1) > 0.7)
         Known[i,j] <- 1
   }
}
Ucorr <- Known %*% Uorig

# Recover the original image using total variation in-painting
U <- Variable(rows, cols)
obj <- Minimize(tv(U))
constraints <- list(Known * U == Known * Ucorr)
prob <- Problem(obj, constraints)
result <- solve(prob, solver = "SCS")
result$getValue(U)
#>           [,1]     [,2]     [,3]     [,4]     [,5]      [,6]     [,7]     [,8]
#>  [1,] 235.0000 313.3977 312.9312 310.0597 297.7336 281.65656 281.6365 281.6403
#>  [2,] 415.0000 314.2499 314.2813 311.1897 297.7986  71.00001 281.6395 282.0189
#>  [3,] 227.0000 314.2175 318.0000 316.6306 314.7278 308.75011 298.2558 284.4663
#>  [4,] 341.3480 323.1193 320.4950 318.2808 316.4811 316.48539 316.4898 290.0102
#>  [5,] 437.0000 333.8089 327.3023 321.2944 316.4820 316.48347 465.0000 320.1587
#>  [6,] 345.9296 342.7687 337.6650 329.5103 316.4828 173.00000 244.0000 320.1582
#>  [7,] 346.9047 346.9067 346.9085 346.9119 346.0035 346.01181 345.1322 342.9804
#>  [8,] 346.9055 346.9063 346.9071 348.7477 346.0000 348.86401 349.9296 342.9838
#>  [9,] 346.9056 346.9057 425.0000 384.0000 384.2425 384.32351 473.0000 342.9894
#> [10,] 346.9056 346.9057 425.0000 458.0000 550.0000 384.32351 642.0000 342.9894
#>           [,9]    [,10]
#>  [1,] 281.0000 281.0000
#>  [2,] 280.9897 280.9897
#>  [3,] 280.9939 280.9939
#>  [4,] 281.0000 287.0000
#>  [5,] 320.1588 320.1588
#>  [6,] 320.1578 320.1578
#>  [7,] 342.9818 342.9818
#>  [8,] 342.9828 361.0000
#>  [9,] 342.9947 342.9947
#> [10,] 343.0000   0.0000