cvHoughLines2霍夫直线检测函数详解及源码解析

转载请注明出处。
文章链接:https:

霍夫变换是一种在图像中寻找直线、圆及其他简单形状的方法,利用Hough变换在二值图像中找到直线。本文主要介绍opencv自带的几种直线检测函数,以及主要检测函数cvHoughLines2()的源码解析。

目前opencv直线检测方法有如下三种:

1. CV_HOUGH_STANDARD(SHT)
    传统或标准Hough变换.每一个线段由两个浮点数(ρ,θ)表示,此中ρ是原点(0,0)到直线的距
    离,θ表示线段与x-轴之间的夹角。是以,矩阵类型必须是 CV_32FC2 type.

2. CV_HOUGH_PROBABILISTIC(PPHT)
    概率Hough变换(如果图像包含一些长的线性分割,则效率更高). 它返回线段分割而不是整条
    直线。每个分割用起点和终点来表示,所以矩阵(或创建的序列)类型是 CV_32SC4.

3. CV_HOUGH_MULTI_SCALE(MSHT)
    传统 Hough 变换的多标准变种。线段的编码体式格式与 CV_HOUGH_STANDARD 的一致。

opencv自带的几种直线检测函数,如下:

(一) 函数说明

!!!注意:参数中的theta为检测直线对应的垂线角度,从后面的源码解析可以看出。

CV_IMPL CvSeq*
cvHoughLines2( CvArr* src_image, void* lineStorage, int method,
               double rho, double theta, int threshold,
               double param1, double param2 )
函数说明:C接口中的hough检测实现了上述三种检测方法,调用时可通过method设置检测方法。
返 回 值:返回找到的线段序列.

参数说明:
        src_image   输入 8-比特、单通道 (二值) 图像
        lineStorage 指向保存结果位置的指针,既可以是内存块cvMemoryStorage,
                    也可以是N*1的矩阵数列(行数N将有助于限制直线的最大数量)
        method      采用的检测方法,可以是
                    CV_HOUGH_STANDARD(SHT)
                    CV_HOUGH_PROBABILISTIC(PPHT)
                    CV_HOUGH_MULTI_SCALE(MSHT)
        rho         以像素为单位的距离精度。另一种形容方式是直线搜索时的进步尺寸的
                    单位半径。一般设置为1
        theta       以弧度为单位的角度精度.另一种形容方式是直线搜索时的进步尺寸的
                    单位角度.一般设置为CV_PI/180.
        threshold   累加平面的阈值参数,即识别某部分为图中的一条直线时它在累加平面
                    中必须达到的值.阈值>threshold的线段才可以被检测通过并返回到
                    结果中.
        param1      1)对传统 Hough 变换,不使用(0).
                    2)对概率 Hough 变换,它是最小线段长度.x方向或y向有一者距离满足
                    要求即可.
                    3)对多尺度 Hough 变换,它是距离精度rho的分母(大致的距离精度是
                    rho而精确的应该是rho/param1 ).
        param2      1)对传统 Hough 变换,不使用 (0).
                    2)对概率 Hough 变换,这个参数表示在同一条直线上进行碎线段连接的
                    最大间隔值(gap), 即当同一条直线上的两条碎线段之间的间隔小于
                    param2时,将其合二为一。
                    3)对多尺度 Hough 变换,它是角度精度 theta 的分母 (大致的角度精
                    度是 theta 而精确的角度应该是 theta/param2).


void HoughLines(InputArray image, OutputArray lines,
                double rho, double theta, int threshold,
                double srn=0, double stn=0 )
函数说明:此函数实现了标准霍夫变换SHT和多尺度霍夫变换MSHT进行直线检测。
        调用时可通过method设置检测方法。
