# How to Calculate Total Variation (TV) of an Image?

TV is L1 norm of gradient of an image. So we've to find gradient of the image (which is still matrix, right?). Then take the sum of absolute values of the gradient matrix (So now it must be a scalar, right?).

Can I simply use imgradient function in matlab for first step? It gives two values magnitude and direction, so which one should I consider for next step?

After getting gradient matrix say Igrad, will just summing absolute values (sum(abs(Igrad(:))) give me TV? And it must be just a single scalar value right?

The Total Variation of an image $I$ can be computed using two formulas:

• $TV(I) = \sum_{x} \| \nabla I (x) \|_1$ (anisotropic TV);
• $TV(I) = \sum_{x} \sqrt{ \| \nabla I (x) \|_2^2 }$ (isotropic TV).

In practice, both formulas yield almost the same result.

Using matlab, what you propose implements the first formula, while the second one can be obtained by sqrt(gradI_x.^2 + gradI_y.^2).

• Thank you for your reply. I have one more query. Since the result of TV is scalar how are we adding it as a regularization term in many image restoration problems? Because images and other parameters are matrices. I don't know if this is proper place to ask this. Anyway thanks again – akhilc May 15 '14 at 16:05
• Two solutions that have the same data fidelity score will probably have different TV score. Thus, among the possible solutions, the one with the lowest TV is kept. – sansuiso May 15 '14 at 20:53
• I have a related inquiry. How can we calculate DTV (differential total variation) using Matlab? – Mohammad nagdawi Sep 1 '18 at 2:56
• @Mohammadnagdawi, Have a look on my answer here - dsp.stackexchange.com/a/57985/128. There is a MATLAB code. – Royi Oct 19 '19 at 20:58