2014-08-27 3 views
1

나는 공간 1 차 미분의 l1 표준을 사용하여 matlab에서 이미지의 전체 변형을 계산하려고합니다. 코드는 다음과 같습니다.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 표준을 사용하여 전체 변형을 계산합니까?

편집 : 유도체를 얻기 위하여 수평 차원을 따라

g = gradient(y) 

만 반환 유도체 :

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(:)); 
+1

더 빠르게하고 싶다면'A = abs (img (1 : end-1, :) - img (2 : end, :))와 같은 것을 선호합니다. B = abs (img (:, 1 : end-1) -img (:, 2 : end)); sum (A (:)) + sum (B (:))' – matheburg

답변

2

당신은 고려하지 않는 두 번째 희미한의 파생 상품 수직 치수를 따라야합니다.

[gx, gy] = gradient(y); 
+0

질문을 편집하고 파생물을 수직 방향으로 추가했습니다. 코드가 유효합니까? – Sebi