参数说明:
        image       InputArray类型的image,输入图像,即源图像,需为8位的单通道二进
                    制,可将任意的源图载入进来由函数修改成此格式后,填在此处。
        lines       OutputArray类型的lines,储存检测到线条的输出矢量.每一条线由
                    (ρ,θ),其中,ρ是离坐标原点((0,0)(也就是图像的左上角)的距离.
                    θ是弧度线条旋转角度(0~垂直线,π/2~水平线).
        rho         同cvHoughLines2中参数说明
        theta       同cvHoughLines2中参数说明
        threshold   同cvHoughLines2中参数说明
        srn         默认值0
                    对于多尺度霍夫变换,是第三个参数进步尺寸rho的除数距离。
                    粗略的累加器进步尺寸直接是第三个参数rho,而精确的累加器进步尺寸为
                    rho/srn。

        stn         默认值0
                    对于多尺度霍夫变换,srn表示第四个参数进步尺寸的单位角度theta的
                    除数距离。且如果srn和stn同时为0,就表示使用经典的霍夫变换。否则,
                    这两个参数应该都为正数。

void HoughLinesP(InputArray image, OutputArray lines,
                double rho, double theta, int threshold,
                double minLineLength=0, double maxLineGap=0 )
函数说明:C++接口将概率霍夫变换单独出来的函数。
参数说明:
        image           同HoughLines中参数说明
        lines           同HoughLines中参数说明
        rho             同cvHoughLines2中参数说明
        theta           同cvHoughLines2中参数说明
        threshold       同cvHoughLines2中参数说明
        minLineLength   同cvHoughLines2中参数param1-2)说明
        maxLineGap      同cvHoughLines2中参数param2-2)说明

(二) 函数使用

#include
#include
#include

int main(int argc, char** argv)
{
    IplImage* src;
    src = cvLoadImage( "./001.jpg", 0 );
    IplImage* dst = cvCreateImage( cvGetSize( src ), IPL_DEPTH_8U, 1 );
    IplImage* color_dst = cvCreateImage( cvGetSize( src ), IPL_DEPTH_8U, 3 );
    CvMemStorage* storage = cvCreateMemStorage(0);
    CvSeq* lines = 0;

    cvCanny( src, dst, 50, 100, 3 );
    lines = cvHoughLines2( dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 80, 30, 10 );

    cvCvtColor( dst, color_dst, CV_GRAY2BGR );
    for( int i = 0; i < lines ->total; i++ )
    {
        CvPoint* line = ( CvPoint* )cvGetSeqElem( lines, i );
        cvLine( color_dst, line[0], line[1], CV_RGB( 0, 255, 0 ) );
    }
    cvNamedWindow( "src", 1 );
    cvShowImage( "src", src );
    cvNamedWindow( "Hough", 1 );
    cvShowImage( "Hough", color_dst );
    cvWaitKey(0);

    return 0;
}

(一) HoughLines、HoughLinesP源码

先贴出HoughLines、HoughLinesP函数源码,可以看出,二者最终都调用了cvHoughLines2函数,因此,我们直接对cvHoughLines2源码进行解析。

void cv::HoughLines( InputArray _image, OutputArray _lines,
                     double rho, double theta, int threshold,
                     double srn, double stn )
{
    Ptr<CvMemStorage> storage = cvCreateMemStorage(STORAGE_SIZE);
    Mat image = _image.getMat();
    CvMat c_image = image;
    CvSeq* seq = cvHoughLines2( &c_image, storage, srn == 0 && stn == 0 ?
                    CV_HOUGH_STANDARD : CV_HOUGH_MULTI_SCALE,
                    rho, theta, threshold, srn, stn );
    seqToMat(seq, _lines);
}

void cv::HoughLinesP( InputArray _image, OutputArray _lines,
                      double rho, double theta, int threshold,
                      double minLineLength, double maxGap )
{
    Ptr<CvMemStorage> storage = cvCreateMemStorage(STORAGE_SIZE);
    Mat image = _image.getMat();
    CvMat c_image = image;
    CvSeq* seq = cvHoughLines2( &c_image, storage, CV_HOUGH_PROBABILISTIC,
                    rho, theta, threshold, minLineLength, maxGap );
    seqToMat(seq, _lines);
}

(二) cvHoughLines2源码解析

这里主要对概率霍夫变换和标准变换进行分析,直接上源码,注释在代码中~


