在matlab中实现指定厚度函数

时间:2015-06-28 17:50:35

标签: matlab image-processing

我有一个定义为的厚度函数 enter image description here

是两个曲面之间距离为h(x)= 1的函数 在可接受的范围内[d+f D-f],否则h(x)= 0; D,d和f是常数。 xphi矩阵中的值。其他函数c定义为

enter image description here

给定d,D,f and phi。如何用matlab计算h(phi)和c(phi)?我想展示我的实施。你能看到并给我一些评论吗?感谢

function h=compute_h(phi)
  d=1;D=5;f=1;
  h=zeros(size(phi));
  idx1=find(phi<=d&phi>D);
  idx2=find(phi>(d+f)&phi<(D-f));
  idx3=find(phi>d&phi<=(d+f));
  idx4=find(phi>(D-f)&phi<=D);

  h(idx1)=0;
  h(idx2)=1;
  h(idx3)=1-((phi(idx3)-d-f)./f).^2;
  h(idx4)=1-((phi(idx4)-D+f)./f).^2;    
end
%% Defined phi
[Height Wide] = size(Img);% Assume size Img is 11 by 11
[xx yy] = meshgrid(1:Wide,1:Height);
phi = (sqrt(((xx - 3).^2 + (yy - 3).^2 )) - 3);
c=(1-compute_h(abs(phi)).*sign(phi).*sign(abs(phi)-D+f)

1 个答案:

答案 0 :(得分:2)

看起来你正走在正确的轨道上。您不需要使用find

function C = StackOverflow(Img, d, D, f)
    [Height, Width] = size(Img);% Assume size Img is 11 by 11
    [xx, yy] = meshgrid(1:Width,1:Height);
    phi = (sqrt(((xx - 3).^2 + (yy - 3).^2 )) - 3);
    C = c(phi, d, D, f);
end

function H = h(x, d, D, f)
    H = zeros(size(x));

    c1 = (d + f < x) & (x < D - f);
    c2 = (d < x) & (x <= d + f);
    c3 = (D - f <= x) & (x <= D);

    H(c1) = 1;
    H(c2) = 1 - ((x(c2) - d - f)/(f)).^2;
    H(c3) = 1 - ((x(c3) - D - f)/(f)).^2;

end

function C = c(phi, d, D, f)
    c1 = (abs(phi) >= D - f);
    c2 = (abs(phi) <= d + f);

    C = zeros(size(phi));
    C(c1) = (1 - h(abs(phi(c1)), d, D, f)) .* (sign(phi(c1)));
    C(c2) = (1 - h(abs(phi(c1)), d, D, f)) .* (-sign(phi(c2)));
end