title: Segmentation fault when calling a function which contains an image as parameter opencv / c++
body: <p>I'm implementing a method that decompose the images and I have a segmentation fault error ,I'm going to start by showing you the debugger output :</p>
  
  <pre><code>#0  0x0804c2cb in cv::Mat::Mat (this=0xbffff110, m=...)
        at /usr/local/include/opencv2/core/mat.hpp:117
  #1  0x0804ad14 in decompose (input=...) at main.cpp:241
  #2  0x0804bbd7 in main (argc=2, argv=0xbffff6c4) at main.cpp:322
  </code></pre>
  
  <p>Here's a portion of my code :</p>
  
  <pre><code>#define MAX_ITERATIONS 15
  //////////////////
  /////////////////
  float sum(cv::Mat img, int startedY, int startedX, int w) {
      float res = 0.0f;
  for (int j = startedY - ((w - 1) / 2) ; j &lt; startedY + ((w + 1) / 2); j++)
  for (int i = startedX - ((w - 1) / 2); i &lt; startedX + ((w + 1) / 2); i++) {
          {
    if ((i &gt;= 0 &amp;&amp; i &lt; img.size().width) &amp;&amp; (j &gt;= 0  &amp;&amp; j &lt; img.size().height)) {  res += img.at&lt;float&gt;(j,i);
              }}}   return res; }
  
   cv::Mat decompose(cv::Mat input)
  {
   /////////////
   cv::Mat inputImg(input)
   ///////////////////
   cv::Mat imgSource(inputImg);
   cv::Mat imgMax(inputImg);
   cv::Mat newImgMax(imgMax.size().height,imgMax.size().width,CV_32F);
   ////////////////////////////////
   ////////////////////////////////////
  for (int l = 0; l &lt; imgSource.size().width; l++){
   for (int k = 0; k &lt; imgSource.size().height; k++){
     if( (k &gt;= 0 &amp;&amp; k &lt; imgSource.size().height) &amp;&amp; (l &gt;= 0  &amp;&amp; l &lt;    
            imgSource.size().width) ) {
    newImgMax.at&lt;float&gt;(k,l) = (float)sum(imgMax,k, l, wmax) / (wmax * wmax);***LIGNE 241*********
   }}}
   ////////////////////
  /////////////////////
  
  ****************************************************************** 
  cv::Mat imgMoyenne(inputImg);
  
  for (int i = 0; i &lt; inputImg.size().height; i++) {
      for (int j = 0; j &lt; inputImg.size().width; j++) {
         imgMoyenne.at&lt;float&gt;(i, j) = (newImgMin.at&lt;float&gt;(i, j) +   
            newImgMax.at&lt;float&gt;(i, j)) /2;
      }
  }
  Mat diff_im=inputImg - imgMoyenne; 
  
  return diff_im; //the end of the decomposition function
    }
  
   *************************************************************
  int main(int argc, char **argv)
  {
      char modeTitle[30]= {0}, residueTitle[50];
      double variance = 1000000;
      if (argc != 2) {
          std::cout &lt;&lt; "Usage: ./emd &lt;image&gt;" &lt;&lt; std::endl;
          return 1;
      }
   cv::Mat inputImg;
   cv::Mat imgMode;
  
   inputImg=imread(argv[1]); 
   if(! inputImg.data )                             
      {cout &lt;&lt;  "Could not open or find the image" &lt;&lt; std::endl ;
          return -1; }
  
    namedWindow("Source Image",WINDOW_AUTOSIZE);
    imshow("Source Image",inputImg);
    cv::waitKey(1000);
      for (int i = 1; i &lt; MAX_ITERATIONS + 1; i++) {
          sprintf(modeTitle, "BEMC-%d", i);
          std::cout &lt;&lt; "Decomposing " &lt;&lt; modeTitle &lt;&lt; std::endl;
          imgMode = decompose(inputImg);**************LIGNE 322****************
         //////////////
         /////////////
       }
  </code></pre>
  
  <p>here's a portion of /usr/local/include/opencv2/core/mat.hpp :</p>
  
  <pre><code>inline Mat::Mat(const Mat&amp; m)
  : flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), data(m.data),
  refcount(m.refcount), datastart(m.datastart), dataend(m.dataend),
  datalimit(m.datalimit), allocator(m.allocator), size(&amp;rows)
  {
  if( refcount )
      CV_XADD(refcount, 1);**************** LIGNE 117******************
  if( m.dims &lt;= 2 )
  {
      step[0] = m.step[0]; step[1] = m.step[1];
  }
  else
  {
      dims = 0;
      copySize(m);
  }
  }
  </code></pre>
  
  <p>Any ideas how I could fix this issue?</p>