CV_IMPL CvSeq*
cvHoughLines2( CvArr* src_image, void* lineStorage, int method,
               double rho, double theta, int threshold,
               double param1, double param2 )
{
    CvSeq* result = 0;

    CvMat stub, *img = (CvMat*)src_image;
    CvMat* mat = 0;
    CvSeq* lines = 0;
    CvSeq lines_header;
    CvSeqBlock lines_block;
    int lineType, elemSize;
    int linesMax = INT_MAX;
    int iparam1, iparam2;

    img = cvGetMat( img, &stub );

    if( !CV_IS_MASK_ARR(img))
        CV_Error( CV_StsBadArg, "The source image must be 8-bit, single-channel" );

    if( !lineStorage )
        CV_Error( CV_StsNullPtr, "NULL destination" );

    if( rho  0 || theta  0 || threshold  0 )
        CV_Error( CV_StsOutOfRange, "rho, theta and threshold must be positive" );

    if( method != CV_HOUGH_PROBABILISTIC )
    {
        lineType = CV_32FC2;
        elemSize = sizeof(float)*2;
    }
    else
    {
        lineType = CV_32SC4;
        elemSize = sizeof(int)*4;
    }

    if( CV_IS_STORAGE( lineStorage ))
    {
        lines = cvCreateSeq( lineType, sizeof(CvSeq), elemSize, (CvMemStorage*)lineStorage );
    }
    else if( CV_IS_MAT( lineStorage ))
    {
        mat = (CvMat*)lineStorage;

        if( !CV_IS_MAT_CONT( mat->type ) || (mat->rows != 1 && mat->cols != 1) )
            CV_Error( CV_StsBadArg,
            "The destination matrix should be continuous and have a single row or a single column" );

        if( CV_MAT_TYPE( mat->type ) != lineType )
            CV_Error( CV_StsBadArg,
            "The destination matrix data type is inappropriate, see the manual" );

        lines = cvMakeSeqHeaderForArray( lineType, sizeof(CvSeq), elemSize, mat->data.ptr,
                                         mat->rows + mat->cols - 1, &lines_header, &lines_block );
        linesMax = lines->total;
        cvClearSeq( lines );
    }
    else
        CV_Error( CV_StsBadArg, "Destination is not CvMemStorage* nor CvMat*" );

    iparam1 = cvRound(param1);
    iparam2 = cvRound(param2);

    switch( method )
    {
    case CV_HOUGH_STANDARD:
          icvHoughLinesStandard( img, (float)rho,
                (float)theta, threshold, lines, linesMax );
          break;
    case CV_HOUGH_MULTI_SCALE:
          icvHoughLinesSDiv( img, (float)rho, (float)theta,
                threshold, iparam1, iparam2, lines, linesMax );
          break;
    case CV_HOUGH_PROBABILISTIC:
          icvHoughLinesProbabilistic( img, (float)rho, (float)theta,
                threshold, iparam1, iparam2, lines, linesMax );
          break;
    default:
        CV_Error( CV_StsBadArg, "Unrecognized method id" );
    }

    if( mat )
    {
        if( mat->cols > mat->rows )
            mat->cols = lines->total;
        else
            mat->rows = lines->total;
    }
    else
        result = lines;

    return result;
}
static void
icvHoughLinesStandard( const CvMat* img, float rho, float theta,
                       int threshold, CvSeq *lines, int linesMax )
{
    const uchar* image;
    int step, width, height;
    int numangle, numrho;
    int total = 0;
    int i, j;
    float irho = 1 / rho;
    double scale;

    CV_Assert( CV_IS_MAT(img) && CV_MAT_TYPE(img->type) == CV_8UC1 );

    image = img->data.ptr;
    step = img->step;
    width = img->cols;
    height = img->rows;

    numangle = cvRound(CV_PI / theta);
    numrho = cvRound(((width + height) * 2 + 1) / rho);

    _accum.allocate((numangle+2) * (numrho+2));

    _sort_buf.allocate(numangle * numrho);
    _tabSin.allocate(numangle);
    _tabCos.allocate(numangle);
    int *accum = _accum, *sort_buf = _sort_buf;
    float *tabSin = _tabSin, *tabCos = _tabCos;

    memset( accum, 0, sizeof(accum[0]) * (numangle+2) * (numrho+2) );

    float ang = 0;
    for(int n = 0; n < numangle; ang += theta, n++ )
    {
        tabSin[n] = (float)(sin((double)ang) * irho);
        tabCos[n] = (float)(cos((double)ang) * irho);
    }

    for( i = 0; i < height; i++ )
        for( j = 0; j < width; j++ )
        {
            if( image[i * step + j] != 0 )
                for(int n = 0; n < numangle; n++ )
                {
                    int r = cvRound( j * tabCos[n] + i * tabSin[n] );
                    r += (numrho - 1) / 2;
                    accum[(n+1) * (numrho+2) + r+1]++;

                }
        }

    for(int r = 0; r < numrho; r++ )
        for(int n = 0; n < numangle; n++ )
        {
            int base = (n+1) * (numrho+2) + r+1;
            if( accum[base] > threshold &&
                accum[base] > accum[base - 1] && accum[base] >= accum[base + 1] &&
                accum[base] > accum[base - numrho - 2] && accum[base] >= accum[base + numrho + 2] )
                sort_buf[total++] = base;
        }

    icvHoughSortDescent32s( sort_buf, total, accum );

    linesMax = MIN(linesMax, total);
    scale = 1./(numrho+2);
    for( i = 0; i < linesMax; i++ )
    {
        CvLinePolar line;
        int idx = sort_buf[i];
        int n = cvFloor(idx*scale) - 1;

        int r = idx - (n+1)*(numrho+2) - 1;
        line.rho = (r - (numrho - 1)*0.5f) * rho;
        line.angle = n * theta;
        cvSeqPush( lines, &line );
    }

}

