位图到Mat,出来的Mat是null

时间:2013-10-28 12:51:57

标签: android opencv bitmap mat

我尝试在相机视图上使用MatchTemplate,但是当我尝试将位图转换回mat时,输出mat为null,并且没有错误。

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();      


        if(Template != null){

            Imgproc.matchTemplate(mRgba, Template, mRgba, Imgproc.TM_SQDIFF);
            Core.putText(mRgba, "Matching", new Point(50, 50), BIND_AUTO_CREATE, BIND_AUTO_CREATE, Color_Green, 2);
        }   
    return mRgba;
    }  
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
            Mat tmp = new Mat (100, 100, CvType.CV_8UC3, new Scalar(4));
        Imgproc.cvtColor(mRgba, tmp, Imgproc.COLOR_RGBA2mRGBA, 0);
        bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(tmp, bmp);
        bmp = Bitmap.createBitmap(bmp, mRgba.cols()/2-50, mRgba.rows()/2-50, 100, 100, null, false);

        //show for debug            
        ImageView Img = (ImageView)findViewById(R.id.imageView1);                   
        Img.setImageBitmap(bmp);

        Bitmap matbitmap = bmp.copy(Bitmap.Config.ARGB_8888, false);
        Utils.bitmapToMat(matbitmap, Template);//the output Template is null,so that I can't do next work 

        return false;
        }

1 个答案:

答案 0 :(得分:0)

您需要初始化模板。

Mat Template= new Mat();

然后你可以使用它。另外我希望Utils.bitmapToMatTemplate = null抛出异常,但这是另一回事......