【问题标题】:How to compute the total variation of an image in matlab如何在matlab中计算图像的总变化
【发布时间】:2014-08-27 11:21:51
【问题描述】:

我正在尝试使用空间一阶导数的 l1 范数在 matlab 中计算图像的总变化。代码如下:

function TV = compute_total_variation1(y)
% y is the image
nbdims = 2;

% check number of channels in an image
if size(y,1)==1 || size(y,2)==1
    % we have one dimension
    nbdims = 1;
end

if size(y,1)>1 && size(y,2)>1 && size(y,3)>1
    % we have three dimensions
    nbdims = 3;
end

if nbdims==1
    TV = sum(abs(diff(y)));
    return;
end

% the total variation weight is 1
% weight_tv = ones(size(y));

g = gradient(y);
% compute using the l1 norm of the first order derivatives
TV = sum( abs(g),nbdims+1);

% TV = TV .* weight_tv;
TV = sum(TV(:));

我是否使用 l1 范数正确计算了总变异?

编辑:

function TV = compute_total_variation1(y)
% y is the image
nbdims = 2;

% check number of channels in an image
if size(y,1)==1 || size(y,2)==1
    % we have one dimension
    nbdims = 1;
end

if size(y,1)>1 && size(y,2)>1 && size(y,3)>1
    % we have three dimensions
    nbdims = 3;
end

if nbdims==1
    TV = sum(abs(diff(y)));
    return;
end

% the total variation weight is 1
% weight_tv = ones(size(y));

[gx gy] = gradient(y);
% compute using the l1 norm of the first order derivatives
% horizontal
TVgx = sum( abs(gx),nbdims+1);
% vertical
TVgy = sum( abs(gy),nbdims+1);
% TV = TV .* weight_tv;
TV = sum(TVgx(:)) + sum(TVgy(:));

【问题讨论】:

  • 如果你想让它更快,我更喜欢A = abs(img(1:end-1,:)-img(2:end,:)); B = abs(img(:,1:end-1)-img(:,2:end)); sum(A(:)) + sum(B(:))

标签: image matlab image-processing variations


【解决方案1】:

你没有考虑第二个维度的导数:

g = gradient(y)

只返回沿水平维度的导数,为了获得沿垂直维度的导数,您需要

[gx, gy] = gradient(y);

【讨论】:

  • 我已经编辑了问题并沿垂直方向添加了导数。验证码现在有效吗?
猜你喜欢
  • 2011-04-16
  • 2016-03-28
  • 2017-03-05
  • 2017-03-06
  • 2012-03-09
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 2015-04-27
相关资源
最近更新 更多