資源簡介
將2D圖像轉換為紅藍3D效果。由本人編寫,算法來自論文《A novel 2D-to-3D conversion technique based on relative height depth cue》by Yong Ju Jung等。
代碼片段和文件信息
tic;
clc;clear?all;
%%?檢測邊緣
f=imread(‘flower.jpg‘);
f_gray=rgb2gray(f);
f_gray=im2double(f_gray);
w1=[-1?-2?-1;0?0?0;1?2?1];???%?sobel圖像邊緣增強
w2=[-1?0?1;-2?0?2;-1?0?1];
g1=imfilter(f_grayw1);
g2=imfilter(f_grayw2);
g=abs(g1)+abs(g2);
%%?生成初始深度圖,initial_map
n=20;?%?畫20條線
[MN]=size(f_gray);
initial_map=ones(MN);
for?k=1:n
????initial_map(ceil(M/n*(k-1)+1):ceil(M/n*k):)=255/n*(k-1)*initial_map(ceil(M/n*(k-1)+1):ceil(M/n*k):);
end
initial_map=uint8(initial_map);
%figureimshow(initial_map)
%%?生成line_map
line=zeros(nN);
for?k=1:n
????line(k1)=ceil(M/n*k);
end
Elt=size(MN);
Es=size(MN);
Ee=size(MN);
E=size(MN);
a=0.1;
b=N/4;%?N/4
c=N/4;%?N/4
for?k=1:n
????for?j=2:N
????????for?i=1:M
???????????
????????????Elt(ij)=exp(-g(ij)/a);
????????????Es(ij)=abs(line(kj-1)-i)/b;
????????????Ee(ij)=abs(i-line(k1))/c;
????????????E(ij)=0.4*Elt(ij)+0.3*Es(ij)+0.3*Ee(ij);
????????end?
????????[minCminI]=min(E(:j));
????????line(kj)=minI;
????end
????
end
line_map=zeros(MN);
for?k=1:n
????for?j=1:N
????????line_map(line(kj)j)=1;
????end
end
figureimshow(line_map);
%%?生成深度圖depth_map
depth_map=zeros(MN);
for?k=1:n-1
????for?j=1:N
????????for?h=line(kj):line(k+1j)
????????????depth_map(hj)=255/n*(k-1);
????????end
????end
end
for?j=1:N
????for?u=line(nj):M
????????depth_map(uj)=255;
????end
end
depth_map=uint8(depth_map);
figureimshow(depth_map);
%imwrite(depth_map‘wall深度圖.bmp‘);
flab?=?rgb2hsi(f);???%flab為原始圖像f的hsi色彩空間版本
flab?=?im2double(flab);
depth_map=im2double(depth_map);
%?Pre-compute?Gaussian?domain?weights.
w=5;
[XY]?=?meshgrid(-w:w-w:w);
sigma_d=3;
sigma_r=0.1;
G?=?exp(-(X.^2+Y.^2)/(2*sigma_d^2));
%?Rescale?range?variance?(using?maximum?luminance).
%?Create?waitbar.
hh?=?waitbar(0‘Applying?bilateral?filter...‘);
set(hh‘Name‘‘Bilateral?Filter?Progress‘);
finaldepth=zeros(MN);
dim?=?size(flab);
for?oo=1:5
for?i?=?1:dim(1)
???for?j?=?1:dim(2)
??????
?????????%?Extract?local?region.
?????????iMin?=?max(i-w1);
?????????iMax?=?min(i+wdim(1));
?????????jMin?=?max(j-w1);
?????????jMax?=?min(j+wdim(2));
?????????I?=?flab(iMin:iMaxjMin:jMax:);
?????????II=depth_map(iMin:iMaxjMin:jMax);
??????
?????????%?Compute?Gaussian?range?weights.
?????????dI?=?I(::3)-flab(ij3);
?????????
?????????
?????????H?=?exp(-(dI.^2)/(2*sigma_r^2));???
??????
?????????%?Calculate?bilateral?filter?response.
?????????F?=?H.*G((iMin:iMax)-i+w+1(jMin:jMax)-j+w+1);?????%?權重系數
?????????
?????????norm_F?=?sum(F(:));
?????????depth_map(ij)=sum(sum(F.*II))/norm_F;
?????????%B(ij1)?=?sum(sum(F.*I(::1)))/norm_F;
?????????%B(ij2)?=?sum(sum(F.*I(::2)))/norm_F;
?????????%B(ij3)?=?sum(sum(F.*I(::3)))/norm_F;
????????????????
???end
???
end
waitbar(oo/10);
end
close(hh);
figureimshow(depth_map);
%imwrite(depth_map‘平滑wall深度圖.bmp‘);
%%?DIBR紅藍3D合成
fr=f(::1);
fg=f(::
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3759??2013-05-04?15:11??myfinalwork.m
-----------?---------??----------?-----??----
?????????????????3759????????????????????1
評論
共有 條評論