如何将mat转换为位图和位图到mat?

时间:2017-01-03 15:34:56

标签: android opencv bitmap

将mat转换为位图并将位图转换为mat。我得到了黑色场景。可能是什么原因,有人帮助我

 package com.example.alper.myapplication;

            import android.graphics.Bitmap;
            import android.graphics.BitmapFactory;
            import android.os.Environment;
            import android.support.v7.app.AppCompatActivity;
            import android.os.Bundle;
            import android.util.Log;
            import android.view.View;
            import android.widget.Button;
            import android.widget.ImageView;
            import android.widget.Toast;

            import org.opencv.android.Utils;
            import org.opencv.core.CvException;
            import org.opencv.core.CvType;
            import org.opencv.core.Mat;
            import org.opencv.core.Scalar;
            import org.opencv.imgproc.Imgproc;

            import static org.opencv.imgproc.Imgproc.cvtColor;

    public class Main2Activity extends AppCompatActivity {
        ImageView iv;
        Bitmap bmpInput, bmpOutput;
        Mat matInput , matOutput;
        Button bt;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            iv = (ImageView) findViewById(R.id.imageView2);
            bt= (Button) findViewById(R.id.button2);

            String photoPath = Environment.getExternalStorageDirectory()+"/frames/frame.png";
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            bmpInput = BitmapFactory.decodeFile(photoPath,options);

            iv.setImageBitmap(bmpInput);
            matInput = convertBitMap2Mat(bmpInput);
            matOutput = new Mat(matInput.rows(), matInput.cols(), CvType.CV_8UC3);

            bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    bmpOutput = converMat2Bitmat(matOutput);
                    iv.setImageBitmap(bmpOutput);
                }
            });



        }
        Bitmap converMat2Bitmat (Mat img) {
            int width = img.width();
            int hight = img.height();


            Bitmap bmp;
            bmp = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888);
            Mat tmp;
            tmp = img.channels()==1? new Mat(width, hight, CvType.CV_8UC1, new Scalar(1)): new Mat(width, hight, CvType.CV_8UC3, new Scalar(3));
            try {
                if (img.channels()==3)
                    cvtColor(img, tmp, Imgproc.COLOR_RGB2BGRA);
                else if (img.channels()==1)
                    cvtColor(img, tmp, Imgproc.COLOR_GRAY2RGBA);
                Utils.matToBitmap(tmp, bmp);
            }
            catch (CvException e){
                Log.d("Expection",e.getMessage());
            }
            return bmp;
        }


        Mat convertBitMap2Mat (Bitmap rgbaImage){
            Mat rgbaMat = new Mat(rgbaImage.getHeight(), rgbaImage.getWidth(),CvType.CV_8UC4);
            Bitmap bmp32 = rgbaImage.copy(Bitmap.Config.ARGB_8888, true);
            Utils.bitmapToMat(bmp32, rgbaMat);

            Mat rgbMat = new Mat(rgbaImage.getHeight(), rgbaImage.getWidth(), CvType.CV_8UC3);
            cvtColor(rgbaMat,rgbMat,Imgproc.COLOR_RGBA2BGR, 3);
            return rgbMat;
        }

我有一个位图图片。我希望从位图转换为垫,而不是ı应用一些图像处理再次从席子转换为位图,而不是在图像视图显示但是有黑色场景。你认为可能是什么原因?帮我。

2 个答案:

答案 0 :(得分:1)

Utils 类中,opencv中有可用的功能。尝试使用these

答案 1 :(得分:0)

您的代码没问题。错误在您的图像处理代码中。 如果您尝试将bmpInput转换为mat然后将其重新转换为位图,则它将与您的代码一起使用。