title: OpenCV Error: Assertion failed in matrix.cpp line 522, /matrix.cpp:522: error: (-215)
body: <p>I am trying to create an ROI above the face detected to place a hat like shown in the image: Plz click here : <a href="https://i.stack.imgur.com/pievb.jpg" rel="nofollow noreferrer">ROI created above face to place a hat</a></p>
  
  <p>I have made sure that the ROI created is withing the bounds of the image. It looks like this: 
   // Create an ROI
   // where face is the detected face ROI</p>
  
  <pre><code>    if (0&lt;=face.x &amp;&amp; 0&lt;=face.x-face.width*0.08&lt;=image.cols &amp;&amp; 0&lt;=face.x+face.width+face.width*0.08&lt;=image.cols 
           &amp;&amp; 0&lt;=face.y &amp;&amp; 0&lt;=face.y-face.height*0.28&lt;=image.rows)
      {
        Mat ROI_hat = image(Rect(abs(face.x-face.width*0.08),abs(face.y-face.height*0.28),abs(face.x+face.width+face.width*0.08),abs(face.y)));
        rectangle(image,Point(abs(face.x-face.width*0.08),abs(face.y-face.height*0.28)),Point(abs(face.x+face.width+face.width*0.08),abs(face.y)),Scalar(255, 0, 0), 1, 4);
  
        cout&lt;&lt;"Within the bounds of Image"&lt;&lt;endl;
      }
      else{
       cout&lt;&lt;" Out of bounds of Image "&lt;&lt;endl;
          }
  </code></pre>
  
  <p>There are no negative values and for every frame it says ROI is withing the bounds. But I still get assertion error:</p>
  
  <blockquote>
    <p>OpenCV Error: Assertion failed (0 &lt;= roi.x &amp;&amp; 0 &lt;= roi.width &amp;&amp; roi.x + roi.width &lt;= m.cols &amp;&amp; 0 &lt;= roi.y &amp;&amp; 0 &lt;= roi.height &amp;&amp; roi.y + roi.height &lt;= m.rows) in Mat, file /home/user/OpenCV_Installed/opencv-3.2.0/modules/core/src/ma‌​trix.cpp, line 522 terminate called after throwing an instance of 'cv::Exception' what(): /home/user/OpenCV_Installed/opencv-3.2.0/modules/core/src/ma‌​trix.cpp:522: error: (-215) 0 &lt;= roi.x &amp;&amp; 0 &lt;= roi.width &amp;&amp; roi.x + roi.width &lt;= m.cols &amp;&amp; 0 &lt;= roi.y &amp;&amp; 0 &lt;= roi.height &amp;&amp; roi.y + roi.height &lt;= m.rows in function Mat Aborted (core dumped)</p>
  </blockquote>
  
  <p>Can someone please tell me where I'm going wrong?</p>