Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

convert Mat to Bitmap Opencv for Android

I'm trying to covert a MAt to a Bitmap use following code :

    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());}

my seedsImage is a Mat object. And the Exception and got is 10-09 22:15:09.418: D/Exception(2461): ....modulesjavageneratorsrccpputils.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) I tried to search through but no solution worked for me. Can anynone help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

1) OpenCV Mat constructor expects <rows, cols> pair instead of <width, height> as its arguments. So you have to change your second line to

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

2) Imgproc.cvtColor can change the dimensions of the tmp object. So it is safe to create a bitmap after the color conversion:

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());}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...