使用OpenCV for Android应用程序分别显示红色蓝色和绿色值

问题描述 投票:0回答:1

我正在尝试创建一个应用程序,我将我的相机指向某个东西,该应用程序将显示该对象的红色蓝色和绿色。我正在使用OpenCV库在android studio中开发应用程序。我已成功完成所有设置,可以转换直播视频的色彩空间。但是,我无法分别显示R,G和B值。说实话,我不知道该怎么做。我已经完成谷歌搜索,并在C ++中得到了一些帮助,但是没有找到很多帮助java。

我的活动文件就是这个

package com.example.virus.bloodpressurereader;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.SurfaceView;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.JavaCameraView;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.dnn.Importer;
import org.opencv.imgproc.Imgproc;

public class VideoRecordingActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2{

//custom toolbar
private Toolbar toolbar;

//java camera view
JavaCameraView javaCameraView;

Mat mRgba, rgb_scale;


//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;
        }
    }
};


public static String TAG="VideoRecordingActivity";


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

    //custom toolbar
    toolbar = (Toolbar)findViewById(R.id.nav_action);
    setSupportActionBar(toolbar);


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

    //set visibility
    javaCameraView.setVisibility(SurfaceView.VISIBLE);
    //set callback function
    javaCameraView.setCvCameraViewListener(this);


}

@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(TAG, "Connected");

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


    }else{
        Log.d(TAG, "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(height, width, CvType.CV_8UC4);
    //rgb_scale = new Mat(height, width, CvType.CV_8UC1);
}

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

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

   //Imgproc.cvtColor(mRgba, rgb_scale, Imgproc.COLOR_RGB2HSV);

    return mRgba;
 }
}

注意:我没有显示红色蓝色和绿色的文本视图,因为我希望只记录值。

java android image-processing video-processing opencv3.3
1个回答
0
投票
 public class VideoRecordingActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2{

//custom toolbar
private Toolbar toolbar;

//java camera view
JavaCameraView javaCameraView;

Mat mRgba, mHsv;

//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);

    //custom toolbar
    toolbar = (Toolbar)findViewById(R.id.nav_action);
    setSupportActionBar(toolbar);

    //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);


}

@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);

    /*Log.d("intensity", "Color: #" + String.format("%02X", (int)mColorHsv.val[0])
            + String.format("%02X", (int)mColorHsv.val[1])
            + String.format("%02X", (int)mColorHsv.val[2]) );*/
    //print scalar value
    //Log.d("intensity", "R:"+ String.valueOf(mColorRgb.val[0])+" G:"+String.valueOf(mColorRgb.val[1])+" B:"+String.valueOf(mColorRgb.val[2]));


    /*Convert to YUV*/

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

    int Y = (int) (R *  .299000 + G *  .587000 + B *  .114000);
    int U = (int) (R * -.168736 + G * -.331264 + B *  .500000 + 128);
    int V = (int) (R *  .500000 + G * -.418688 + B * -.081312 + 128);

    //int I = (R+G+B)/3;


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

    /*calibration*/


    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));
 }
}
© www.soinside.com 2019 - 2024. All rights reserved.