PDA

查看完整版本 : [求助]好象很简单的问题,大家帮我看看嘛,谢谢咯~~


yitsoh
2008-05-11, 17:05
我是一个新手,在做区域生长处理镜面反射的科目。
大家教我怎么运行下面这个东东嘛 (有一个原图,我还不知道怎么弄进去):cry:

function contraGrowth(img)
re=imread(img);
yuantu=re;
re=double(re);
jname=sprintf('%s%s','j',img);
ename=sprintf('%s%s','e',img);
de=imread(jname);
ed=imread(ename);
[x,y]=Get2(de)
to=1;
while x~=0&y~=0
r=0.0;g=0.0;b=0.0; num=0.0;
%1 假设没有位于边界的点,否则要考虑超界
eq=equation(de(x-1,y-1,:));
if eq~=1
r=r+4*re(x-1,y-1,1);
g=g+4*re(x-1,y-1,2);
b=b+4*re(x-1,y-1,3);
num=num+4;
end
%2
eq=equation(de(x-1,y,:));
if eq~=1
r=r+6*re(x-1,y,1);
g=g+6*re(x-1,y,2);
b=b+6*re(x-1,y,3);
num=num+6;
end
%3
eq=equation(de(x-1,y+1,:));
if eq~=1
r=r+4*re(x-1,y+1,1);
g=g+4*re(x-1,y+1,2);
b=b+4*re(x-1,y+1,3);
num=num+4;
end
%4
eq=equation(de(x,y-1,:));
if eq~=1
r=r+6*re(x,y-1,1);
g=g+6*re(x,y-1,2);
b=b+6*re(x,y-1,3);
num=num+6;
end
%5
eq=equation(de(x,y+1,:));
if eq~=1
r=r+6*re(x,y+1,1);
g=g+6*re(x,y+1,2);
b=b+6*re(x,y+1,3);
num=num+6;
end
%6
eq=equation(de(x+1,y-1,:));
if eq~=1
r=r+4*re(x+1,y-1,1);
g=g+4*re(x+1,y-1,2);
b=b+4*re(x+1,y-1,3);
num=num+4;
end
%7
eq=equation(de(x+1,y,:));
if eq~=1
r=r+6*re(x+1,y,1);
g=g+6*re(x+1,y,2);
b=b+6*re(x+1,y,3);
num=num+6;
end
%8
eq=equation(de(x+1,y+1,:));
if eq~=1
r=r+4*re(x+1,y+1,1);
g=g+4*re(x+1,y+1,2);
b=b+4*re(x+1,y+1,3);
num=num+4;
end
%9
% r=r+re(x,y,1);
% g=g+re(x,y,2);
% b=b+re(x,y,3);
% num=num+1;

r1=r/num;
g1=g/num;
b1=b/num;
re(x,y,1)=r1;re(x,y,2)=g1;re(x,y,3)=b1;
de(x,y,:)=[0,255,255];
to=to+1;
if mod(to,4)==1
[x,y]=Get1(de);
end
if mod(to,4)==2
[x,y]=Get2(de);
end
if mod(to,4)==3
[x,y]=Get3(de);
end
if mod(to,4)==0
[x,y]=Get4(de);
end
end
figure,
subplot(1,2,1),imshow(yuantu);
subplot(1,2,2),imshow(uint8(re));
name=sprintf('%s%s','r',img);
imwrite(uint8(re),name);
%从上向下求取第一个红色点的位置,如果整幅图像没有则返回[0,0]
function [x,y]=Get1(img)
[M,N]=size(img(:,:,1));
x=0;y=0;
for i=1:M
for j=1:N
if img(i,j,1)==255&img(i,j,2)==0&img(i,j,3)==0
x=i;
y=j;
return;
end
end
end
%从下向上求取第一个红色点的位置,如果整幅图像没有则返回[0,0]
function [x,y]=Get2(img)
[M,N]=size(img(:,:,1));
x=0;y=0;
for i=1:M
for j=1:N
if img(M-i+1,N-j+1,1)==255&img(M-i+1,N-j+1,2)==0&img(M-i+1,N-j+1,3)==0
x=M-i+1;
y=N-j+1;
return;
end
end
end
%从左向右求取第一个红色点的位置,如果整幅图像没有则返回[0,0]
function [x,y]=Get3(img)
[M,N]=size(img(:,:,1));
x=0;y=0;
for i=1:N
for j=1:M
if img(j,i,1)==255&img(j,i,2)==0&img(j,i,3)==0
x=j;
y=i;
return;
end
end
end
%从右向左求取第一个红色点的位置,如果整幅图像没有则返回[0,0]
function [x,y]=Get4(img)
[M,N]=size(img(:,:,1));
x=0;y=0;
for i=1:N
for j=1:M
if img(M-j+1,N-i+1,1)==255&img(M-j+1,N-i+1,2)==0&img(M-j+1,N-i+1,3)==0
x=M-j+1;
y=N-i+1;
return;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function eq=equation(x)
if x(1)==255&x(2)==0&x(3)==0
eq=1;
else
eq=0;
end