title: Convert Android Bitmap to OpenCV Mat and backwards
body: <p>I wanted to simply convert a bitmap from Android to a Mat object for OpenCV. This topic is often adressed on Stack Overflow. For example:</p>
  
  <p><a href="https://stackoverflow.com/questions/13134682/convert-mat-to-bitmap-opencv-for-android">convert Mat to Bitmap Opencv for Android</a> ;</p>
  
  <p><a href="https://stackoverflow.com/questions/17390289/convert-bitmap-to-mat-after-capture-image-using-android-camera?rq=1">convert Bitmap to Mat after capture image using android camera</a> ;</p>
  
  <p><a href="https://stackoverflow.com/questions/13900247/templatematching-mattobitmap-opencv-for-android?lq=1">templateMatching mattoBitmap opencv for android</a></p>
  
  <p>There is even more to find. I followed the instrcutions in this answers, but I'm still unable to get is done the right way.</p>
  
  <p>Minimal code:</p>
  
  <pre><code>//First convert Bitmap to Mat
  Mat ImageMat = new Mat ( image.getHeight(), image.getWidth(), CvType.CV_8U, new Scalar(4));
  Bitmap myBitmap32 = image.copy(Bitmap.Config.ARGB_8888, true);
  Utils.bitmapToMat(myBitmap32, ImageMat);
  
  //Do smth.
  Imgproc.cvtColor(ImageMat, ImageMat, Imgproc.COLOR_RGB2GRAY,4);
  
  //Then convert the processed Mat to Bitmap
  Bitmap resultBitmap = Bitmap.createBitmap(ImageMat.cols(),  ImageMat.rows(),Bitmap.Config.ARGB_8888);;
  Utils.matToBitmap(ImageMat, resultBitmap);
  
  //Set member to the Result Bitmap. This member is displayed in an ImageView
  mResult = resultBitmap;
  </code></pre>
  
  <p>(note: image is a Bitmap supplied to this lines of code)</p>
  
  <p>Errors:</p>
  
  <blockquote>
    <p>08-07 15:13:59.188: E/AndroidRuntime(2115): FATAL EXCEPTION: main</p>
    
    <p>08-07 15:13:59.188: E/AndroidRuntime(2115):
    java.lang.NoClassDefFoundError: org.opencv.core.Mat</p>
  </blockquote>
  
  <p>But my imports are: </p>
  
  <pre><code>import android.graphics.Bitmap;
  import android.graphics.Bitmap.Config;
  
  
  //OpenCV
  import org.opencv.android.Utils;
  import org.opencv.core.CvType;
  import org.opencv.core.Mat;
  import org.opencv.core.Scalar;
  import org.opencv.imgproc.Imgproc;
  </code></pre>
  
  <p>Would really appreciate any sort  of help. 
  Thanks DanS</p>