【问题标题】:Matlab - Find Coordinates between a straight line and a perimeterMatlab - 查找直线和周长之间的坐标
【发布时间】:2016-06-16 16:44:59
【问题描述】:

我分割了一只鼠标并使用bwlabel 获取它的图像属性。因此,我可以访问质心的位置和鼠标的方向。我还使用bwperim 获取鼠标周长。

我想找到穿过质心的直线上的两个点,其方向与鼠标切割周边的方向相同。

我使用该代码找到了直线方程:

% E is a 2*2 matrix containing the coordinates of the centroid and the
% coordinates of the point which belong to the straight line and making 
% the right angle given by the orientation
coeffs = polyfit(E(:,1),E(:,2),1);
% Create the equation of the straight line
x = 1:width;
yfit = coeffs(1)*x+coeffs(2);
% Make sure there are only int values.
yfit = uint16(yfit);

我将我的值转换为uint16,因为我想填充一个新矩阵,我将与包含周长的矩阵进行比较。这就是我所做的:

% Create a matrix of zeros and set to 1 all the pixels which belong to the
% straight line 
k = 1;
temp = false;
m = false(size(iPerim));
while temp~=true
    temp = false;
    if yfit(k) > 0
        m(yfit(k),k)=1;
        temp = true;
    end
    k = k+1;
end
[t,p] = ind2sub(size(m), find(m==1));
minM = [min(p),min(t)];
% complete the straight line to don't have little holes
x = linspace(minM(1),D(1),width);
y = coeffs(1)*x+coeffs(2);
idx = sub2ind(size(m),round(y),round(x));
m(idx) = 1;

然后我将 m 与包含我的周长的矩阵 iPerim 进行比较:

% Compare the matrix of the perimeter and the matrix of the straight line
% and find the two points in common. It is the points where the straight
% line cut the perimeter
p = m & iPerim;
% Extract thoses coordinates 
[coordsY,coordsX] = ind2sub(size(p), find(p==1));

好吧,我是 Matlab 的新用户,所以我认为这不是一个优雅的解决方案,但结果如下:

矩阵m

我绘制 yfit 的周长

如您所见,该算法仅检测到一个点,而不是第二个点(黄点)...我知道原因,但我找不到解决方案。这是因为直线通过对角线切割周长,但没有共同的坐标......

有人可以解决我的问题吗?当然,我正在接受有关我的代码的任何建议 :)

非常感谢!

编辑:如果有更简单的解决方案,我显然会接受

【问题讨论】:

    标签: matlab image-processing matrix intersection


    【解决方案1】:

    当鼠标周长与直线交叉点的坐标为E(2,:)时,则该点在直线中的位置是距离最小的位置。例如。喜欢:

    [xLine, yLine] = find(m);    % x,y positions of the line
    dX = abs(xline-E(2,1))       % x-distance to x-coordinate of direction-point
    dY = abs(yLine-E(2,2))       % y-distance to y-coordinate of direction-point
    distP =  sqrt(dX.^2+dY.^2)   % distance of line-points to directon-point
    [~,indMin] = min(distP);     % index of line-point which has the minimum distance
    xPoint = xLine(indMin(1)); 
    yPoint = yLine(indMin(1));
    

    abssqrt 函数在这里不是找到正确点所必需的,仅用于正确的中间值...

    来自关于ind2sub的Matlab文档:

    对于矩阵,[I,J] = ind2sub(size(A),find(A>5)) 返回与 [I,J] = find(A>5) 相同的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 2020-03-04
      • 2017-05-29
      • 1970-01-01
      相关资源
      最近更新 更多