本文介绍了适用于Android的OpenCV:使用Imgproc.cvtColor将相机预览从YUV转换为RGB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果尝试转换摄像机预览YUV字节数组,则会收到运行时错误到具有Imgproc.cvtColor(mYUV_Mat,mRgba_Mat,Imgproc.COLOR_YUV420sp2RGBA,4)的RGB(A)字节数组在onPreviewFrame(byte [] data,Camera camera)中:

I get a runtime error if I try to convert camera preview YUV byte arrayto a RGB(A) byte array with Imgproc.cvtColor( mYUV_Mat, mRgba_Mat, Imgproc.COLOR_YUV420sp2RGBA, 4 )in onPreviewFrame(byte[] data, Camera camera):

Preview.java:

Preview.java:

     mCamera.setPreviewCallback(new PreviewCallback() {
          public void onPreviewFrame(byte[] data, Camera camera)
          {
            // Pass YUV data to draw-on-top companion
            System.arraycopy(data, 0, mDrawOnTop.mYUVData, 0, data.length);
            mDrawOnTop.invalidate();
          }
       });

DrawOnTop.java:

DrawOnTop.java:

public class DrawOnTop extends View {
Bitmap mBitmap;
Mat mYUV_Mat;
protected void onDraw(Canvas canvas) {
    if (mBitmap != null)
    {

        canvasWidth = canvas.getWidth();
        canvasHeight = canvas.getHeight();
        int newImageWidth = 640;
        int newImageHeight = 480;
        marginWidth = (canvasWidth - newImageWidth)/2;

        if( mYUV_Mat != null ) mYUV_Mat.release();

        //mYUV_Mat = new Mat( newImageWidth, newImageHeight, CvType.CV_8UC1 );
        mYUV_Mat = new Mat( newImageWidth, newImageHeight, CvType.CV_8UC4 );
        mYUV_Mat.put( 0, 0, mYUVData );

        //Mat mRgba_Mat = new Mat();
        Mat mRgba_Mat = new Mat(newImageWidth,newImageHeight,CvType.CV_8UC4);

        //Mat mRgba_Mat = mYUV_Mat;

        //Imgproc.cvtColor( mYUV_Mat, mRgba_Mat, Imgproc.COLOR_YUV2RGBA_NV21, 4 );
        //Imgproc.cvtColor( mYUV_Mat, mRgba_Mat, Imgproc.COLOR_YUV420sp2RGB, 4 );
        Imgproc.cvtColor( mYUV_Mat, mRgba_Mat, Imgproc.COLOR_YUV420sp2RGBA, 4 );


        // Draw Bitmap New:
        Bitmap mBitmap = Bitmap.createBitmap( newImageWidth, newImageHeight, Bitmap.Config.ARGB_8888 );
        Utils.matToBitmap( mRgba_Mat, mBitmap );
        mRgba_Mat.release();
}
}

转换mYUV_Mat.put(0,0,mYUVData)正确运行.但是尝试使用Imgproc.cvtColor将mYUV_Mat转换为mRgb_Mat导致所有运行时错误(在Eclipse中为找不到源.".)

The conversion mYUV_Mat.put( 0, 0, mYUVData ) runs correctly.But the attempts to convert mYUV_Mat to mRgb_Mat using Imgproc.cvtColorlead all to runtime errors ("Source not found." with Eclipse).

对于我的目标而言,正确的Imgproc.cvtColor命令是什么?

What is the correct Imgproc.cvtColor command for my goal?

(我不想使用Java YUV2RGB(A)解码方法,因为它会变慢用于图像处理.我想使用OpenCV Imgproc.cvtColor方法因为我可以打本地电话)

(I don't want to use a Java YUV2RGB(A) decode method because it's to slowfor image processing. I want to use the OpenCV Imgproc.cvtColor methodbecause I can do native calls)

推荐答案

也许Imgproc库未正确包含在您的项目中,但是其他OpenCV库是吗?崩溃的行是使用Imgproc中的方法的第一行,这将解释为什么早期代码正确运行.

Maybe the Imgproc library isn't properly included in your project, but other OpenCV libraries are? The line that crashes is the first line where you use a method from Imgproc, which would explain why earlier parts of the code run correctly.

您的代码看起来不错,除了您可以对mRgba_Mat使用无参数构造函数(因为大多数OpenCV4Android函数(包括cvtColor都可以推断出目标矩阵的所需大小)),并且您分配了很多mYUV_Mat的浪费空间.如果您只分配YUV矩阵比RGB对应矩阵多50%的空间,则不需要完整的4个通道:

Your code looks fine, except you can use the no-argument constructor for mRgba_Mat (since most OpenCV4Android functions, including cvtColor, can infer the required size of the destination matrix), and you're allocating a lot of wasted space for mYUV_Mat. You don't need a full 4 channels if you just allocate YUV matrices 50% more space than their RGB counterparts:

mYUV_Mat = new Mat( newImageHeight + newImageHeight / 2, newImageWidth, CvType.CV_8UC1 );

这篇关于适用于Android的OpenCV:使用Imgproc.cvtColor将相机预览从YUV转换为RGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 00:20