OpenCV错误:断言在MixChannels(.)中失败

OpenCV Error: Assertion Failed in MixChannels(..)

本文关键字:失败 MixChannels 错误 断言 OpenCV      更新时间:2023-10-16

我正试图将MATLAB.mat文件转换为openCV mat,然后对这些文件应用几个掩码。我是从cvmatio源代码构建的。我收到以下错误:

OpenCV错误:初始化中断言失败(A.size==arrays[i0]->size),文件/home/derek/Documents/Libraries/opencv-3.0.0-beta/modules/core/src/matrix.cpp,第4279行在抛出的实例后终止调用'cv::Exception'what():/home/derek/Documents/Libraries/opencv-3.0.0-beta/modules/core/src/matrics.cpp:4279:错误:(-215)A.size==数组[i0]->函数初始化中的大小

这是我写的源文件。它出现在MixChannels的线路上。请注意,SrcImage是一个3通道的Mat。lower和upper是数组中长度等于通道数的阈值。

/*
 * Mask.cpp
 *
 *  Created on: Mar 16, 2015
 *      Author: derek
 */

#include <cv.h>
#include <highgui.h>
#include "imgcodecs.hpp"
#include "highgui.hpp"
#include "imgproc.hpp"
using namespace cv;

Mat Mask(Mat SrcImage, double lower[], double upper[]){
    int height=SrcImage.rows;
    int width=SrcImage.cols;
    int depth=SrcImage.depth();
    Mat B2d = Mat::ones(height, width,depth);
    Mat out(height, width, depth);
    Mat outL(height, width, depth);
    Mat outU(height,width, depth);
    for (int i=1; i< SrcImage.channels(); i=i+1){
        int from_to[]={i,1};
        mixChannels(&SrcImage, 3, &out, 1, from_to, 1 );
        threshold(out, outL, lower[i], 1, THRESH_BINARY);
        threshold(out, outU, upper[i], 1, THRESH_BINARY);
        bitwise_and(B2d, outL, B2d);
        bitwise_and(B2d, outU, B2d);
    }
    return B2d;
}

此外,以下是实际CV_Assertion错误位置的摘录。如错误所示,它发生在"(A.size==arrays[i0]->size)"。

void NAryMatIterator::init(const Mat** _arrays, Mat* _planes, uchar** _ptrs, int _narrays)
{
    CV_Assert( _arrays && (_ptrs || _planes) );
    int i, j, d1=0, i0 = -1, d = -1;
    arrays = _arrays;
    ptrs = _ptrs;
    planes = _planes;
    narrays = _narrays;
    nplanes = 0;
    size = 0;
    if( narrays < 0 )
    {
        for( i = 0; _arrays[i] != 0; i++ )
            ;
        narrays = i;
        CV_Assert(narrays <= 1000);
    }
    iterdepth = 0;
    for( i = 0; i < narrays; i++ )
    {
        CV_Assert(arrays[i] != 0);
        const Mat& A = *arrays[i];
        if( ptrs )
            ptrs[i] = A.data;
        if( !A.data )
            continue;
        if( i0 < 0 )
        {
            i0 = i;
            d = A.dims;
            // find the first dimensionality which is different from 1;
            // in any of the arrays the first "d1" step do not affect the continuity
            for( d1 = 0; d1 < d; d1++ )
                if( A.size[d1] > 1 )
                    break;
        }
        else
            CV_Assert( A.size == arrays[i0]->size );
        if( !A.isContinuous() )
        {
            CV_Assert( A.step[d-1] == A.elemSize() );
            for( j = d-1; j > d1; j-- )
                if( A.step[j]*A.size[j] < A.step[j-1] )
                    break;
            iterdepth = std::max(iterdepth, j);
        }
    }
    if( i0 >= 0 )
    {
        size = arrays[i0]->size[d-1];
        for( j = d-1; j > iterdepth; j-- )
        {
            int64 total1 = (int64)size*arrays[i0]->size[j-1];
            if( total1 != (int)total1 )
                break;
            size = (int)total1;
        }
        iterdepth = j;
        if( iterdepth == d1 )
            iterdepth = 0;
        nplanes = 1;
        for( j = iterdepth-1; j >= 0; j-- )
            nplanes *= arrays[i0]->size[j];
    }
    else
        iterdepth = 0;
    idx = 0;
    if( !planes )
        return;
    for( i = 0; i < narrays; i++ )
    {
        CV_Assert(arrays[i] != 0);
        const Mat& A = *arrays[i];
        if( !A.data )
        {
            planes[i] = Mat();
            continue;
        }
        planes[i] = Mat(1, (int)size, A.type(), A.data);
    }
}

答案显然为时已晚,但以下是代码失败的原因:

由于你的SrcImage是一个有多个通道的单垫子,你应该写:

mixChannels(&SrcImage, 1, &out, 1, from_to, 1 );

(断言错误与此有关,因为mixChannels需要3个Mat,这是Mat的3倍大。)

此外,opencv MixChannels标记了0中的频道,不确定i=1是有意的,还是只是打字错误。

干杯!