使用Android相机捕获图像后将Bitmap转换为Mat

Joh*_*ohn 31 android opencv bitmap mat

Mat b = new Mat();
Bitmap bmp = getIntent().getExtras().getParcelable("image_send");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_image);
    Mat tmp = new Mat (bmp.getWidth(), bmp.getHeight(), CvType.CV_8UC1);
    Utils.bitmapToMat(bmp, tmp);
    Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
    //Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_GRAY2RGB, 4);
    Utils.matToBitmap(tmp, bmp);

    iv = (ImageView) findViewById(R.id.imageView1);
    iv.setImageBitmap(bmp);
}
Run Code Online (Sandbox Code Playgroud)

无法显示bmp.我的应用程序在拍照后停止了.

小智 43

Utils.bitmapToMat需要类型ARGB_8888或的位图RGB_565.

import org.opencv.android.Utils;

Mat mat = new Mat();    
Bitmap bmp32 = bmp.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bmp32, mat);
Run Code Online (Sandbox Code Playgroud)


小智 6

Mat tmp = new Mat (bmp.getWidth(), bmp.getHeight(), CvType.CV_8UC1);

OpenCV Mat构造函数需要行,列对而不是宽度,高度作为参数,反转它们.

尝试:

Mat tmp = new Mat (bmp.getHeight(), bmp.getWidth(), CvType.CV_8UC1);