title: OpenCV Error: Assertion Failed in MixChannels(..)
body: <p>I'm attempting to convert a MATLAB .mat file to openCV MAT and then applying several masks to those files.  I am building from <a href="https://github.com/hbristow/cvmatio" rel="nofollow">cvmatio source code</a>.  I am receiving the following error:</p>
  
  <blockquote>
    <p>OpenCV Error: Assertion failed (A.size == arrays[i0]->size) in init,
    file
    /home/derek/Documents/Libraries/opencv-3.0.0-beta/modules/core/src/matrix.cpp,
    line 4279 terminate called after throwing an instance of
    'cv::Exception'   what(): 
    /home/derek/Documents/Libraries/opencv-3.0.0-beta/modules/core/src/matrix.cpp:4279:
    error: (-215) A.size == arrays[i0]->size in function init</p>
  </blockquote>
  
  <p>Here is the source file I've written.  It occurs at the line with MixChannels.  Note that SrcImage is a 3 channel Mat.  lower and upper are the threshold values in an array who's length is equal to the number of channels.</p>
  
  <pre><code>/*
   * Mask.cpp
   *
   *  Created on: Mar 16, 2015
   *      Author: derek
   */
  
  
  #include &lt;cv.h&gt;
  #include &lt;highgui.h&gt;
  #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&lt; SrcImage.channels(); i=i+1){
          int from_to[]={i,1};
          mixChannels(&amp;SrcImage, 3, &amp;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;
  }
  </code></pre>
  
  <p>Also, here is an excerpt of the actual CV_Assertion error location.  As indicated in the error, it occurs at "(A.size == arrays[i0]->size)".</p>
  
  <pre><code>void NAryMatIterator::init(const Mat** _arrays, Mat* _planes, uchar** _ptrs, int _narrays)
  {
      CV_Assert( _arrays &amp;&amp; (_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 &lt; 0 )
      {
          for( i = 0; _arrays[i] != 0; i++ )
              ;
          narrays = i;
          CV_Assert(narrays &lt;= 1000);
      }
  
      iterdepth = 0;
  
      for( i = 0; i &lt; narrays; i++ )
      {
          CV_Assert(arrays[i] != 0);
          const Mat&amp; A = *arrays[i];
          if( ptrs )
              ptrs[i] = A.data;
  
          if( !A.data )
              continue;
  
          if( i0 &lt; 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 &lt; d; d1++ )
                  if( A.size[d1] &gt; 1 )
                      break;
          }
          else
              CV_Assert( A.size == arrays[i0]-&gt;size );
  
          if( !A.isContinuous() )
          {
              CV_Assert( A.step[d-1] == A.elemSize() );
              for( j = d-1; j &gt; d1; j-- )
                  if( A.step[j]*A.size[j] &lt; A.step[j-1] )
                      break;
              iterdepth = std::max(iterdepth, j);
          }
      }
  
      if( i0 &gt;= 0 )
      {
          size = arrays[i0]-&gt;size[d-1];
          for( j = d-1; j &gt; iterdepth; j-- )
          {
              int64 total1 = (int64)size*arrays[i0]-&gt;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 &gt;= 0; j-- )
              nplanes *= arrays[i0]-&gt;size[j];
      }
      else
          iterdepth = 0;
  
      idx = 0;
  
      if( !planes )
          return;
  
      for( i = 0; i &lt; narrays; i++ )
      {
          CV_Assert(arrays[i] != 0);
          const Mat&amp; A = *arrays[i];
  
          if( !A.data )
          {
              planes[i] = Mat();
              continue;
          }
  
          planes[i] = Mat(1, (int)size, A.type(), A.data);
      }
  }
  </code></pre>