Reputation: 1
In the App I am developing I open the Camera using OpenCV4Android
using CameraBridgeViewBase.CvCameraViewListener2
and when I touch the screen I set that frame as an image inside an ImageView
as shown below in the code.
the problem is the image set to the imageview
is always of different color than the preview on the camera as shown in the picture. I believe that this issue has something to do with the conversion I made which is stated in the code below
My question is how to convert the Mat object to a Bitmap
preserving the same color?
pic
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
Log.w(TAG, "onCameraFrame");
if (mRGBT != null) {
mRGBT.release();
}
mRGBT = inputFrame.rgba().t();
Core.flip(mRGBT, mRGBT, 1);
Imgproc.resize(mRGBT, mRGBT, inputFrame.rgba().size());
if (touched) {
touched = false;
Imgproc.cvtColor(mRGBT, mRGBT, CvType.CV_8U);
final Bitmap bitmap = Bitmap.createBitmap(mRGBT.cols(), mRGBT.rows(), Bitmap.Config.RGB_565);
Utils.matToBitmap(mRGBT, bitmap);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mIV.setImageBitmap(bitmap);
}
});
}
return mRGBT;
}
Upvotes: 2
Views: 4575
Reputation: 14390
You are converting the image incorrectly.
If you want the bitmap to be a color image, you don't need the cvtColor
.
inputFrame.rgba()
returns a RGBA Mat and that is the input you need for Utils.matToBitmap
(See JavaDoc).
if (touched) {
touched = false;
final Bitmap bitmap =
Bitmap.createBitmap(mRGBT.cols(), mRGBT.rows(), Bitmap.Config.RGB_565);
Utils.matToBitmap(mRGBT, bitmap);
runOnUiThread(new Runnable() {
@Override
public void run() {
mIV.setImageBitmap(bitmap);
}
});
}
If you want the bitmap to be a gray image use Imgproc.COLOR_BGRA2GRAY
:
if (touched) {
touched = false;
Imgproc.cvtColor(mRGBT, mRGBT, Imgproc.COLOR_BGRA2GRAY);
final Bitmap bitmap =
Bitmap.createBitmap(mRGBT.cols(), mRGBT.rows(), Bitmap.Config.RGB_565);
Utils.matToBitmap(mRGBT, bitmap);
runOnUiThread(new Runnable() {
@Override
public void run() {
mIV.setImageBitmap(bitmap);
}
});
}
If you need to work with bitmaps Bitmap.Config.ARGB_8888
add true
as a third parameter in Utils.matToBitmap
, so the Mat is converted to alpha premultiplied format (See JavaDoc).
final Bitmap bitmap =
Bitmap.createBitmap(mRGBT.cols(), mRGBT.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mRGBT, bitmap, true);
Upvotes: 4
Reputation: 936
I am using this and it works fine:
Mat mat = inputFrame.rgba();
Bitmap bm = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mat, bm);
Upvotes: 1