title: how to crop mat using contours in opencv for java
body: <p>I'm using the below code in order to find mat contours of an Image. I found the contours correct. But when I try to crop the image at contours the app crashes. Where am I wrong? </p>
  
  <pre><code>        List&lt;MatOfPoint&gt; contours = new ArrayList&lt;MatOfPoint&gt;();    
  
          Imgproc.findContours(imageA, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
          for(int i=0; i&lt; contours.size();i++){
              if (Imgproc.contourArea(contours.get(i)) &gt; 50 ){
                  Rect rect = Imgproc.boundingRect(contours.get(i));
                  if (rect.height &gt; 28){
                      Core.rectangle(image, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255));
                      Mat ROI = image.submat(rect.x, rect.x + rect.heigth, rect.x, rect.x + rect.width);
  
                      Highgui.imwrite("/mnt/sdcard/images/test4.png",ROI);
  
                  }
              }
          }
  </code></pre>