检查图像点是否与圆相交

时间:2015-08-24 10:26:49

标签: matlab image-processing geometry intersection

我正试图探测肩膀。 在彩色图片中,您可以看到2个圆圈,大圆圈与4个点的边缘图片相交。 我使边缘更细,并将所有边缘点(x,y)放在一个数组中。 现在我试图使用圆方程找到交点:(x1 - x)+(y1-y) - r ^ 2 = 0。 问题是,方程只有一次零(有时从不)。我舍入了中心点和圆的半径,所以我可以得到一个特定的像素......没有帮助!

彩色照片: enter image description here

边缘图片: enter image description here

代码:

获取圈子:

%% im - colored image.
%  x1,y1,x2,y2,x3,y3 - are the right eye left eye and chin points.
%  The function calculates a new circule and sends its center points and 
%  radius back.



function [cx,cy,newRadius] = calcFaceCircle(im,x1,y1,x2,y2,x3,y3) 


m1 = (y1 - y3)/(x1-x3);
m2 = (y2 - y3)/(x2-x3);

d = sqrt((x2-x1)^2 + (y2-y1)^2);

centerX = ((m1*m2*(y2-y1) + m2*(x2 + x3) - m1*(x1 + x3))/(2*(m2-m1)));
centerY = (-(1/m1)*(centerX - (x3 + x2)/2) + (y3 + y2)/2);

radius = (sqrt((x1 - centerX)^2 + (y1 - centerY)^2));
%% new circle radius
newRadius = round(radius * 1.65);


newCircleX = (x1+x2)/2; 
newCircleY = (y1+y2)/2;
%% new circle center points
cx = round(newCircleX + sqrt(newRadius^2-(d/2)^2)*(y1-y2)/d);
cy = round(newCircleY + sqrt(newRadius^2-(d/2)^2)*(x2-x1)/d);

%% display the image with circles and points
figure(01);
imshow(im);
hold on; 

ang=0:0.01:2*pi; 
xp=radius*cos(ang);
yp=radius*sin(ang);
plot(x1,y1,'.');
plot(x2,y2,'.');
plot(x3,y3,'.');

plot(centerX+xp,centerY+yp);

xp=newRadius*cos(ang);
yp=newRadius*sin(ang);
plot(cx+xp,cy+yp);

hold off;

end

得到肩膀:

%% 
%edgeFun - array of edge image points (x,y).
%  x - circle's center X.
%  y - circle's center Y.
%  r - circle's radios
%
% The function calculates the interection points between edgeFunc and the
% circle.
% The funciton returns the first intersection and last intersection.

function [xL,xR] = getCropInfo(edgeFunc,x,y,r)

j=1;
flag = 0;
xL = -1;
xR = -1;
count = 0;

if(x > 0 && y > 0 && r > 0)


    X = edgeFunc(:,2);     

    [edgeX,edgeY] = size(X);


    for i = 1:edgeX
        x1 = edgeFunc(i,2);
        y1 = edgeFunc(i,1); 

        % check if the points intersect with the circle
        if((floor((x-x1)^2 + (-y+y1)^2 - r^2) == 0))
            if(flag == 0)  % check if first intersection point found
                disp('found');
                flag = 1;
                xL = i;  % first point
            end

            j = i;  % last point
            count = count + 1;
        end

        xR = j;

    end


else

    return;

end
if(count == 4)
    disp('ok');

end

2 个答案:

答案 0 :(得分:1)

为了让您的生活更轻松,我尝试了一下代码:

x1 = [-4,-3,-2,-1,0,1,2,3]; %//My edge, which is no where as handsome as yours
y1 = [-5,3,6,5,5,6,3,-5];

ang = 1:0.1:2*pi;
xp=4*cos(ang);  %//My circle, which is no where as good looking as yours
yp=4*sin(ang);

[xi,yi] = polyxpoly(x1,y1,xp,yp);  //Evaluate the intersects by interpolation

figure 
plot(x1,y1,'r')
hold on
plot(xp,yp,'b')

mapshow(xi,yi,'DisplayType','point','Marker','o');
  
    

xi =         1.8113;        -3.0529;        -3.5942;         2.7387

         

yi = 3.5662;         2.5771;        -1.7535;        -2.9094

  

To add insults to my simulation

正如您所看到的,数据的低分辨率并不重要,它总是会插入并评估交点,这理想情况下你想要的(因为你可能想要改变你的分辨率)图片)。 polyxpoly使用标准坐标系,因此很容易适应图像像素坐标系。

答案 1 :(得分:0)

最好注册边缘与圆相交的边缘线段。对于

这样的段值的起点和终点

S =(x1-x)+(y1-y)-r ^ 2

会有不同的符号(完全符合为零)。