title: C++/OpenCV Can't initialize 3D Mat
body: <p>I have a problem with initializing a 3D Mat with openCV. 
  I would like to create a 3D matrix of size (rows x cols x 16), rows and cols being the dimensions of an image given earlier in the program. I tried I can not say how many different methods, and all return to me more or less the same thing: the dimensions of my matrices are worth 0 or -858993460. </p>
  
  <p>My code lines : </p>
  
  <pre><code>Mat image_Conv;
  
  int rows = imageBicubic.rows;
  int cols = imageBicubic.cols;
  
  image_Conv = Mat::zeros(rows, cols, CV_32FC(16));
  </code></pre>
  
  <p>Can you tell me why I have this problem? Of course I read all the posts that speak, read the doc opencv on the class Mat, but nothing works, I still have the same problem. I specify that my data in the Mat will be float.</p>
  
  <p>The code : </p>
  
  <pre><code>// Include standard headers
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;vector&gt;
  #include &lt;ctime&gt;
  
  #include &lt;iostream&gt;
  using namespace std;
  
  //#include &lt;opencv.hpp&gt;
  #include &lt;opencv/cv.h&gt;
  #include &lt;opencv2/core/core.hpp&gt;
  #include &lt;opencv2/imgproc/imgproc.hpp&gt;
  #include &lt;opencv2/imgcodecs.hpp&gt;
  #include &lt;opencv/highgui.h&gt;
  using namespace cv;
  
  ////////////////////////////////////////
  // main file
  int main() 
  {
      string fileName = "myImage.jpg";
  
      Mat imageSrc = cv::imread(fileName, CV_LOAD_IMAGE_UNCHANGED);   // Read the file
      if (!imageSrc.data) // Check for invalid input
      {
          cout &lt;&lt; "Could not open or find the image\n";
          return 1;
      }
      cout &lt;&lt; "Loaded " &lt;&lt; fileName &lt;&lt; " (" &lt;&lt; imageSrc.channels() &lt;&lt; " channels)\n";
  
      //int colorTransform = (imageSrc.channels() == 4) ? CV_BGRA2RGBA : (imageSrc.channels() == 3) ? CV_BGR2RGB : CV_GRAY2RGB;
      //cv::cvtColor(imageSrc, imageSrc, colorTransform);
  
      imageSrc.convertTo(imageSrc, CV_32F, 1 / 255.0, 0.0);
  
      int SliceSizeWidth = imageSrc.cols / 2;
      int sliceShiftWidth = imageSrc.cols / 4;
      int sliceWidthNumber = (imageSrc.cols / sliceShiftWidth) - 1;
  
      int SliceSizeHeight = imageSrc.rows / 2;
      int sliceShiftHeight = imageSrc.rows / 4;
      int sliceHeightNumber = (imageSrc.rows / sliceShiftHeight) - 1;
  
      for (int sliceIndexHeight = 0; sliceIndexHeight &lt; sliceHeightNumber; sliceIndexHeight++)
      {
          for (int sliceIndexWidth = 0; sliceIndexWidth &lt; sliceWidthNumber; sliceIndexWidth++)
          {
              Mat patchImage = imageSrc(Rect(sliceIndexWidth*sliceShiftWidth, sliceIndexHeight*sliceShiftHeight, SliceSizeWidth, SliceSizeHeight));
  
              Mat patchImageCopy;
              patchImage.copyTo(patchImageCopy); // Deep copy =&gt; data are contiguous in patchImageCopy
  
              Mat imageBicubic;
  
              resize(patchImageCopy, imageBicubic, Size(2 * patchImage.cols, 2 * patchImage.rows), INTER_CUBIC);
  
  
              Mat image_Padding;
              int padding = 1;
  
              copyMakeBorder(imageBicubic, image_Padding, padding, padding, padding, padding, BORDER_CONSTANT, Scalar(0));
  
  
              Mat image_Conv;
  
              int rows = imageBicubic.rows;
              int cols = imageBicubic.cols;
  
              image_Conv = Mat::zeros(rows, cols, CV_32FC(16));
  
              /* rest of the code I have to write */
  
  
              image_Conv.convertTo(image_Conv, CV_8U, 255.0, 0.0);
  
              string nameBase = fileName.substr(0, fileName.find('.'));
              string nameExt = fileName.substr(fileName.find('.'), fileName.length() - nameBase.length());
  
              string strH = to_string(sliceIndexHeight);
              string strW = to_string(sliceIndexWidth);
  
              string outFileName = nameBase + "_H" + strH + "W" + strW + nameExt;
  
              imwrite(outFileName, image_Conv);
  
          }
      }
      return 0;
  }
  </code></pre>
  
  <p>PS : Most of the code is not mine, I have to use it for my internship and can only edit between the lines :</p>
  
  <pre><code>resize(patchImageCopy, imageBicubic, Size(2 * patchImage.cols, 2 * patchImage.rows), INTER_CUBIC);
  </code></pre>
  
  <p>and </p>
  
  <pre><code>image_Conv.convertTo(image_Conv, CV_8U, 255.0, 0.0);
  </code></pre>
  
  <p>Thank you for your help !</p>
  
  <p><strong>EDIT :</strong> My first problem is solved, but it seems that it didn't work after all. I suppose that Mat::zeros set all the Mat elements at 0, right ? But if I write </p>
  
  <pre><code>cout &lt;&lt; image_Conv.at&lt;float&gt;(0,0,0) &lt;&lt; endl; 
  </code></pre>
  
  <p>I have the error : "Unhandled exception at 0x000007FEFD4FA06D in xxxxxx.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000000023E540.".</p>
  
  <p>I don't know what the problem is with the memory and how to fix it. </p>
  
  <p>My goal is to fill my matrix element by element thanks to several for loops which will be realized several operations, before the result is written in the element of my corresponding Mat. I did that why 3D and 4D arrays, and maybe it's the easiest solution, to do all the calculs with arrays, but I can't go from a 3D array to a 3D Mat or a 3D Mat to a 3D array. </p>