2

My question is how to convert the bitmap image which I got into onActivityResult into mat object so that I do some image processing using native part.I want to do the image Processing on my mat image.Thanks in advance

 private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    //System.loadLibrary("nativegray");
                    Log.i(TAG, "OpenCV loaded successfully");
                    //mOpenCvCameraView.enableView();
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };


protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     imgFavorite = (ImageView)findViewById(R.id.imageView1);
     imgMat=new Mat();
     /*imgFavorite.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
            open();
         }
      });*/
}
public void open(){
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, 0);
 }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   // TODO Auto-generated method stub
   super.onActivityResult(requestCode, resultCode, data);
   Bitmap bp = (Bitmap) data.getExtras().get("data");

   //my code here
   imgFavorite.setImageBitmap(bp);
}
@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoaderCallback);
    imgFavorite.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
           open();
        }
     });
}









@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Mirza
  • 97
  • 1
  • 13
  • Got the answer http://stackoverflow.com/questions/17390289/convert-bitmap-to-mat-after-capture-image-using-android-camera but you need to implement it in onActivityResult method – Mirza Dec 22 '14 at 07:01

2 Answers2

1

The secret to convert any data type to a Mat is to understand the requirements of the following constructor:

Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP)

The Java interface might not offer the exact same constructor, but I'm sure the fundamentals still apply:

  • rows: the height of the image;
  • cols: the width of the image;
  • type: the internal structure of the data. For instance, CV_8UC3 means: a 3-channel image of unsigned char;
  • data: a pointer to a 1-D array with the pixels. The data type of the array has to be same as the one informed on type.

  // pseudocode
  int width = getWidthOfBitmap();
  int height = getHeightOfBitmap();
  unsigned char* pixels = getDataOfBitmap();
  Mat image(height, width, CV_8UC1, pixels);

In this case, CV_8UC1 indicates a single channel image (i.e. grayscale).

You are going to have to investigate which methods of Bitmap return these info.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • They have done through a function look at below link.thanks http://docs.opencv.org/java/org/opencv/android/Utils.html#bitmapToMat(Bitmap – Mirza Dec 21 '14 at 10:59
  • They have done through a function http://docs.opencv.org/java/org/opencv/android/Utils.html#bitmapToMat(Bitmap – Mirza Dec 22 '14 at 05:06
  • how to convert the on touch coordinate into opencv coordinate.I have gone through this [link] (http://stackoverflow.com/questions/27279860/how-to-convert-android-touch-coordinates-to-opencv-image-cordinates) but unable to get the required result. I want to get the ROI of the image and do some processing – Mirza Jan 19 '15 at 09:55
  • Its the same math operation, just update those numbers with the ones you have. – karlphillip Jan 19 '15 at 14:59
  • thank you karlphillip but I did both way. first I converted the touch coordinate by multiplying it with it getwidth/getheight and again in opencv we multiplied the coordinate by outImg.rows / bitmap_row.where bitmap_row is the actual number of rows when image is drawn.Any way I am getting the result.thankyou – Mirza Jan 20 '15 at 05:27
0

A Mat is a multi dimension array, bitmaps are 2 dimensional arrays.

You can get the raw data out of a Bitmap with copyPixelsToBuffer: http://developer.android.com/reference/android/graphics/Bitmap.html#copyPixelsToBuffer(java.nio.Buffer)

You can then use http://developer.android.com/reference/java/nio/ByteBuffer.html#array() to get the byte[].

Hopefully you should be able to use this to create your Mat.

deive
  • 862
  • 7
  • 19
  • see this link they have done through a function on util package in android http://docs.opencv.org/java/org/opencv/android/Utils.html#bitmapToMat(Bitmap, org.opencv.core.Mat, boolean) – Mirza Dec 19 '14 at 05:21
  • That looks like the right way of doing it! (I only know a very small amount of OpenCV) You should probably put that as the answer and mark it as correct (for others with the same issue)! – deive Dec 19 '14 at 10:23