title: Unhandled Exception at Memory Location
body: <p>I am new at OpenCV and I am trying to write a simple code to get the mean of a block size in an image.  I wrote the following code, the build is ok, however, the debug is giving me an unhandled exception at memory location.  This exception is at the following line:</p>
  
  <pre><code>mean_img.at&lt;double&gt;(i/block_size, j/block_size) = mean_img.at&lt;double&gt;(i/block_size,j/block_size) + new_img.at&lt;double&gt;(i + x, j + y) / (mean);
  </code></pre>
  
  <p>So, I will be grateful if anyone give me some hints. Thanks in advance and here is the whole code:</p>
  
  <pre><code>    #include "opencv2/highgui/highgui.hpp" // Include Libs for OpenCV and Image Processing
  #include &lt;opencv2/opencv.hpp&gt; // check that
  #include "opencv2/core/core.hpp" // check that 
  #include &lt;iostream&gt; // Include Libs for C++
  #include "opencv2/imgproc/imgproc.hpp" // Include Libs for OpenCV and Image Processing
  #include &lt;math.h&gt;
  
  using namespace cv; // namespace parameters not important in OpenCV2.4.6 
  using namespace std; // namespace parameters not important in OpenCV2.4.6 
  
  int main( int argc, const char** argv )
  {
       /*This part is to compute the parameters(block size, resize parameter) of the new_img*/
       int resize_parameter; // resize parameter must be multiplication of 2
       resize_parameter = 500;
       int block_size; // block parameter must be divisable by of block size
       block_size = 50;
       if ((resize_parameter % 2) != 0)  resize_parameter = resize_parameter - (resize_parameter % 2); 
       while ((resize_parameter % block_size) != 0) block_size = block_size - 1;
       int mean_size = resize_parameter/block_size; // this is the size of the mean matrix
       int mean = block_size * block_size; // this no is ti get the mean of every element in the matrix
       //int mean_img [mean_size][mean_size] = {}; // the mean image matrix initialized by zero 
       /*This part is to allocate the array with dynamic size*/
       //int** mean_img = new int*[mean_size];
       //for(int x = 0; x &lt; mean_size; x++)
       //mean_img[x] = new int[mean_size];
       /*Then we can use the array*/
       /*This part is to fill all the elements of the mean matrix with zeros*/
       //memset(mean_img, 0, sizeof(mean_img[0][0]) * mean_size * mean_size);
  
       /*This part is the definition of the matrices that are used for the images*/
       Mat mean_img = Mat(mean_size,mean_size,CV_64FC4, cv::Scalar(0));   // define a new matrix with meansize*meansize elements to compute the mean
       Mat mean_img_full = Mat(resize_parameter,resize_parameter,CV_64FC4, cv::Scalar(0));   // define a new matrix with resizeparameter*resizeparameter elements to compute the mean
       Mat new_img = Mat(resize_parameter,resize_parameter,CV_64FC4);  // define a new matrix with resize_parameter*resize_parameter elements 
       Mat original_img = imread("Desert.JPG", CV_LOAD_IMAGE_GRAYSCALE); //define a new matrix and read the image data in the file "Desert.JPG" and store it in 'original_img'
       // notes: the location of the image must be in the same directory of the C++ file 
  
       if (original_img.empty()) //check whether the image is loaded or not
       {
            cout &lt;&lt; "Error : Image cannot be loaded..!!" &lt;&lt; endl;
            //system("pause"); //wait for a key press
            return -1;
       }
  
       // explicitly specify dsize=dst.size(); fx and fy will be computed from that.
       // resize( src matrix, dst matrix, dst.size to get the size of the dst matrix, 0, 0 "to deal with the dst matrix size, may be 0.5 or any fraction from the src size, "AREA,CUBIC,LINEAR")
       resize(original_img, new_img, new_img.size(), 0, 0, CV_INTER_AREA);
  
       /*This part is to compute the mean of each block*/
       for ( int i = 0; i &lt; resize_parameter; i = i + block_size) // i represents the index of the raw
       {
           for ( int j = 0; j  &lt; resize_parameter; j = j + block_size) // for the blocks in the same raw with different columns
           {
               for ( int x = 0; x &lt; block_size; x++) // x represents the index of the raw
               {
                   for ( int y = 0; y &lt; block_size; y++) // y represents the index of the column
                   {
                       //cout &lt;&lt; i ; //cout &lt;&lt; "\n"; //cout &lt;&lt; j ; //cout &lt;&lt; "\n"; //cout &lt;&lt; x ; //cout &lt;&lt; "\n"; //cout &lt;&lt; y ; //cout &lt;&lt; "\n";
                       mean_img.at&lt;double&gt;(i/block_size, j/block_size) = mean_img.at&lt;double&gt;(i/block_size,j/block_size) + new_img.at&lt;double&gt;(i + x, j + y) / (mean);
  
                   }
               } 
           }
       }
       /*This is the end of the part to compute the mean of each block*/
       /*This part is to fill all the resize matrix with the mean value*/
       for ( int x = 0; x &lt; resize_parameter/block_size; x++) // x represents the index of the raw in the mean matrix
       {
           for ( int y = 0; y &lt; resize_parameter/block_size; y++) // y represents the index of the column in the mean matrix
           {
               for ( int i = 0; i &lt; block_size; i++) // i represents the index of the raw in the mean_full matrix
               {
                   for ( int j = 0; j &lt; block_size; j++) // j represents the index of the column in the mean_full matrix
                   {
                       mean_img_full.at&lt;double&gt;((x*block_size)+i,(y*block_size)+j) = mean_img.at&lt;double&gt;(x,y);
                   }
               }
           }
       }
       //cout &lt;&lt; cv::getBuildInformation() &lt;&lt; endl;
  
       /*This is the end of the part to fill all the resize matrix with the mean value*/
  
       namedWindow("OriginalImage", CV_WINDOW_AUTOSIZE); //create a window with the name "OriginalImage"
       imshow("OriginalImage", original_img); //display the image which is stored in the 'original_img' in the "OriginalImage" window
       namedWindow("NewImage", CV_WINDOW_AUTOSIZE); //create a window with the name "NewImage"
       imshow("NewImage", new_img); //display the image which is stored in the 'new_img' in the "NewImage" window
       namedWindow("MeanImage", CV_WINDOW_AUTOSIZE); //create a window with the name "MeanImage"
       imshow("MeanImage", mean_img); //display the image which is stored in the 'mean_img' in the "MeanImage" window
       namedWindow("MeanFullImage", CV_WINDOW_AUTOSIZE); //create a window with the name "MeanFullImage"
       imshow("MeanFullImage", mean_img_full); //display the image which is stored in the 'mean_img_full' in the "MeanFullImage" window
  
       waitKey(0); //wait infinite time for a keypress
       destroyWindow("OriginalImage"); //destroy the window with the name, "OriginalImage"
       destroyWindow("NewImage"); //destroy the window with the name, "NewImage"
       destroyWindow("MeanImage"); //destroy the window with the name, "MeanImage"
       destroyWindow("MeanFullImage"); //destroy the window with the name, "MeanImage"
       return 0;
  }
  </code></pre>