创建单侧距离地图

时间:2017-03-31 13:16:33

标签: matlab euclidean-distance

我用这个代码创建了一个黑色方块的零,中间有一条白色的垂直线,我想创建一条相对于这条线的距离图,但只在垂直线的左侧。我怎样才能做到这一点?

以下代码在线的两侧生成距离图。

c=zeros(500,500);
c(:,250)=1;
figure, imshow(c)

[D, idx]= bwdist(c,'euclidean')

2 个答案:

答案 0 :(得分:2)

您可以计算整个图像的距离贴图,然后将您不感兴趣的那边的零点(或将值设置为NaN

D = bwdist(c, 'euclidean'); 
D(:,251:end) = NaN;

更健壮的方式(不对任何列进行硬编码)是在调用c之前修改bwdist,方法是将行的右侧设置为1,以便生成0每个像素的距离为D = bwdist(cumsum(c, 2) > 0, 'euclidean'); 。您可以通过计算行中的累积总和来实现此目的

typedef struct
{
    int feet;
    float inch;
} distance;

enter image description here

答案 1 :(得分:0)

实际上,这个问题可能会以不同且具有挑战性的方式提出。如果我有不同的输入形状(不是单个列线),如果我想在特定的角度间隔(而不仅仅是左侧)找到距离图,该怎么办?这是我的解决方案。找到输入形状的(x,y)坐标,然后为每个像素找到特定方向+方向间隔的掩模。这是功能。

function mask = directional_mask(im, mainDirection, dirInterval)

% im: BW input image
% mainDirection : mask direction (in degrees)
% dirInterval:  mask direction interval (from dirMask-dirInterval to dirMask+dirInterval)

[Ys, Xs] = find(im);

[sizeX, sizeY] = size(im);

[X, Y] = meshgrid(1:sizeX,1:sizeY);

mask = im * 0;

for i = 1 : numel(Xs)

    refX = Xs(i);
    refY = Ys(i);

    [theta, ~] = cart2pol(X-refX, Y-refY);

    % adding pi/2 so that north angle is 0 degree
    dirmat = wrapTo2Pi(theta + pi/2);
    dirmat = rad2deg(dirmat);

    % upper and lower direction intervals
    thetaUp = mainDirection + dirInterval;
    thetaDown = mainDirection - dirInterval;

    dirmat2 = dirmat(:);

    % finding indices of the angle intervals
    thetas = [thetaUp, thetaDown];
    if thetaUp >= 360
        thetaUp = thetas(1) - 360;
        thetaDown = thetas(2);
        dirmat2((dirmat2>thetaUp) & (dirmat2<thetaDown)) = nan;
    elseif thetaDown < 0
        thetaUp = thetas(1);
        thetaDown = thetas(2) + 360;
        dirmat2((dirmat2>thetaUp) & (dirmat2<thetaDown)) = nan;
    else
        dirmat2((dirmat2>thetaUp) | (dirmat2<thetaDown)) = nan;
    end

    % final mask
    tmp = im*0;
    tmp(~isnan(dirmat2)) = 1;

    mask = mask | tmp;

end

mask = double(mask);

然后假设我有一个像下面那样的输入图像,我想计算45度的距离图。

im=zeros(100);
for i=35:65; im(i,i)=1; end
im2=imrotate(im,60); im=imresize(im2,size(im)); im=im~=0;

mainDirection = 45;
dirInterval = 5;
[mask] = directional_mask(im, mainDirection, dirInterval);

% original distance map
d = bwdist(im);

% directional distance map
d2 = mask.*d;

figure;
subplot(131); imagesc(mask+2*im), axis image; colorbar; title('Mask + Orig. Input')
subplot(132); imagesc(d), axis image;  colorbar; title('Initial Distance Map')
subplot(133); imagesc(d2), axis image;  colorbar; title('Directional Distance Map')
set(findall(gcf,'-property','FontSize'),'FontSize',16)

enter image description here

假设我希望距离图朝180度。只需更改“mainDirection”

即可
mainDirection = 180;

enter image description here

让我们尝试不同的形状。

im=zeros(100);
im(40:60,40:60)=1; im2=imrotate(im,30);im=imresize(im2,size(im)); im=im~=0;

mainDirection = 180;
dirInterval = 5;

enter image description here

如果您想要在特定方向间隔(theta + -interval)中使用遮罩,只需更改“dirInterval”即可。这是一个例子

mainDirection = 180;
dirInterval = 30;

enter image description here

在最初的问题中,@ Mac想要将距离图屏蔽到左侧。然后:

im=zeros(100); im(:,50)=1;

mainDirection = 270;
dirInterval = 90;

enter image description here