title: Android OpenCV MatToBitmap causes black image
body: <p>I am using OpenCV on android. I try to detect largest rectangle and extract this rectangle from image. I am using this code below. When the photo is taken, some image processing is done. My aim is, I want to detect a rectangle which is like border of paper and if its area is bigger than threshold_area, I want to crop it from original bitmap and draw on an imageview.</p>
  
  <pre><code>Mat cropped_mat;
  
  PictureCallback _jpgCallBack = new PictureCallback(){
      @Override
      public void onPictureTaken(byte[] data, Camera camera) {
  
          mCamera.startPreview();
  
          bitmap = BitmapFactory.decodeByteArray(data , 0, data .length); 
          Mat ImageMat = new Mat();
          Utils.bitmapToMat(cropped_mat, ImageMat); 
          double area = findLargestRectangle(ImageMat);
  
          if(area &gt; THRESHOLD_AREA){
  
              mCamera.stopPreview();
  
              try{
                  bitmap=Bitmap.createBitmap(cropped_mat.cols(), cropped_mat.rows(),Bitmap.Config.ARGB_8888);
                  Utils.matToBitmap(cropped_mat, bitmap);
              }
              catch(Exception e){}
  
              img.setImageBitmap(bitmap);
  
          }
          else{
              takePicture();
          }
  
      }
  };
  </code></pre>
  
  <p>This is my findLargestRectangle method.</p>
  
  <pre><code>private double findLargestRectangle(Mat imgSource) {
  
  
      Imgproc.Canny(imgSource, imgSource, 50, 50);
  
  
      Imgproc.GaussianBlur(imgSource, imgSource, new Size(5, 5), 5);
  
  
      List&lt;MatOfPoint&gt; contours = new ArrayList&lt;MatOfPoint&gt;();
      Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
  
      double maxArea = -1;
      int maxAreaIdx = -1;        
  
      MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point
      MatOfPoint2f approxCurve = new MatOfPoint2f();
      Mat largest_contour = contours.get(0);
  
      List&lt;MatOfPoint&gt; largest_contours = new ArrayList&lt;MatOfPoint&gt;();
      for (int idx = 0; idx &lt; contours.size(); idx++) {
  
          temp_contour = contours.get(idx);
          double contourarea = Imgproc.contourArea(temp_contour);
  
          //compare this contour to the previous largest contour found
          if (contourarea &gt; maxArea) {
              //check if this contour is a square
              MatOfPoint2f new_mat = new MatOfPoint2f( temp_contour.toArray() );
              int contourSize = (int)temp_contour.total();
              Imgproc.approxPolyDP(new_mat, approxCurve, contourSize*0.05, true);
  
              if (approxCurve.total() == 4) {
                  maxArea = contourarea;
                  maxAreaIdx = idx;
                  largest_contours.add(temp_contour);
                  largest_contour = temp_contour;
              }
          }
      }
  
      MatOfPoint temp_largest = largest_contours.get(largest_contours.size()-1);
      Rect rect = Imgproc.boundingRect(temp_largest);
  
      largest_contours = new ArrayList&lt;MatOfPoint&gt;();
      largest_contours.add(temp_largest);
  
      Core.rectangle(imgSource, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255));
      cropped_mat= imgSource.submat(rect.y , rect.y + rect.height, rect.x, rect.x + rect.width);
      return Imgproc.contourArea(temp_largest);
  }
  </code></pre>
  
  <p>But result is like below. Where am i wrong ?</p>
  
  <p>original frame
  <img src="https://i.stack.imgur.com/I4N7t.jpg" alt="Original frame"></p>
  
  <p>result frame
  <img src="https://i.stack.imgur.com/vkXJS.png" alt="Result frame"></p>
  
  <p>intended bitmap
  <img src="https://i.stack.imgur.com/EC38l.png" alt="Intended bitmap"></p>