static void
icvHoughLinesProbabalistic( CvMat* image,
                            float rho, float theta, int threshold,
                            int lineLength, int lineGap,
                            CvSeq *lines, int linesMax )
{
        CvMat* accum = 0;
        CvMat* mask = 0;
        CvMat* trigtab = 0;
    CvMemStorage* storage = 0;

    CV_FUNCNAME( "icvHoughLinesProbalistic" );

    __BEGIN__;

    CvSeq* seq;
    CvSeqWriter writer;
    int width, height;
    int numangle, numrho;
    float ang;
    int r, n, count;
    CvPoint pt;
    float irho = 1 / rho;
    CvRNG rng = cvRNG(-1);
    const float* ttab;
    uchar* mdata0;

    CV_ASSERT( CV_IS_MAT(image) && CV_MAT_TYPE(image->type) == CV_8UC1 );

    width = image->cols;
    height = image->rows;

    numangle = cvRound(CV_PI / theta);
    numrho = cvRound(((width + height) * 2 + 1) / rho);

    CV_CALL( accum = cvCreateMat( numangle, numrho, CV_32SC1 ));
    CV_CALL( mask = cvCreateMat( height, width, CV_8UC1 ));
    CV_CALL( trigtab = cvCreateMat( 1, numangle, CV_32FC2 ));
    cvZero( accum );

    CV_CALL( storage = cvCreateMemStorage(0) );

    for( ang = 0, n = 0; n < numangle; ang += theta, n++ )
    {
        trigtab->data.fl[n*2] = (float)(cos(ang) * irho);
        trigtab->data.fl[n*2+1] = (float)(sin(ang) * irho);
    }
    ttab = trigtab->data.fl;
    mdata0 = mask->data.ptr;

    CV_CALL( cvStartWriteSeq( CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), storage, &writer ));

    for( pt.y = 0, count = 0; pt.y < height; pt.y++ )
    {
        const uchar* data = image->data.ptr + pt.y*image->step;
        uchar* mdata = mdata0 + pt.y*width;
        for( pt.x = 0; pt.x < width; pt.x++ )
        {
            if( data[pt.x] )
            {
                mdata[pt.x] = (uchar)1;
                CV_WRITE_SEQ_ELEM( pt, writer );
            }
            else
                mdata[pt.x] = 0;
        }
    }

    seq = cvEndWriteSeq( &writer );
    count = seq->total;

    for( ; count > 0; count-- )
    {

                int idx = cvRandInt(&rng) % count;
                int max_val = threshold-1, max_n = 0;
                CvPoint* pt = (CvPoint*)cvGetSeqElem( seq, idx );
                CvPoint line_end[2] = {{0,0}, {0,0}};
                float a, b;
                int* adata = accum->data.i;
                int i, j, k, x0, y0, dx0, dy0, xflag;
                int good_line;
                const int shift = 16;

                i = pt->y;
                j = pt->x;

        *pt = *(CvPoint*)cvGetSeqElem( seq, count-1 );

        if( !mdata0[i*width + j] )
            continue;

        for( n = 0; n < numangle; n++, adata += numrho )
        {
            r = cvRound( j * ttab[n*2] + i * ttab[n*2+1] );
            r += (numrho - 1) / 2;
            int val = ++adata[r];
            if( max_val < val )
            {
                max_val = val;
                max_n = n;
            }
        }

        if( max_val < threshold )
            continue;

                    a = -ttab[max_n*2+1];
                    b = ttab[max_n*2];
                    x0 = j;
                    y0 = i;

        if( fabs(a) > fabs(b) )
        {
            xflag = 1;
            dx0 = a > 0 ? 1 : -1;
            dy0 = cvRound( b*(1 << shift)/fabs(a) );
            y0 = (y0 << shift) + (1 << (shift-1));

        }
        else
        {
            xflag = 0;
            dy0 = b > 0 ? 1 : -1;
            dx0 = cvRound( a*(1 << shift)/fabs(b) );
            x0 = (x0 << shift) + (1 << (shift-1));
        }

        for( k = 0; k < 2; k++ )
        {
            int gap = 0, x = x0, y = y0, dx = dx0, dy = dy0;

            if( k > 0 )
                dx = -dx, dy = -dy;

            for( ;; x += dx, y += dy )
            {
                uchar* mdata;
                int i1, j1;

                if( xflag )
                {
                    j1 = x;
                    i1 = y >> shift;
                }
                else
                {
                    j1 = x >> shift;
                    i1 = y;
                }

                if( j1 < 0 || j1 >= width || i1 < 0 || i1 >= height )
                    break;

                mdata = mdata0 + i1*width + j1;

                if( *mdata )
                {
                                    gap = 0;
                                    line_end[k].y = i1;
                                    line_end[k].x = j1;
                }
                else if( ++gap > lineGap )
                    break;
            }
        }

        good_line = abs(line_end[1].x - line_end[0].x) >= lineLength ||
                    abs(line_end[1].y - line_end[0].y) >= lineLength;

        for( k = 0; k < 2; k++ )
        {
            int x = x0, y = y0, dx = dx0, dy = dy0;

            if( k > 0 )
                dx = -dx, dy = -dy;

            for( ;; x += dx, y += dy )
            {
                uchar* mdata;
                int i1, j1;

                if( xflag )
                {
                    j1 = x;
                    i1 = y >> shift;
                }
                else
                {
                                    j1 = x >> shift;
                                    i1 = y;
                                }

                mdata = mdata0 + i1*width + j1;

                if( *mdata )
                {

                    if( good_line )
                    {
                        adata = accum->data.i;
                        for( n = 0; n < numangle; n++, adata += numrho )
                        {
                            r = cvRound( j1 * ttab[n*2] + i1 * ttab[n*2+1] );
                            r += (numrho - 1) / 2;
                            adata[r]--;
                        }
                    }
                    *mdata = 0;
                }

                if( i1 == line_end[k].y && j1 == line_end[k].x )
                    break;
            }
        }

        if( good_line )
        {
            CvRect lr = { line_end[0].x, line_end[0].y, line_end[1].x, line_end[1].y };
            cvSeqPush( lines, &lr );
            if( lines->total >= linesMax )
                EXIT;
        }
    }

    __END__;

    cvReleaseMat( &accum );
    cvReleaseMat( &mask );
    cvReleaseMat( &trigtab );
    cvReleaseMemStorage( &storage );
}

今天的博客就到这里啦,欢迎大家在评论区互相学习讨论,我们下期见,三连哦~

Original: https://blog.csdn.net/duiwangxiaomi/article/details/126406184
Author: 对望小秘
Title: cvHoughLines2霍夫直线检测函数详解及源码解析

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/704494/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球