【发布时间】: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