15

我正在尝试将 MAt 转换为位图,使用以下代码:

    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Mat tmp = new Mat (width,height,CvType.CV_8UC1,new Scalar(4));
    try {
    //Imgproc.cvtColor(seedsImage, tmp, Imgproc.COLOR_RGB2BGRA);
    Imgproc.cvtColor(seedsImage, tmp, Imgproc.COLOR_GRAY2RGBA, 4);
    Utils.matToBitmap(tmp, bmp);}
    catch (CvException e){Log.d("Exception",e.getMessage());}

我的 seedImage 是一个 Mat 对象。例外是10-09 22:15:09.418: D/Exception(2461): ..\..\modules\java\generator\src\cpp\utils.cpp:105: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, _jclass*, jlong, _jobject*, jboolean) 我试图搜索但没有解决方案对我有用。任何人都可以帮忙吗?

4

2 回答 2

24

1) OpenCV Mat 构造函数期望<rows, cols>对而不是<width, height>作为它的参数。所以你必须改变你的第二行

Mat tmp = new Mat (height, width, CvType.CV_8U, new Scalar(4));

2)Imgproc.cvtColor可以改变tmp物体的尺寸。所以在颜色转换后创建位图是安全的:

Bitmap bmp = null;
Mat tmp = new Mat (height, width, CvType.CV_8U, new Scalar(4));
try {
    //Imgproc.cvtColor(seedsImage, tmp, Imgproc.COLOR_RGB2BGRA);
    Imgproc.cvtColor(seedsImage, tmp, Imgproc.COLOR_GRAY2RGBA, 4);
    bmp = Bitmap.createBitmap(tmp.cols(), tmp.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(tmp, bmp);
}
catch (CvException e){Log.d("Exception",e.getMessage());}
于 2012-10-30T09:18:31.030 回答
4

尝试使用此代码进行 mat 到位图的转换

**Mat mRgba;
public void onCameraViewStarted(int width, int height) {
        mRgba = new Mat(height, width, CvType.CV_8UC4);
}
public Mat onCameraFrame(Mat inputFrame) {
   inputFrame.copyTo(mRgba);
   return mRgba;
}
private void captureBitmap(){
       bitmap = Bitmap.createBitmap(mOpenCvCameraView.getWidth()/4,mOpenCvCameraView.getHeight()/4, Bitmap.Config.ARGB_8888);
    try {
          bitmap = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
                Utils.matToBitmap(mRgba, bitmap);
               mBitmap.setImageBitmap(bitmap);
               mBitmap.invalidate();
    }catch(Exception ex){
          System.out.println(ex.getMessage());
    }
 }**
于 2014-07-03T12:18:44.133 回答