传递mat引用时opencv malloc错误

时间:2013-11-18 16:52:07

标签: c++ objective-c opencv

我目前正在使用obj-c / c ++将matlab代码移植到opencv,我遇到了malloc问题。传递Mat时一切正常,但如果我从imageChannel传递一个频道:

cv::Mat *imageChannel = new cv::Mat[c];
cv::split(image, imageChannel); This is my code:

它抛出了几个malloc错误:

OpenCvTest(15537,0xb0081000) malloc: *** error for object 0x11915000: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

这是我的代码:

+(cv::Mat)boxFilterWithImage:(cv::Mat)image r:(int)r
{
    int hei = image.rows;
    int wid = image.cols;
    int c = image.channels();
    cv::Mat imDst = cv::Mat::zeros(hei, wid, CV_32FC(c));
    cv::Mat imCum = [self cumSumWith:image dim:1];

    imCum.colRange(r, 2*r).copyTo(imDst.colRange(0, r));

    cv::Mat calc = imCum.colRange((2*r+2)-1, wid-1) - imCum.colRange(0, (wid-2*r-1)-1); 
    calc.copyTo(imDst.colRange(r+1, (wid-r)-1));

    cv::repeat(imCum.col(wid-1), 1, r, calc);
    calc -= imCum.colRange((wid-2*r)-1, wid-r-1);
    calc.copyTo(imDst.colRange((wid-r+1)-2, wid-1));

    imCum = [self cumSumWith:imDst dim:2];
    imCum.rowRange(r, 2*r).copyTo(imDst.rowRange(0, r));
    calc = imCum.rowRange((2*r+2)-1, hei-1) - imCum.rowRange(0, (hei-2*r-1)-1);
    calc.copyTo(imDst.rowRange(r+1, hei-r-1));
    cv::repeat(imCum.row(hei-1), r, 1, calc);
    calc -= imCum.rowRange((hei-2*r)-2, (hei-r-1)-1);
    calc.copyTo(imDst.rowRange(hei-r-1, hei-1));

    return imDst;
}

那么有人对此有所了解吗?

编辑: 所以我有时间调试它:

似乎有问题
imCum.rowRange(r, 2*r).copyTo(imDst.rowRange(0, r));

第一个子矩阵在提取后被释放。

此致 卡斯滕

1 个答案:

答案 0 :(得分:1)

我通常以这种方式处理频道:

vector<Mat> src_arr;
cv::split(src,src_arr);
Mat tmp;
    // You can do the loop here 
MyFuncForSingleChannel(src_arr[0]);
tmp.copyTo(src_arr[0]);
MyFuncForSingleChannel(src_arr[1]);
tmp.copyTo(src_arr[1]);
MyFuncForSingleChannel(src_arr[2]);
tmp.copyTo(src_arr[2]);

cv::merge(src_arr,dst);