本文介紹一種利用移動最小二乘法來實現圖像變形的方法,該方法由用戶指定圖像中的控制點,並通過拖拽控制點來驅動圖像變形。假設p為原圖像中控制點的位置,q為拖拽后控制點的位置,我們利用移動最小二乘法來為原圖像上的每個像素點v構建相應的仿射變換lv(x),並通過該變換來計算得到圖像變形后的位置:
其中權重wi的表達式為wi = 1/|pi - v|2α。
仿射變換lv(x)由兩部分組成lv(x) = xM + T,其中M為線性轉換矩陣,T為平移量。事實上將最小化表達式對變量T求偏導后可以得到T的表達式T = q* - p*M,其中p* = ∑wipi/∑wi,q* = ∑wiqi/∑wi。
於是仿射變換可以化簡為lv(x) = (x - p*)M + q*,而最小化表達式可以變化為:
其中,
。
注意移動最小二乘法並未對轉換矩陣M進行條件限制,如果添加其他限制條件后,能得到不同形式的轉換矩陣M,文章根據不同的轉換矩陣M提出了三種變形方法:仿射變形(Affine Deformation)、相似變形(Similarity Deformation)和剛性變形(Rigid Deformation),下面分別介紹這三種方法。
- 仿射變形(Affine Deformation)
仿射變形是利用經典正規方程對最小化表達式直接求解得到的結果:
有了旋轉矩陣M的表達式后,我們得到變形的表達式:
由於用戶是通過控制q的位置來實現圖像變形,而p的位置是固定不變的,因此上式中大部分內容可以預先計算並保存,從而提高運算速度,重寫變形表達式如下:
其中。

% Precomputing the affine deformation: function data = Precompute_Affine(p,v,w) % Computing pstar: pstar = Precompute_pstar(p,w); % Precomputing the first matrix: M1 = v - pstar; np = size(p,1); nv = size(v,1); % Iterating on points: phat = cell(1,np); M2 = zeros(2,2,nv); for i = 1:np % Computing the hat points: phat{i} = bsxfun(@minus, p(i,:), pstar); % Computing the matrix elements: M2(1,1,:) = M2(1,1,:) + permute(w(:,i).*phat{i}(:,1).^2, [3,2,1]); M2(1,2,:) = M2(1,2,:) + permute(w(:,i).*phat{i}(:,1).*phat{i}(:,2), [3,2,1]); M2(2,1,:) = M2(1,2,:); M2(2,2,:) = M2(2,2,:) + permute(w(:,i).*phat{i}(:,2).^2, [3,2,1]); end % Computing the inverse: nv = size(v,1); IM2 = mmx('backslash', M2, repmat(eye(2), [1,1,nv])); % Computing the first product elements: F1 = [sum(M1.*squeeze(IM2(:,1,:))',2), sum(M1.*squeeze(IM2(:,2,:))',2)]; % Computing the A values: A = zeros(nv,np); for i = 1:np A(:,i) = sum(F1.*phat{i},2).*w(:,i); end % The data structure: data.A = A; end
- 相似變形(Similarity Deformation)
由於仿射變形包含非均勻縮放,因此其變形效果不是很好。相似變形是仿射變形的一個特殊子集,它的變形效果只包含平移、旋轉和均勻縮放,我們限制轉換矩陣M使其滿足MTM = λ2I,根據該條件得到相似變形的轉換矩陣M如下:
其中。
與仿射變形一樣,我們提取出可以預先計算的部分后得到變形表達式:
其中。

% Precomputing the similar deformation: function data = Precompute_Similar(p,v,w) % Computing pstar: pstar = Precompute_pstar(p,w); np = size(p,1); nv = size(v,1); % Iterating on points: phat = cell(1,np); mu = zeros(nv,1); for i = 1:np % Computing the hat points: phat{i} = bsxfun(@minus, p(i,:), pstar); % Updating the values of mu: mu = mu + w(:,i).*sum(phat{i}.^2,2); end % Computing the matrix A: A = cell(1,np); R1 = v - pstar; R2 = [R1(:,2),-R1(:,1)]; for i = 1:np L1 = phat{i}; L2 = [L1(:,2),-L1(:,1)]; % [col1 col2] % [col3 col4] A{i} = [w(:,i).*sum(L1.*R1,2), ... w(:,i).*sum(L1.*R2,2), ... w(:,i).*sum(L2.*R1,2), ... w(:,i).*sum(L2.*R2,2)]; end % Premultiplying A/mu: for i = 1:np A{i} = bsxfun(@rdivide, A{i}, mu); end % The data structure: data.A = A; end
- 剛性變形(Rigid Deformation)
最近許多研究都是關於剛性變形,也就是說變形不含任何縮放效果,我們進一步限制轉換矩陣M使其滿足MTM = I,這樣可以得到剛性變形,剛性變形的表達式如下:
其中,式中Ai表達式與相似變形中的Ai表達式相同。

% Precomputing the rigid deformation: function data = Precompute_Rigid(p,v,w) % Computing pstar: pstar = Precompute_pstar(p,w); np = size(p,1); % Iterating on points: phat = cell(1,np); for i = 1:np % Computing the hat points: phat{i} = bsxfun(@minus, p(i,:), pstar); end % Computing the matrix A: A = cell(1,np); R1 = v - pstar; R2 = [R1(:,2),-R1(:,1)]; for i = 1:np L1 = phat{i}; L2 = [L1(:,2),-L1(:,1)]; % [col1 col2] % [col3 col4] A{i} = [w(:,i).*sum(L1.*R1,2), ... w(:,i).*sum(L1.*R2,2), ... w(:,i).*sum(L2.*R1,2), ... w(:,i).*sum(L2.*R2,2)]; end % The norm of v-pstar: norm_v_pstar = sqrt(sum((v - pstar).^2, 2)); % The data structure: data.A = A; data.norm_v_pstar = norm_v_pstar; end
本文為原創,轉載請注明出處:http://www.cnblogs.com/shushen
參考文獻:
[1] Scott Schaefer, Travis McPhail, and Joe Warren. 2006. Image deformation using moving least squares. ACM Trans. Graph. 25, 3 (July 2006), 533-540.