Halcon实战记录之二《判断两个直线或者矩形是否相交》

来源:互联网 发布:22周四维彩超正常数据 编辑:程序博客网 时间:2024/06/06 10:54

项目中使用到需要判断两个矩形是否相交,由于我使用Halcon不久,对其算子还不熟悉,不知道是否有现成的算子可以直接实现,如果有,还请各位朋友给留言指出,先谢谢了,我这里用了如下的方法。

1、如果两个矩形相交,那么它们中的线段一定会有相交的,我下面写了判断线段相交的函数:

C++代码

void CrossLine (HTuple hv_left, HTuple hv_right, HTuple hv_y, HTuple hv_top, HTuple hv_bottom, HTuple hv_x, HTuple hv_minDis, HTuple *hv_isCross){    // Local iconic variables    (*hv_isCross) = 0;    if (0 != (HTuple(hv_top<=(hv_y+hv_minDis)).TupleAnd(hv_bottom>=(hv_y-hv_minDis))))    {        if (0 != (HTuple(hv_left<=(hv_x+hv_minDis)).TupleAnd(hv_right>=(hv_x-hv_minDis))))        {            (*hv_isCross) = 1;        }    }}

Halcon 代码

CrossLine(::left, right, y, top, bottom, x, minDis : isCross){    isCross := 0    #判断一根横线和一根竖线是否交叉    #横线有三个参数:left, right和y    #竖线有三个参数:top, bottom和x    if((top <= y+minDis) and (bottom >= y-minDis))        if((left <= x+minDis) and (right >= x-minDis))                isCross := 1        endif    endif    return()}

2、判断两个矩形是否相交
C++代码

void CrossRect (HObject ho_rectangle1, HObject ho_rectangle2, HTuple hv_minDis, HTuple *hv_isCross){  // Local control variables  HTuple  hv_Number1, hv_Number2, hv_Row1, hv_Column1;  HTuple  hv_Row2, hv_Column2, hv_Row11, hv_Column11, hv_Row21;  HTuple  hv_Column21, hv_isCross1, hv_isCross2, hv_isCross3;  HTuple  hv_isCross4, hv_isCross5, hv_isCross6, hv_isCross7;  HTuple  hv_isCross8;  CountObj(ho_rectangle1, &hv_Number1);  CountObj(ho_rectangle2, &hv_Number2);  if (0 != (HTuple(hv_Number1==0).TupleOr(hv_Number2==0)))  {    (*hv_isCross) = 2;    return;  }  SmallestRectangle1(ho_rectangle1, &hv_Row1, &hv_Column1, &hv_Row2, &hv_Column2);  SmallestRectangle1(ho_rectangle2, &hv_Row11, &hv_Column11, &hv_Row21, &hv_Column21);  if (0 !=(HTuple(HTuple(HTuple(hv_Row1==hv_Row11).TupleAnd(hv_Column1==hv_Column11)).TupleAnd(hv_Row2==hv_Row21)).TupleAnd(hv_Column2==hv_Column21)))  {    (*hv_isCross) = 2;    return;  }  CrossLine(hv_Column1, hv_Column2, hv_Row1, hv_Row11, hv_Row21, hv_Column11, hv_minDis, &hv_isCross1);  CrossLine(hv_Column1, hv_Column2, hv_Row1, hv_Row11, hv_Row21, hv_Column21, hv_minDis, &hv_isCross2);  CrossLine(hv_Column1, hv_Column2, hv_Row2, hv_Row11, hv_Row21, hv_Column11, hv_minDis, &hv_isCross3);  CrossLine(hv_Column1, hv_Column2, hv_Row2, hv_Row11, hv_Row21, hv_Column21, hv_minDis, &hv_isCross4);  CrossLine(hv_Column11, hv_Column21, hv_Row11, hv_Row1, hv_Row2, hv_Column1, hv_minDis, &hv_isCross5);  CrossLine(hv_Column11, hv_Column21, hv_Row11, hv_Row1, hv_Row2, hv_Column2, hv_minDis, &hv_isCross6);  CrossLine(hv_Column11, hv_Column21, hv_Row21, hv_Row1, hv_Row2, hv_Column1, hv_minDis, &hv_isCross7);  CrossLine(hv_Column11, hv_Column21, hv_Row21, hv_Row1, hv_Row2, hv_Column2, hv_minDis, &hv_isCross8);  (*hv_isCross) = 0;  if (0 != ((((((((hv_isCross1+hv_isCross2)+hv_isCross3)+hv_isCross4)+hv_isCross5)+hv_isCross6)+hv_isCross7)+hv_isCross8)>1))  {    (*hv_isCross) = 1;  }}

Halcon代码

CrossRect(rectabgle1, rectabgle2::minDis : isCross){    count_obj (rectangle1, Number1)    count_obj (rectangle2, Number2)    if(Number1 == 0 or Number2 == 0)        isCross := 2        return()    endif    smallest_rectangle1 (rectangle1, Row1, Column1, Row2, Column2)    smallest_rectangle1 (rectangle2, Row11, Column11, Row21, Column21)    if(Row1 == Row11 and Column1 == Column11 and Row2 == Row21 and Column2 == Column21)        isCross := 2        return()    endif    CrossLine (Column1, Column2, Row1, Row11, Row21, Column11, minDis, isCross1)    CrossLine (Column1, Column2, Row1, Row11, Row21, Column21, minDis, isCross2)    CrossLine (Column1, Column2, Row2, Row11, Row21, Column11, minDis, isCross3)    CrossLine (Column1, Column2, Row2, Row11, Row21, Column21, minDis, isCross4)    CrossLine (Column11, Column21, Row11, Row1, Row2, Column1, minDis, isCross5)    CrossLine (Column11, Column21, Row11, Row1, Row2, Column2, minDis, isCross6)    CrossLine (Column11, Column21, Row21, Row1, Row2, Column1, minDis, isCross7)    CrossLine (Column11, Column21, Row21, Row1, Row2, Column2, minDis, isCross8)    isCross := 0    if(isCross1 + isCross2 + isCross3 + isCross4 + isCross5 + isCross6 + isCross7 + isCross8 > 1)        isCross := 1    endif    return ()}

注意:minDis解释–如果两个矩形或者线段不相交,但是距离小于minDis,则认为是相交

原创粉丝点击