title: producing "unhandled exception X at memory location Y" when checking every pixel of an grayscale image
body: <p>I am trying to solve a problem where I have to check every pixel from a 640x480 cv::Mat (Greyscale Video Image -&gt; cv::Mat MainImageSW (global)) and check if the pixel above, below, to the left and to the right is equal or below the Threshholdvalue ( GetTreshholdSW() ).</p>
  <p>My target matrix XY_ThreshholdMat is also 640x480 and set equal to Matrix255 which is initialized with&quot;255&quot;.
  If the requirements meet I set the pixel of my new Mat &quot;XY_ThreshholdMat&quot; to &quot;0&quot; in that location.
  The aim is to have every pixel which meets the req. to be black and the others white.</p>
  <p>To do so I wrote the following code:</p>
  <pre><code>void GraphicsView_PP::Fill_XY_TreshholdMat()
  {
      cv::Mat Matrix255(GetPixmapWidth(), GetPixmapHeight(), CV_8UC1, cv::Scalar(255)); 
      XY_ThreshholdMat = Matrix255;
      
      for (int y = 1; y &lt; (GetPixmapHeight()-1); y++)
      {
          for (int x = 1; x &lt; (GetPixmapWidth()-1); x++)
          {
              if ((MainImageSW.at&lt;uchar&gt;(y, x)) &lt;= GetTreshholdSW())
              {
  
                  if ((MainImageSW.at&lt;uchar&gt;((y + 1), x) &lt;= GetTreshholdSW()) &amp;&amp;  
                      (MainImageSW.at&lt;uchar&gt;((y - 1), x) &lt;= GetTreshholdSW()) &amp;&amp;
                      (MainImageSW.at&lt;uchar&gt;(y, (x + 1)) &lt;= GetTreshholdSW()) &amp;&amp; 
                      (MainImageSW.at&lt;uchar&gt;(y, (x - 1)) &lt;= GetTreshholdSW()))            
                  {
                      XY_ThreshholdMat.at&lt;uchar&gt;(y, x) = 0;  
                  }
              }
          }
      }
  
      QImage Image((uchar*)XY_ThreshholdMat.data, XY_ThreshholdMat.cols, XY_ThreshholdMat.rows, XY_ThreshholdMat.step, QImage::Format_Grayscale8);
      QPixmap Pixmap = QPixmap::fromImage(Image);
      ui.graphicsView_Bild-&gt;UpdateStream(Pixmap);
      cv::waitKey(5000); 
  }
  </code></pre>
  <p>Because I am checking the pixels above, below, to the left and to the right I start both for-loops at X/Y = 1 and let it end to (MaxWidth-1) and (MaxHeight-1).</p>
  <p>After checking every individual pixel I want to save the new Mat where every Match is a &quot;0&quot; and everything else is &quot;255&quot; to a Pixmap and display it in my custom QGraphicsView element.</p>
  <p>Once I run the code I get the typical &quot;unhandled exception X at memory location Y&quot; error but I dont know why..</p>
  <p>Also: I am rather new at programming and I know that the way the code is designed is not the most efficient way. All that matters is that it works..</p>
  <p>Anyone having an idea how to solve this problem?</p>