红色与时间的关系图:OpenCV Android

时间:2017-09-21 21:35:07

标签: android opencv android-graphview

我在我的Android应用程序中使用OpenCV,它检测摄像机前面对象的RGB值。我可以成功检测到RGB值(不完美,因为我的手机摄像头不断调整亮度),我想绘制红色/蓝色/绿色值与时间的关系图。我还想将视频限制为1分钟。我正在使用GraphView库来绘制图形。问题是图表视图需要2个参数,x(时间)和y(我的R / G / B值),因为我找不到设置计时器的解决方案,我无法绘制图形。 任何人都可以帮我理解如何设置定时器,以便将视频录制到一段时间吗?

public class VideoRecordingActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2{

//java camera view
JavaCameraView javaCameraView;

Mat mRgba, mHsv;

GraphView graph;

//callback loader
BaseLoaderCallback mCallBackLoader = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {


        switch (status){
            case BaseLoaderCallback.SUCCESS:
                javaCameraView.enableView();
                break;
            default:
                super.onManagerConnected(status);
                break;
        }
    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video_recording);

    //connect the camera
    javaCameraView = (JavaCameraView)findViewById(R.id.java_camera_view);

    //set visibility
    javaCameraView.setVisibility(SurfaceView.VISIBLE);
    javaCameraView.setMaxFrameSize(320, 240);

    javaCameraView.enableFpsMeter();
    javaCameraView.clearFocus();

    //set callback function
    javaCameraView.setCvCameraViewListener(this);

    /*Graph*/
    graph = (GraphView)findViewById(R.id.graphView);


}

@Override
protected void onPause() {
    super.onPause();

    if(javaCameraView!=null){
        javaCameraView.disableView();
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (javaCameraView!=null){
        javaCameraView.disableView();
    }
}

@Override
protected void onResume() {
    super.onResume();

    if (OpenCVLoader.initDebug()){
        Log.d("openCV", "Connected");

        //display when the activity resumed,, callback loader
        mCallBackLoader.onManagerConnected(LoaderCallbackInterface.SUCCESS);


    }else{
        Log.d("openCV", "Not connected");
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_3_0, this, mCallBackLoader);
    }
}

@Override
public void onCameraViewStarted(int width, int height) {
    //4 channel
    mRgba = new Mat(width, height, CvType.CV_8UC4);
    mHsv = new Mat(width, height, CvType.CV_8UC3);

}

@Override
public void onCameraViewStopped() {
    //release
    mRgba.release();
}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    //get each frame from camera
    mRgba = inputFrame.rgba();


    /**********HSV conversion**************/
    //convert mat rgb to mat hsv
    Imgproc.cvtColor(mRgba, mHsv, Imgproc.COLOR_RGB2HSV);

    //find scalar sum of hsv
    Scalar mColorHsv = Core.sumElems(mHsv);

    int pointCount = 320*240;


    //convert each pixel
    for (int i = 0; i < mColorHsv.val.length; i++) {
        mColorHsv.val[i] /= pointCount;
    }

    //convert hsv scalar to rgb scalar
    Scalar mColorRgb = convertScalarHsv2Rgba(mColorHsv);

    //print scalar value
    Log.d("intensity", "R:"+ String.valueOf(mColorRgb.val[0])+" G:"+String.valueOf(mColorRgb.val[1])+" B:"+String.valueOf(mColorRgb.val[2]));

    int R = (int) mColorRgb.val[0];
    int G = (int) mColorRgb.val[1];
    int B = (int) mColorRgb.val[2];

    Log.d("intensity", "Y:"+ String.valueOf(Y)+" U:"+String.valueOf(U)+" V:"+String.valueOf(V));


    /*>>>>>>>>GRAPH<<<<<<<<<<*/

    LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[]{
            new DataPoint(0, R),
            new DataPoint(1, R),
            new DataPoint(2, R),
            new DataPoint(3, R)
    });
    graph.addSeries(series);




    return mRgba;

}



//convert Mat hsv to scalar
private Scalar convertScalarHsv2Rgba(Scalar hsvColor) {
    Mat pointMatRgba = new Mat();
    Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3, hsvColor);
    Imgproc.cvtColor(pointMatHsv, pointMatRgba, Imgproc.COLOR_HSV2RGB);

    return new Scalar(pointMatRgba.get(0, 0));
 }
}

视频大约是15-20 fps。我想绘制每帧的R / G / B值与时间的关系。 任何帮助将非常感谢。

0 个答案:

没有答案