function L = matLap(I)
% L = matLap(I)
% Computes the "Matting Laplacian" matrix for image I with a window size
% of 3x3 (default). To adjust window size, change parameter 'win_size'
% Run time is approximately 70 seconds for 400x600 image on
% AMD Turion Neo x2 processor with 4GB RAM
win_size = 3; % Set window size to 3x3
win_els = win_size.^2; % calculate number of window elements
l_els = win_els.^2; %# of elements calculated in each forloop iteration
% Calculate the size of the border of pixels not in matlap calculation
% - For example, if win_size = 3x3, there is a 1 pixel border to avoid
% around the whole image.
win_bord = floor(win_size./2);
% epsilon in Matting Laplacian equation. Levin et al. report using this
% value, but can be adjusted depending on image.
e = 0.000001;
[m,n,c] = size(I); % find number of rows, columns, and colors of I
numpix = m*n; % calculate number of rows or columns for L
k = reshape(1:numpix, m, n); % create matrix of indices for each pixel
U = eye(win_size); % create a an identity matrix of win_size
D = eye(win_els); % create an identity matrix of win_els
% calculate number of terms that will be created in for loop
num_els = l_els*(m-2*win_bord)*(n-2*win_bord);
% create empty index vectors for L
ind_i = ones(1,num_els);
ind_j = ind_i;
% create empty vector to store calculated L terms
els = zeros(1,num_els);
% initialize counter for calculating where to deposit new index and L terms
count = 0;
% Begin Matting Laplacian term calculations!
for x = (1 + win_bord):(n - win_bord) % start and end within border
for y = (1 + win_bord):(m - win_bord) %start and end within border
% Extract (win_size x win_size) window from image and reshape to
% (win_els x c) matrix
wk = reshape(I(y-win_bord:y+win_bord,x-win_bord:x+win_bord,:),...
win_els, c);
% Extract pixel indices for window, reshape to (win_els x 1)
w_ind = reshape(k(y-win_bord:y+win_bord,x-win_bord:x+win_bord),...
1, win_els);
% Create grid of L locations
[i j] = meshgrid(w_ind, w_ind);
% Reshape to vectors
i = reshape(i,1,l_els);
j = reshape(j,1,l_els);
% Add locations to L index vectors
ind_i((count*l_els + 1):(count*l_els+l_els)) = i;
ind_j((count*l_els + 1):(count*l_els+l_els)) = j;
win_mu = mean(wk)'; % Calculate mean of colors in window
% Calculate normalized covariance matrix
% - direct calculation is slightly faster than using cov function
win_cov = wk'*wk/win_els-win_mu*win_mu';
% Calculate difference between window values and mean
dif = wk' - repmat(win_mu,1,win_els);
% Compute L terms
% Note that both '\' and inv have been tried and don't result in
% a noticeable time difference, likely due to the fact that the
% matrix being inverted is very small. So inv was used for clarity.
elements = D - (1 + dif(:,1:win_els)'*inv(...
win_cov + e./win_els.*U)*dif(:,1:win_els))...
./win_els;
% Add terms to L elements vector
els((count*l_els + 1):(count*l_els+l_els)) = ...
reshape(elements,1,l_els);
% increment count index
count = count + 1;
end
end
% Compute final L matrix. Note that sparse automatically adds together
% terms sharing the same location.
L = sparse(ind_i, ind_j, els, numpix, numpix);
没有合适的资源?快使用搜索试试~ 我知道了~
去雾代码和图片
共32个文件
jpg:13个
m:7个
bmp:6个
5星 · 超过95%的资源 需积分: 10 191 下载量 103 浏览量
2017-01-07
20:22:46
上传
评论 3
收藏 6.19MB ZIP 举报
温馨提示
内含一些去雾效果比较好的图像和代码
资源推荐
资源详情
资源评论
收起资源包目录
MATLAB CODE.zip (32个子文件)
MATLAB CODE
mountain 0.8 soft.jpg 42KB
pumpkins-input.bmp 703KB
ny17_photo.jpg 775KB
color_histeq.m 1KB
canberra_input.jpg 30KB
sweden_input.bmp 703KB
trans.m 3KB
hazefree.m 582B
mountain 0.85 soft.jpg 42KB
NY17 SOFT 0.75 206KB
HazeRemovalREADME.m 1KB
NY17 SOFT 0.85 210KB
NY17 SOFT 0.95.jpg 216KB
train.bmp 703KB
matLap.m 4KB
canyon-hazefree.png 437KB
mountain 0.95 soft.jpg 39KB
house-input.bmp 582KB
mountain-input.bmp 576KB
forest-input.jpg 59KB
NY17 SOFT 0.9.jpg 214KB
t.jpg 146KB
tiananmen1.bmp 791KB
swan_input.jpg 307KB
forest1.jpg 189KB
canyon-tc.png 20KB
mountain 0.9 soft.jpg 41KB
dark_channel.m 1KB
002.png 388KB
refine_t.m 477B
canyon-t.png 87KB
tokyo_input.jpg 91KB
共 32 条
- 1
资源评论
- qq_159753252017-10-13很不错,可以用
- qiyuefree2017-10-24资源很棒,赞!
- 路易X2017-05-07很不错,最近研究去雾
ghy004
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功