附件里的图像是预处理后的焊缝图像,我想找出中间断点的位置坐标。但不知道怎么做程序。请教

解决方案 »

  1.   

    感谢楼上两位。同时还想请教。
    这是我们上一届同学做的程序 对接接头焊缝检测,这只是一部分。
      
    BOOL SearchForVersus(LPSTR lpDIBBits, LONG lWidth, LONG lHeight,int a[],Point p[])
    {
    // 指向源图像的指针
    LPSTR lpSrc;
    // 指向缓存图像的指针
    LPSTR lpDst;
       // 图像每行的字节数
    LONG lLineBytes;
    //循环变量
    long i;
    long j;
    int k;
       //像素值
    unsigned char pixel;
    //八个方向和起始扫描方向
    int Direction[8][2]={{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1}};
    int BeginDirect;
       //起始边界点与当前边界点
        Point StartPoint,CurrentPoint,lPoint,rPoint,EndPoint;
       //是否找到起始点
    bool bFindStartPoint;
    bool bFindEndPoint;
    //是否扫描到一个边界点
    bool bFindPoint;
        //是否扫描到断点
    bool bFindBreakPoint;
        //是否扫描到最后
    bool end;
        POINTINFO *PointInfo,*PointInfo1;
        PointInfo=new POINTINFO[lWidth+100];
    PointInfo1=new POINTINFO[lWidth+100];
        //初始化结构数组
    for(i=0;i<lWidth+100;i++)
    {
    PointInfo[i].Mark=FALSE;
    }
    for(i=0;i<lWidth+100;i++)
    {
    PointInfo1[i].Mark=FALSE;
    }
    // 计算图像每行的字节数
    lLineBytes = WIDTHBYTES(lWidth * 8);   
    //先找到最左方的一起点
    bFindStartPoint = FALSE;
    //每列
    for(i =1;i < lWidth-1 && !bFindStartPoint; i++)
    {
    //每行
    for (j = 1;j < lHeight-1 && !bFindStartPoint;j++)
    {
     // 指向源图像倒数第j行,第i个象素的指针
    lpSrc = (char *)lpDIBBits + lLineBytes * j + i;
    //取得当前指针处的像素值,注意要转换为unsigned char型
    pixel = (unsigned char)*lpSrc;
    if(pixel == 0 || pixel == 1)
    {
    bFindStartPoint = TRUE;
    StartPoint.Height = j;
    StartPoint.Width = i; 
    *lpSrc=1;
    }
     }
     }
    if(bFindStartPoint==FALSE)
    return FALSE;
    BeginDirect = 1;
    //从初始点开始扫描
    CurrentPoint.Height = StartPoint.Height;
    CurrentPoint.Width = StartPoint.Width;
    PointInfo[0].Mark=TRUE;
    PointInfo[0].Height=StartPoint.Height;
    PointInfo[0].Width=StartPoint.Width;
    end=FALSE;
    i=0;
    while(!end && i<=lWidth+100)
    {
    bFindPoint = FALSE;
    while(!bFindPoint)
    {
    //沿扫描方向查看一个像素
    lpSrc = (char *)lpDIBBits + lLineBytes * (CurrentPoint.Height + Direction[BeginDirect][1])
     + (CurrentPoint.Width + Direction[BeginDirect][0]);
    pixel = (unsigned char)*lpSrc;
    if(pixel == 0 || pixel == 1)
    {
    PointInfo[i+1].Mark=TRUE;
    PointInfo[i+1].Height=CurrentPoint.Height+Direction[BeginDirect][1];
    PointInfo[i+1].Width=CurrentPoint.Width+Direction[BeginDirect][0];
    *lpSrc=1;
    CurrentPoint.Height+=Direction[BeginDirect][1];
    CurrentPoint.Width+=Direction[BeginDirect][0];
    i++;
    BeginDirect=1;
    bFindPoint=TRUE;
    }
    else
    {
    BeginDirect++;
    if(BeginDirect==4)//可能是断点,也可能是端点
    {//bFindPoint=TRUE;end=TRUE;
     if(i<50)
     {
    bFindBreakPoint=FALSE;
    for(j=1;j<20 && !bFindBreakPoint && CurrentPoint.Width+j<lWidth;j++)
    {
     for(k=0;k<3 && !bFindBreakPoint;k++)//&& CurrentPoint.Height+k<lHeight
    {
      lpSrc=(char *)lpDIBBits+lLineBytes*(CurrentPoint.Height-k)+CurrentPoint.Width+j;
      pixel=(unsigned char) *lpSrc;
      if(pixel == 0 || pixel == 1)      //则为断点
      {
    PointInfo[i+1].Mark=TRUE;
    PointInfo[i+1].Height=CurrentPoint.Height-k;
    PointInfo[i+1].Width=CurrentPoint.Width+j;
    *lpSrc=1;
    CurrentPoint.Height-=k;
    CurrentPoint.Width+=j;
    i++;
    BeginDirect=1;
    bFindPoint=TRUE;
    bFindBreakPoint=TRUE;
    break;
    }
    }
    }
    请教各位:
    1.我不明白为什么总用if(pixel == 0 || pixel == 1)  ,我觉得应该是if(pixel == 0 )或用if(pixel == 1),而不是在同一个判据下。
    2。不知道上面程序检测断点的是怎么做的?
    谢谢!