让我们尝试一种更代数的方法,而不是图像处理方法。
您有白色像素 - 平面上的 2D 点,并且您希望找到三个半平面(直线),将这些点与平面的其余部分最好地分开。
那么,让我们开始吧
img=imread('http://i.stack.imgur.com/DL2Cq.png'); %// read the image
bw = img(:,:,1) > 128; %// convert to binary mask
[y x] = find(bw); %// get the x-y coordinates of white pixels
n=numel(x); %// how many do we have
为了稳定性,我们减去所有点的平均值 - 以原点为中心的白色像素:
mm = mean([x y],1);
mA = bsxfun(@minus, [x y], mm);
现在,一条线可以用两个参数来描述,所有满足L(1)*x + L(2)*y = 1的点(x, y)。为了找到一条所有点都严格在其一侧的线,这个不等式必须适用于集合中的所有点(x,y):L(1)*x + L(2)*y <= 1。我们可以强制这些不等式并使用quadprog 搜索满足此约束的最紧半平面L:
L1 = quadprog(eye(2), -ones(2,1), mA, ones(n,1));
L2 = quadprog(eye(2), ones(2,1), mA, ones(n,1));
L3 = quadprog(eye(2), [1; -1], mA, ones(n,1));
注意如何通过改变二次优化目标f,我们能够得到不同的半平面来分隔白色像素。
一旦我们有了三条线,我们就可以得到交点(将它们从原点移回mm):
x12=inv([L1';L2'])*ones(2,1)+mm';
x23=inv([L3';L2'])*ones(2,1)+mm';
x13=inv([L3';L1'])*ones(2,1)+mm';
您可以使用查看结果
imshow(bw,'border','tight');
hold all;
%// plot the lines
ezplot(gca, @(x,y) L1(1)*(x-mm(1))+L1(2)*(y-mm(2))-1, [1 340 1 352]);
ezplot(gca, @(x,y) L2(1)*(x-mm(1))+L2(2)*(y-mm(2))-1, [1 340 1 352]);
ezplot(gca, @(x,y) L3(1)*(x-mm(1))+L3(2)*(y-mm(2))-1, [1 340 1 352]);
%// plot the intersection points
scatter([x12(1) x23(1) x13(1)],[x12(2) x23(2) x13(2)],50,'+r');