Qt之图像处理(三)霍夫直线检测算法

霍夫直线检测算法

void MainWindow::Hough(QImage *origin,const int nLineRet, int (*pInfoRet)[3])
{


    QColor color;

    //pInfoRet[][0]代表Angle pInfoRet[][1]代表Dist pInfoRet[][2]代表Pixel数;
    //int pInfoRet [nLineRet][3];



    //极坐标域中最大的Rho和Theta
    int nMaxDist = sqrt((double)(origin->height()*origin->height()+origin->width()*origin->width()));
    int nMaxAngle = 90;


    //分配空间(累加器)
    int nAreaNum = nMaxAngle * nMaxDist * 2;
    int *pTransArea = new int[nAreaNum];
    memset(pTransArea,0,nAreaNum * sizeof(int));


    //转化到极坐标域
     BYTE bt;
     int nAngle;
     int nDist;
     double fRadian;
     for(int i = 0;iheight();i++)
     {
         for(int j = 0;jwidth();j++)
         {

             color = QColor(origin->pixel(j, i));

             bt = color.red();
             if(bt>100)
             {
                 for(nAngle = 0; nAngle=0)
                     {
                         pTransArea[nDist * nMaxAngle + nAngle]++;

                     }
                     else
                     {
                         nDist = fabs(nDist);

                         pTransArea[nMaxDist*nMaxAngle + nDist*nMaxAngle + nAngle]++;

                     }
                 }
             }
         }
     }


     //
     cout<<"pTransArea[0]"<Max_Value)
            {
                Max_Value = pTransArea[i];

                Max_Angle = i;

                cout<<"pTransArea[i]"<= -180)
                {

                    nThisAngle += 180;
                    nThisDist *= -1;
                }


                if(nThisAngle >= 180 && nThisAngle < 360)
                {
                    nThisAngle -= 180;
                    nThisDist *= -1;
                }


                if(fabs(nThisDist) <= nMaxDist && nThisAngle >= 0 && nThisAngle <= nMaxAngle*2)
                {

                    nThisAngle /= 2;
                    if(nThisDist >= 0)
                    {
                        pTransArea[nThisDist*nMaxAngle + nThisAngle] = 0;
                    }
                    else {
                        nThisDist = fabs(nThisDist);
                        pTransArea[nMaxDist*nMaxAngle + nThisDist*nMaxAngle + nThisAngle] = 0;
                    }
                }
            }
        }
    }

    delete []pTransArea;


}

调用:

 BYTE bt;
    for(int k = 0; k < 12; k++)
    {


        if(abs(pInfoRet[k][1])<30)
        {
            continue;
        }

        if(abs(pInfoRet[k][1])>500 && pInfoRet[k][0] == 0) continue;

        //cout<<"Dist"<

你可能感兴趣的:(图像处理)