YUV_nv21 android预览帧到opencv_core.Mat RGB图像,然后将RGB垫子反转为YUV_nv21帧进行记录

时间:2018-03-14 17:43:58

标签: android opencv reverse javacv forward

我一直在寻找相对较长的时间来回答这个问题,而且,相信我,我很惊讶我还没有找到任何东西。因此,在寻找答案的过程中,我有代表我的巨大疏忽的风险,我呼吁拥有知识,时间和善意的人至少让我朝着正确的方向前进。

我承认我一般是opencv和计算机视觉的初学者,但我能够在windows / python平台上对我的概念进行证明测试,并希望在Android环境中尝试它。

使用RecordActivity.java示例作为我的基础,我想在下面的代码中

步骤1

将yuv_nv21 android手机相机预览帧(yuvImage)转换为opencv_core.Mat RGB图像(rgbImageMat),然后

第2步

opencv_core.Mat RGB图像(rgbImageMat)将其转换回yuv_nv21 android预览帧(tmpyuvImage),该帧预览了原始yuvImage帧。

第一步我使用以下转换

opencv_imgproc.cvtColor(yuvImageMat,rgbImageMat,COLOR_YUV420sp2RGB)

逻辑上我正在使用的反向步骤

opencv_imgproc.cvtColor(rgbImageMat,yuvImageMat,COLOR_RGB2YUV _ ***)

我尝试了各种可用的COLOR_RGB2YUV _ ***替代品,但出于某种原因,没有发生反向转换。我究竟做错了什么 ????有人知道这个颜色代码应该是什么????

谢谢!!

源代码如下

   @Override
    public void onPreviewFrame(byte[] data, Camera camera) {

        Log.i(LOG_TAG,"onPreviewFrame");
        if (audioRecord == null || audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
            startTime = System.currentTimeMillis();
            return;
        }
        if (RECORD_LENGTH > 0) {
            int i = imagesIndex++ % images.length;
            yuvImage = images[i];
            timestamps[i] = 1000 * (System.currentTimeMillis() - startTime);
        }

        /* get video data */
        if (yuvImage != null && recording) {
            ((ByteBuffer)yuvImage.image[0].position(0)).put(data);
            /**********************************************************************************/
    //Convert YUV Frame to RGB Mat
            Log.i(LOG_TAG,"getPreviewFormat() : "+camera.getParameters().getPreviewFormat()); //=NV21
        //Set up the Frame <=> Mat conversion
            OpenCVFrameConverter.ToMat coverter = new OpenCVFrameConverter.ToMat();
            opencv_core.Mat yuvImageMat = new opencv_core.Mat();
        //yuvImage Frame to Mat
            Frame tmpyuvImage = new Frame();
            tmpyuvImage=yuvImage.clone();
            yuvImageMat = coverter.convert(tmpyuvImage);//yuvImage Frame to Mat
        //yuvImageMat to RGB Mat
            opencv_core.Mat rgbImage = new opencv_core.Mat();
            opencv_imgproc.cvtColor(yuvImageMat,rgbImage,CV_YUV420sp2RGB);//CV_YUV420sp2RGB,COLOR_YUV2RGB_NV21=COLOR_YUV420sp2RGB,COLOR_YUV2RGB_NV21,COLOR_YCrCb2RGB
    //Do the reverse conversion
            Log.i(LOG_TAG,"#### Begin  reverse conversion ####");
            opencv_imgproc.cvtColor(rgbImage,yuvImageMat,COLOR_BGR2YUV_YV12);//What is the inverse COLOR code to the CV_YUV420sp2RGB one 

        //yuvImageMat to  Frame
            tmpyuvImage = coverter.convert(yuvImageMat);//Mat to yuvImage

            /**********************************************************************************/
            if (RECORD_LENGTH <= 0)
                try {
                        Log.v(LOG_TAG,"Writing Frame");
                        long t = 1000 * (System.currentTimeMillis() - startTime);
                        if (t > recorder.getTimestamp()) {
                            recorder.setTimestamp(t);
                        }
                        if(addFilter) {
                            filter.push(yuvImage);
                            Frame frame2;
                            while ((frame2 = filter.pull()) != null) {
                                recorder.record(frame2);
                            }
                        } else {
                            Log.i(LOG_TAG,"yuvImage.imageChannels before rec :"+Integer.toString(yuvImage.imageChannels));                               
                            //recorder.record(yuvImage);//Argument has to be of type Frame
                            recorder.record(tmpyuvImage);
                        }
                    } catch (FFmpegFrameRecorder.Exception | FrameFilter.Exception e) {
                Log.v(LOG_TAG,e.getMessage());
                e.printStackTrace();
            }
        }
    }

0 个答案:

没有答案