引言
陸陸續續在計算攝影學接觸了不少保邊濾波器,其重要性自不必說,可以用在圖像的增強,圖像抽象畫,高動態範圍圖像壓縮,圖像色調映射等。
今天介紹的WLS(最小二乘濾波器)即使其中一種,論文全稱《Edge-Preserving Decompositions for Multi-Scale Tone and Detail Manipulation》,作者Z. Farbman等,發表在ACM SIGGRAPH 2007。
這篇文章是基於最優化理論,代碼是公開,點此下載。當然有更好的濾波器不斷出現。但是這篇文章核心代碼只有十幾行左右,數學功底深厚,方法比較去值得研究和學習之用。這篇文章只是我看此論文一個筆錄,不正確之處歡迎指正。
算法
設計一個保邊濾波器可以看做是兩個矛盾的目標的結合體。對於一副輸入圖像
這裏,下標
將上式寫成矩陣形式(因爲矩陣比較清晰簡潔,很好用的工具,^_^):
這裏,
這裏
這裏
還有平滑項權重的設置,作者如下定義
注意到
matrix)。簡單解釋一下異性,異性因爲
這個算法的核心還是生成拉普拉斯這個矩陣。
先準備一下數據,一會兒再來填充一下這個矩陣。
第一步根據圖像
% Compute affinities between adjacent pixels based on gradients of L
dy = diff(L, 1, 1);
dy = -lambda./(abs(dy).^alpha + smallNum);
dy = padarray(dy, [1 0], 'post');
dy = dy(:);
dx = diff(L, 1, 2);
dx = -lambda./(abs(dx).^alpha + smallNum);
dx = padarray(dx, [0 1], 'post');
dx = dx(:);
這一步基本上就是按照公式(4)(5)編寫的,只是lambda前面多了一個負號,
接下來就是構造拉普拉斯矩陣了,這裏的拉普拉斯是一個對稱,只有少數幾個對角線有元素,其餘爲零的稀疏矩陣。
% Construct a five-point spatially inhomogeneous Laplacian matrix
B(:,1) = dx;
B(:,2) = dy;
d = [-r,-1];
A = spdiags(B,d,k,k);
e = dx;
w = padarray(dx, r, 'pre'); w = w(1:end-r);
s = dy;
n = padarray(dy, 1, 'pre'); n = n(1:end-1);
D = 1-(e+w+s+n);
A = A + A' + spdiags(D, 0, k, k);
這裏
看最終生成的一副拉普拉斯矩陣圖吧。
這張圖中可以看出每一行元素之和都爲0。其中緊靠主對角線元素的兩個對角線填充的是
完整的代碼如下:
function OUT = wlsFilter(IN, lambda, alpha, L)
%WLSFILTER Edge-preserving smoothing based on the weighted least squares(WLS)
% optimization framework, as described in Farbman, Fattal, Lischinski, and
% Szeliski, "Edge-Preserving Decompositions for Multi-Scale Tone and Detail
% Manipulation", ACM Transactions on Graphics, 27(3), August 2008.
%
% Given an input image IN, we seek a new image OUT, which, on the one hand,
% is as close as possible to IN, and, at the same time, is as smooth as
% possible everywhere, except across significant gradients in L.
%
%
% Input arguments:
% ----------------
% IN Input image (2-D, double, N-by-M matrix).
%
% lambda Balances between the data term and the smoothness
% term. Increasing lambda will produce smoother images.
% Default value is 1.0
%
% alpha Gives a degree of control over the affinities by non-
% lineary scaling the gradients. Increasing alpha will
% result in sharper preserved edges. Default value: 1.2
%
% L Source image for the affinity matrix. Same dimensions
% as the input image IN. Default: log(IN)
%
%
% Example
% -------
% RGB = imread('peppers.png');
% I = double(rgb2gray(RGB));
% I = I./max(I(:));
% res = wlsFilter(I, 0.5);
% figure, imshow(I), figure, imshow(res)
% res = wlsFilter(I, 2, 2);
% figure, imshow(res)
if(~exist('L', 'var')),
L = log(IN+eps);
end
if(~exist('alpha', 'var')),
alpha = 1.2;
end
if(~exist('lambda', 'var')),
lambda = 1;
end
smallNum = 0.0001;
[r,c] = size(IN);
k = r*c;
% Compute affinities between adjacent pixels based on gradients of L
dy = diff(L, 1, 1);
dy = -lambda./(abs(dy).^alpha + smallNum);
dy = padarray(dy, [1 0], 'post');
dy = dy(:); %公式(5)
dx = diff(L, 1, 2);
dx = -lambda./(abs(dx).^alpha + smallNum);
dx = padarray(dx, [0 1], 'post');
dx = dx(:); %公式(4)
% Construct a five-point spatially inhomogeneous Laplacian matrix
B(:,1) = dx;
B(:,2) = dy;
d = [-r,-1];
A = spdiags(B,d,k,k);
%構造主對角線
e = dx;
w = padarray(dx, r, 'pre'); w = w(1:end-r);
s = dy;
n = padarray(dy, 1, 'pre'); n = n(1:end-1);
D = 1-(e+w+s+n); %再加上單位矩陣,這裏元素都爲負數,先取反
A = A + A' + spdiags(D, 0, k, k); %A+A'爲非主對角線元素
% Solve
OUT = A\IN(:); %公式(3)
OUT = reshape(OUT, r, c);%轉換爲矩陣
算法介紹完畢,關於其應用可以參考論文的主頁,見末尾。
關於拉普拉斯矩陣
拉普拉斯矩陣在計算機圖形和計算攝影學經常會遇到的,比如說灰度圖像着色問題,泊松圖像編輯,HDR,還有保邊濾波器的設計,當然在計算機圖形學中的也有諸多應用,所以掌握其解法還是非常有必要的。
它經常會出現在如下的最優化問題當中
第一項是數據項,度量
參考資料
FARBMAN, Z., FATTAL, R., LISCHINSKI, D., AND SZELISKI, R. 2008. Edge-preserving decompositions for multi-scale tone and detail manipulation. ACM Transactions on Graphics (Proc. SIGGRAPH) 27, 3 (August).
Krishnan D, Fattal R, Szeliski R. 2013. Efficient preconditioning of laplacian matrices for computer graphics[J]. ACM Transactions on Graphics (Proc. SIGGRAPH), 32,4
轉載請保留以下信息
作者 | 日期 | 聯繫方式 |
---|---|---|
風吹夏天 | 2015年9月21日 | [email protected] |