问题
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;
}
}
回答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.
回答2:
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.
来源:https://stackoverflow.com/questions/27547763/how-to-convert-the-bitmap-image-to-mat-image