Matlab - 查找直线和周长之间的坐标
Matlab - Find Coordinates between a straight line and a perimeter
我分割了一只鼠标并使用 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 的周长
如您所见,该算法仅检测到一个点而不是第二个点(黄点)...我知道为什么但找不到解决方案。这是因为直线通过对角线切割周长但没有共同的坐标...
有人能解决我的问题吗?当然,我会采纳有关我的代码的任何建议 :)
非常感谢!
编辑:如果有更简单的解决方案,我当然会接受
当鼠标周界与直线交叉点的坐标为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));
这里不需要abs
和sqrt
函数来找到正确的点,只需要正确的中间值...
来自关于 ind2sub 的 Matlab 文档:
For matrices, [I,J] = ind2sub(size(A),find(A>5)) returns the same values as [I,J] = find(A>5).
我分割了一只鼠标并使用 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 的周长
如您所见,该算法仅检测到一个点而不是第二个点(黄点)...我知道为什么但找不到解决方案。这是因为直线通过对角线切割周长但没有共同的坐标...
有人能解决我的问题吗?当然,我会采纳有关我的代码的任何建议 :)
非常感谢!
编辑:如果有更简单的解决方案,我当然会接受
当鼠标周界与直线交叉点的坐标为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));
这里不需要abs
和sqrt
函数来找到正确的点,只需要正确的中间值...
来自关于 ind2sub 的 Matlab 文档:
For matrices, [I,J] = ind2sub(size(A),find(A>5)) returns the same values as [I,J] = find(A>5).