OpenCV:free():调整矩阵大小时无效的下一个大小错误

时间:2014-08-30 15:56:28

标签: opencv memory

我需要获得一些积分'坐标(由变量xCoord和yCoord表示)并将矩阵 scan_image 中的关联位置设置为1。可能会发生点的坐标位于矩阵的维度之外,因此在将矩阵中的点相关位置设置为1之前,需要调整 scan_image 的大小。
这是代码:

    #include "line_matching/listener.hpp"

    #include <opencv2/core.hpp>
    #include <opencv2/features2d/features2d.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/core/utility.hpp>

    using namespace cv;
    using namespace std;

    int main(int argv, char** argc){
        /* start listening */
        listener lsr;
        lsr.listen(argv, argc);

        /* get data that were listened to */
        vector<listener::completeInformation> vecCI = lsr.getListeningInfo();

        /* create a matrix to host image */
        Mat scan_image;

        /* fill matrix */
        float currentX = 0;
        float currentY = 0;
        for(int i = 0; i<(int)vecCI.size(); i++)
        {
            /* get current info */
            listener::completeInformation ci = vecCI[i];
            float xCoord = ci.position.first;
            float yCoord = ci.position.second;

            /* matrix is void */
            if(scan_image.empty())
            {
                cout << "case 1" << endl;
                /* create a new row */
                Mat firstRow = Mat::zeros(yCoord+1, xCoord+1, CV_32FC1);
                scan_image.push_back(firstRow);

                /* set element to 1 */
                scan_image.at<float>(xCoord, yCoord) = 1;

                /* update matrix size */
                currentX = xCoord;
                currentY = yCoord;
            }

            /* new coordinate is outside current matrix */
            else if (xCoord > currentX && yCoord > currentY)
            {
                cout << "case 2" << endl;
                /* add necessary rows */
                Mat newRows = Mat::zeros(yCoord-currentY, currentX+1, CV_32FC1);
                cout << "Size scan " << scan_image.rows << " " << scan_image.cols << endl;
                scan_image.push_back(newRows);
                currentY = yCoord;

                /* add new cols */
                Mat newCols = Mat::zeros(currentY+1, xCoord-currentX, CV_32FC1);
                cout << "Size newCols " << newCols.rows << " " << newCols.cols << endl;
                hconcat(scan_image, newCols, scan_image);

                /* update matrix size */
                currentX = xCoord;

                /* set element to 1 */
                scan_image.at<float>(xCoord, yCoord) = 1;
            }

            /* coordinate's column is outside matrix */
            else if(xCoord > currentX && yCoord < currentY)
            {
                cout << "case 3" << endl;
                /* add necessary cols */
                Mat newCols = Mat::zeros(currentY+1, xCoord-currentX, CV_32FC1);
                cout << "Size scan " << scan_image.rows << " " << scan_image.cols << endl;
                cout << "Size newCols " << newCols.rows << " " << newCols.cols << endl;
                Mat newMat;
                hconcat(scan_image, newCols, newMat);
                cout << "Size newMat " << newMat.rows << " " << newMat.cols << endl;

                //scan_image.resize(newMat.rows);
                //scan_image.create(newMat.rows, newMat.cols, CV_32FC1);
                resize(scan_image, scan_image, newMat.size());
                cout << "Size scan dopo" << scan_image.rows << " " << scan_image.cols << endl;
                //scan_image = newMat.clone();
                cout << "create" << endl;
                newMat.copyTo(scan_image);
                //newMat.assignTo(scan_image, -1);
                cout << "Dopo creazione mat " << scan_image.rows << " " << scan_image.cols << endl;


                /* update matrix size */
                currentX = xCoord;

                /* set element to 1 */
                scan_image.at<float>(xCoord, yCoord) = 1;
            }

            /* coordinate's row is outside matrix */
            else if(xCoord < currentX && yCoord > currentY)
            {
                cout << "case 4" << endl;
                /* add necessary rows */
                Mat newRows = Mat::zeros(yCoord-currentY, currentX+1, CV_32FC1);
                scan_image.push_back(newRows);

                /* update matrix size */
                currentY = yCoord;

                /* set element to 1 */
                scan_image.at<float>(xCoord, yCoord) = 1;
            }

            else
            {
                cout << "case 5" << endl;
                /* set element to 1 */
                scan_image.at<float>(xCoord, yCoord) = 1;
            }
        }

        imshow("Scan", scan_image);
        waitKey();


    }

我尝试了很多选项来修改 scan_info 的维度(你会在评论中找到它们),如果表示为CASE 3,但每次出现错误的内存错误:

