转换NV21在Android中使用OpenCV的为RGB [英] Converting NV21 to RGB using OpenCV in Android

查看:4416
本文介绍了转换NV21在Android中使用OpenCV的为RGB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用OpenCV的Andr​​oid中。所以,我首先测试了OpenCV的通过具有两个SurfaceViews放在并排侧。一个SurfaceView用于preVIEW输出(输出格式显然是NV21)从相机。其他SurfaceView显示经过的OpenCV后的同一preVIEW如见下code:

I am trying to use OpenCV in Android. So I first tested out OpenCV by having two SurfaceViews placed side-by-side. One SurfaceView is used to preview output (the output format is clearly NV21) from the camera. The other SurfaceView shows the same preview after passing through OpenCV as shown in the code below:

public void onPreviewFrame(byte[] data, Camera camera) {
    // TODO Auto-generated method stub

    if( mYuv != null ) mYuv.release();
    mYuv = new Mat( height + height/2, width, CvType.CV_8UC1 );
    mYuv.put( 0, 0, data);
    Mat mRgba = new Mat();

    Imgproc.cvtColor( mYuv, mRgba, Imgproc.COLOR_YUV2RGB_NV21, 4 );

    Bitmap map = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888 );

    Utils.matToBitmap( mRgba, map );

    preview.setBackgroundDrawable( new BitmapDrawable( map ));
    mRgba.release();

}

但通过OpenCV的后生成的图像是绿色的,staticy ...的事情:

But the resulting image after passing through OpenCV is an green, staticy... thing:

任何想法?

编辑:

修改code有点按注释。

Modified code a bit as per comment.

public void onPreviewFrame(byte[] data, Camera camera) {
    // TODO Auto-generated method stub

    if( mYuv != null ) mYuv.release();
    mYuv = new Mat( height + height/2, width, CvType.CV_8UC1 );
    mYuv.put( 0, 0, data );
    Mat mRgba = new Mat();

    Imgproc.cvtColor( mYuv, mRgba, Imgproc.COLOR_YUV2RGBA_NV21, 4 );

    Bitmap map = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888 );

    Utils.matToBitmap( mRgba, map );

    preview.setBackgroundDrawable( new BitmapDrawable( where.getResources(), map ));
    mRgba.release();

}

这会导致这样的:

Which results in this:

推荐答案

好吧,我想通了,我哪里失控。

Alright, I figured out where I went haywire.

我最初做了这样的事情:

I initially did something like this:

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {

    this.width = width; this.height = height;
    params.setPreviewSize( width, height );
    camera.setParameters( params );
    camera.startPreview();

}

现在的问题是,在Android上的相机只支持特定的preVIEW决议。因此,具体的解决我设置没有工作。所以,我改成了这样:

The problem is, the cameras on android only supports specific preview resolutions. Therefore the specific resolution I was setting did not work. So, I changed it to this:

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {

    Size size = params.getPreviewSize();
    this.height = size.height;
    this.width = size.width;
    camera.setParameters( params );
    camera.startPreview();

}

然后一切正常OK!老实说,这不是我的预期误差为,所以这不是一个很好形成问题。

And then everything works A-OK! Honestly, this was not where I expected the error to be, so this was not a well formed question.

这篇关于转换NV21在Android中使用OpenCV的为RGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