2

我的问题是 astd:vector<vec4i> ( p_lines and filt_lines)是在函数中创建的,不能在函数末尾释放。所以我在运行代码时遇到free(): invalid pointer错误。在某些情况下,我会出现分段错误错误,但我无法使用 gdb 跟踪源。该函数的目的是找到图片上的边缘和交叉点,在大多数线条相交的区域中,它会淹没一个矩形。图片来自 ROS 视频源。

void Probabilistic_Hough( Mat src, Mat &result )
{
    result=src;
    cvtColor( src, src, CV_RGB2GRAY );
    Canny( src, src, 50, 200, 3 );
    HoughLinesP( src, p_lines, 1, CV_PI/180, 85, 40, 10 );  
vector<Vec4i> filt_lines;
        int fls=filt_lines.size();
        int pls=p_lines.size();
if (!p_lines.empty()){
    for(size_t i = 0; i < p_lines.size();i++)
    {
        Vec4i l = p_lines[i];
        if ((l[0]<l[2]-10 ||  l[0]>l[2]+10) &&  (l[1]<l[3]-10 || l[1]>l[3]+10))
        {
            line( result/*probabilistic_hough*/, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255,0,0), 2);
            filt_lines.push_back(l);

        }

    }


    fls=filt_lines.size();
    pls=p_lines.size();
    printf("f=%d p=%d",fls,pls);
    if (filt_lines.size()>1 && !filt_lines.empty()) 
    {
        int sizeX=src.rows/11, sizeY=src.cols/11;
        int imgGrid[11][11];

        for (int i=0;i<11;i++)
        {
            for (int j=0;j<11;j++) imgGrid[i][j]=0;
        }


        for (size_t i = 0;i<filt_lines.size()-1;i++)
        {           
            Point2f o1 (filt_lines[i][0], filt_lines[i][1]) ;
            Point2f p1 (filt_lines[i][2], filt_lines[i][3]) ; 
            for (size_t j=i+1;j<filt_lines.size();j++)
            {
                Point2f o2 (filt_lines[j][0], filt_lines[j][1]) ;
                Point2f p2 (filt_lines[j][2], filt_lines[j][3]) ;
                Point2f x = o2 - o1;
                Point2f d1 = p1 - o1;
                Point2f d2 = p2 - o2;
                float cross = d1.x*d2.y - d1.y*d2.x;
                if (abs(cross) > 1e-8)
                {
                    double t1 = (x.x * d2.y - x.y * d2.x)/cross;
                    Point2f crosP  = o1 + d1 * t1;
                    if (crosP.x>0.0 && crosP.y>0.0 && crosP.y< src.rows && crosP.x<src.cols)
                    imgGrid[int(crosP.x/sizeX)][int(crosP.y/sizeY)]++;
                }
            }
        }
        int countP=0,maxi=0, maxj=0;
        for (int i=0;i<11;i++)
        {
            for (int j=0;j<11;j++)
            {

            if (countP<imgGrid[i][j])
                {
                    countP=imgGrid[i][j];
                    maxi=i;
                    maxj=j;
                }
            }
        }
        printf("c=%d \n",countP);
        if (countP>0)
        {
            Point vanishP1 = cvPoint(maxi*sizeX,maxj*sizeY), vanishP2 = cvPoint((maxi+1)*sizeX,(maxj+1)*sizeY);
            CvScalar red= CV_RGB(220,0,0);
            if (vanishP1.x>=0 && vanishP1.y>=0 && vanishP2.x>0 && vanishP2.y>0)             
            rectangle(src,vanishP1,vanishP2,red,2,8,0);     
            result=src;
        } else {printf("only 1 intersection, out of img"); }
    } else 
    {
    printf("No filtered lines");
    }


} else 
    {       
    printf("no lines on image");
    }

}