*** glibc detected *** /home/ubisum/fuerte_workspace/line_matching/bin/matching_main: free(): invalid next size (fast): 0x0000000001e81060 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7e846)[0x7feb0e3fe846]
/usr/local/lib/libopencv_core.so.3.0(_ZN2cv8fastFreeEPv+0x9d)[0x7feb0fb24986]
/usr/local/lib/libopencv_core.so.3.0(+0x21cbb0)[0x7feb0fb26bb0]
/usr/local/lib/libopencv_core.so.3.0(_ZNK2cv12MatAllocator5unmapEPNS_8UMatDataE+0x43)[0x7feb0fb25eaf]
/usr/local/lib/libopencv_core.so.3.0(_ZN2cv3Mat10deallocateEv+0x71)[0x7feb0fb27c3b]
/home/ubisum/fuerte_workspace/line_matching/bin/matching_main(_ZN2cv3MatD1Ev+0x75)[0x40de15]
/usr/local/lib/libopencv_imgproc.so.3.0(_ZN2cv6resizeERKNS_11_InputArrayERKNS_12_OutputArrayENS_5Size_IiEEddi+0x16da)[0x7feb0efd80dc]
/home/ubisum/fuerte_workspace/line_matching/bin/matching_main(main+0x1b19)[0x40d389]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7feb0e3a176d]
/home/ubisum/fuerte_workspace/line_matching/bin/matching_main[0x40dce1]
======= Memory map: ========
00400000-0041f000 r-xp 00000000 08:06 1318193                            /home/ubisum/fuerte_workspace/line_matching/bin/matching_main
0061e000-0061f000 r--p 0001e000 08:06 1318193                            /home/ubisum/fuerte_workspace/line_matching/bin/matching_main
0061f000-00620000 rw-p 0001f000 08:06 1318193                            /home/ubisum/fuerte_workspace/line_matching/bin/matching_main
01e43000-030ec000 rw-p 00000000 00:00 0                                  [heap]
7feae470a000-7feae490a000 rw-p 00000000 00:00 0 
7feae490a000-7feae4919000 r-xp 00000000 08:06 2772700                    /usr/lib/libtbbmalloc.so.2
7feae4919000-7feae4b19000 ---p 0000f000 08:06 2772700                    /usr/lib/libtbbmalloc.so.2
7feae4b19000-7feae4b1a000 r--p 0000f000 08:06 2772700                    /usr/lib/libtbbmalloc.so.2
7feae4b1a000-7feae4b1b000 rw-p 00010000 08:06 2772700                    /usr/lib/libtbbmalloc.so.2
7feae4b1b000-7feae4b2c000 rw-p 00000000 00:00 0 
7feae4b2c000-7feae4b6c000 rw-s 00024000 00:05 13777                      /dev/ati/card0
7feae4b6c000-7feae526c000 rw-s 00006000 00:05 13777                      /dev/ati/card0
7feae526c000-7feae53ec000 rw-p 00000000 00:00 0 
7feae53ec000-7feae5486000 r-xp 00000000 08:06 2766513                    /usr/lib/fglrx/libGL.so.1.2
7feae5486000-7feae5586000 ---p 0009a000 08:06 2766513                    /usr/lib/fglrx/libGL.so.1.2
7feae5586000-7feae55ae000 rwxp 0009a000 08:06 2766513                    /usr/lib/fglrx/libGL.so.1.2
7feae55ae000-7feae55cb000 rwxp 00000000 00:00 0 
7feae55cb000-7feae7a13000 r-xp 00000000 08:06 2766506                    /usr/lib/fglrx/libamdocl64.so
7feae7a13000-7feae7c13000 ---p 02448000 08:06 2766506                    /usr/lib/fglrx/libamdocl64.so
7feae7c13000-7feae7ee0000 rw-p 02448000 08:06 2766506                    /usr/lib/fglrx/libamdocl64.so
7feae7ee0000-7feae8000000 rw-p 00000000 00:00 0 
7feae8000000-7feae8021000 rw-p 00000000 00:00 0 
7feae8021000-7feaec000000 ---p 00000000 00:00 0 
7feaec000000-7feaec021000 rw-p 00000000 00:00 0 
7feaec021000-7feaf0000000 ---p 00000000 00:00 0 
7feaf0000000-7feaf0021000 rw-p 00000000 00:00 0 
7feaf0021000-7feaf4000000 ---p 00000000 00:00 0 
7feaf4000000-7feaf4021000 rw-p 00000000 00:00 0 
7feaf4021000-7feaf8000000 ---p 00000000 00:00 0 
7feaf8000000-7feaf8021000 rw-p 00000000 00:00 0 
7feaf8021000-7feafc000000 ---p 00000000 00:00 0 
7feafc011000-7feafc0d1000 rw-p 00000000 00:00 0 
7feafc0d1000-7feafc171000 r-xp 00000000 08:06 2766543                    /usr/lib/fglrx/libatiadlxx.so
7feafc171000-7feafc271000 ---p 000a0000 08:06 2766543                    /usr/lib/fglrx/libatiadlxx.so
7feafc271000-7feafc274000 rw-p 000a0000 08:06 2766543                    /usr/lib/fglrx/libatiadlxx.so
7feafc274000-7feafc284000 rw-p 00000000 00:00 0 
7feafc284000-7feafc28a000 r-xp 00000000 08:06 2766549                    /usr/lib/fglrx/libOpenCL.so.1
7feafc28a000-7feafc489000 ---p 00006000 08:06 2766549                    /usr/lib/fglrx/libOpenCL.so.1
7feafc489000-7feafc48a000 rw-p 00005000 08:06 2766549                    /usr/lib/fglrx/libOpenCL.so.1
7feafc48a000-7feafc48b000 ---p 00000000 00:00 0 
7feafc48b000-7feafcc8b000 rw-p 00000000 00:00 0                          [stack:13933]
7feafcc8b000-7feafcc8c000 ---p 00000000 00:00 0 
7feafcc8c000-7feafd48c000 rw-p 00000000 00:00 0 
7feafd48c000-7feafd48d000 ---p 00000000 00:00 0 
7feafd48d000-7feafdc8d000 rw-p 00000000 00:00 0 
7feafdc8d000-7feafdc8e000 ---p 00000000 00:00 0 
7feafdc8e000-7feafe48e000 rw-p 00000000 00:00 0 
7feafe48e000-7feafe48f000 ---p 00000000 00:00 0 
7feafe48f000-7feafec8f000 rw-p 00000000 00:00 0 
7feafec8f000-7feafec9b000 r-xp 00000000 08:06 1970270                    /lib/x86_64-linux-gnu/libnss_files-2.15.so
7feafec9b000-7feafee9a000 ---p 0000c000 08:06 1970270                    /lib/x86_64-linux-gnu/libnss_files-2.15.so
7feafee9a000-7feafee9b000 r--p 0000b000 08:06 1970270                    /lib/x86_64-linux-gnu/libnss_files-2.15.so
7feafee9b000-7feafee9c000 rw-p 0000c000 08:06 1970270                    /lib/x86_64-linux-gnu/libnss_files-2.15.so
7feafee9c000-7feaff57f000 r--p 00000000 08:06 2759450                    /usr/lib/locale/locale-archive
7feaff57f000-7feaff585000 r-xp 00000000 08:06 2760664                    /usr/lib/x86_64-linux-gnu/libogg.so.0.7.1
7feaff585000-7feaff784000 ---p 00006000 08:06 2760664                    /usr/lib/x86_64-linux-gnu/libogg.so.0.7.1
7feaff784000-7feaff785000 r--p 00005000 08:06 2760664                    /usr/lib/x86_64-linux-gnu/libogg.so.0.7.1
7feaff785000-7feaff786000 rw-p 00006000 08:06 2760664                    /usr/lib/x86_64-linux-gnu/libogg.so.0.7.1
7feaff786000-7feaff7a7000 r-xp 00000000 08:06 2760810                    /usr/lib/x86_64-linux-gnu/libv4lconvert.so.0
7feaff7a7000-7feaff9a6000 ---p 00021000 08:06 2760810                    /usr/lib/x86_64-linux-gnu/libv4lconvert.so.0
7feaff9a6000-7feaff9a8000 r--p 00020000 08:06 2760810                    /usr/lib/x86_64-linux-gnu/libv4lconvert.so.0
7feaff9a8000-7feaff9a9000 rw-p 00022000 08:06 2760810                    /usr/lib/x86_64-linux-gnu/libv4lconvert.so.0
7feaff9a9000-7feaff9fb000 rw-p 00000000 00:00 0 
7feaff9fb000-7feaffa0b000 r-xp 00000000 08:06 2760531                    /usr/lib/x86_64-linux-gnu/libgstinterfaces-0.10.so.0.25.0
7feaffa0b000-7feaffc0b000 ---p 00010000 08:06 2760531                    /usr/lib/x86_64-linux-gnu/libgstinterfaces-0.10.so.0.25.0
7feaffc0b000-7feaffc0c000 r--p 00010000 08:06 2760531                    /usr/lib/x86_64-linux-gnu/libgstinterfaces-0.10.so.0.25.0Aborted (core dumped)

有什么建议吗? 感谢

0 个答案:

没有答案