我厌倦了用 gdb 调试,无效的指针地址是p_lines向量的起始地址。gdb 框架指向函数的 las }。如何解决 free(): invalid pointer 错误以及导致分段错误的问题。来自 gdb 的错误消息:

    glibc detected *** /home/xyz: free(): invalid pointer: 0x080cc482 ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x12fe591]
    /lib/tls/i686/cmov/libc.so.6(+0x6cde8)[0x12ffde8]
    /lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x1302ecd]
    /usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x11fa741]
    /home/xyz(_Z19Probabilistic_HoughN2cv3MatERS0_+0x8c5)[0x804b3b5]
    /home/xyz(_Z13imageCallbackRKN5boost10shared_ptrIKN11sensor_msgs6Image_ISaIvEEEEE+0x5c8)[0x804b9a0][0x804b3b5]
/home/xyz(_Z13imageCallbackRKN5boost10shared_ptrIKN11sensor_msgs6Image_ISaIvEEEEE+0x5c8)[0x804b9a0]

p_lines 的打印是(_M_start = 0x80cc482 等于 free(): ip :0x080cc482):

(gdb) p p_lines
$1 = {<std::_Vector_base<cv::Vec<int, 4>, std::allocator<cv::Vec<int, 4> > >> = {
    _M_impl = {<std::allocator<cv::Vec<int, 4> >> = {<__gnu_cxx::new_allocator<cv::Vec<int, 4> >> = {<No data fields>}, <No data fields>}, _M_start = 0x80cc482, _M_finish = 0x80cc540, 
      _M_end_of_storage = 0x80cc540}}, <No data fields>}

在纠正我的错字后我重新做了一些测试并if (countP>0)在最后添加了一个。找到出现无效指针操作的帧,img有12个p_lines和2个filt_lines,但是countP=0(所以两条线相交超出了img大小)。gdb 显示,在调用_M_start = 0x80d4e70HoughLinesP()起始地址后,崩溃后出现在0x080d4e71和我的函数 gdb 框架中 p_lines 的起始地址为_M_start = 0x80d4e71。什么鬼?向量的起始地址怎么可能随1位而变化?p_linesfree(): i.p.

最后我找到了发生位变化的确切时刻p_lines

(gdb) next
    98                      imgGrid[int(crosP.x/sizeX)][int(crosP.y/sizeY)]++;
    (gdb) p p_lines 
    $68 = {<std::_Vector_base<cv::Vec<int, 4>, std::allocator<cv::Vec<int, 4> > >> = {
        _M_impl = {<std::allocator<cv::Vec<int, 4> >> = {<__gnu_cxx::new_allocator<cv::Vec<int, 4> >> = {<No data fields>}, <No data fields>}, 
          _M_start = 0x80d4c78, _M_finish = 0x80d4d38, _M_end_of_storage = 0x80d4d38}}, <No data fields>}
    (gdb) next
    84              for (size_t j=i+1;j<filt_lines.size();j++)
    (gdb) p p_lines 
    $69 = {<std::_Vector_base<cv::Vec<int, 4>, std::allocator<cv::Vec<int, 4> > >> = {
        _M_impl = {<std::allocator<cv::Vec<int, 4> >> = {<__gnu_cxx::new_allocator<cv::Vec<int, 4> >> = {<No data fields>}, <No data fields>}, 
          _M_start = 0x80d4c79, _M_finish = 0x80d4d38, _M_end_of_storage = 0x80d4d38}}, <No data fields>}
(gdb) p j
$71 = 1

所以在这种情况下,大小filt_lines为 2。并且在检查第一行和第二行之后出现位移for,并且当 j=1 和 j<2 时跳回。为什么?:/请帮助我,解决这个错误。谢谢

4

1 回答 1

1

filt_lines您在最内层循环中的向量上有索引溢出。我想应该是

 for (size_t j = i + 1; j < filt_lines.size(); j++)

代替

 for (size_t j=i+1;j<p_lines.size();j++)
于 2012-12-13T00:47:34.503 回答