简体   繁体   English

如何在Android的OpenCV中获取像素的灰度值

[英]How to get grayscale value of a pixel in OpenCV for Android

I want to get the gray scale value of a pixel in OpenCV for Android. 我想在Android的OpenCV中获取像素的灰度值。 The problem is that OpenCV for Android is very much undocumented and a lot of the methods are different. 问题在于,Android的OpenCV尚未公开,许多方法有所不同。 I have found answers to this problem, but none of them work for android. 我已经找到了解决此问题的方法,但是它们都不适合android。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.hardware.camera2.CameraCaptureSession;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.JavaCameraView;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;

public class testActivity extends AppCompatActivity implements CvCameraViewListener2 {

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
public void onManagerConnected(int status){
        switch (status){
            case LoaderCallbackInterface.SUCCESS:
            {
                mOpenCvCameraView.enableView();
                break;
            }
            default:
            {
                super.onManagerConnected(status);
            }
        }
    }

};
private JavaCameraView mOpenCvCameraView;


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

    mOpenCvCameraView = (JavaCameraView) findViewById(R.id.testVideoView);
    mOpenCvCameraView.setMaxFrameSize(240,320);
    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
    mOpenCvCameraView.setCvCameraViewListener(this);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    //getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    getSupportActionBar().hide();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}



public void onDestroy() {
    super.onDestroy();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

public void onResume(){
    super.onResume();
    if (!OpenCVLoader.initDebug()) {
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoaderCallback);
    } else {
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }
}



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

    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}



@Override
public void onCameraViewStarted(int width, int height) {

}

@Override
public void onCameraViewStopped() {

}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    return inputFrame.gray();
}
}

I'll assume you mean "how to extract gray pixel from RGB Mat", because getting the gray pixel from Mat that represents a grayscale image is trivial. 我假设您的意思是“如何从RGB Mat中提取灰度像素”,因为从Mat中获取代表灰度图像的灰度像素是微不足道的。

What you can do is define submat of size 1x1 for instance 您可以做的是例如定义大小为1x1的子垫

Mat bgrPixel= rgbMat.submat(x,y,x,y).clone();
Mat grayPixel = new Mat(1,1, CvType.CV_8UC1);
Imgproc.cvtColor(bgrPixel, grayPixel, Imgproc.COLOR_BGR2GRAY);

(maybe the indexes are x,y,x+1,y+1, I'm not sure) after that you can use your gray pixel. (也许索引是x,y,x + 1,y + 1,我不确定),之后您就可以使用灰色像素了。

It's not an elegant solution but it should work. 这不是一个优雅的解决方案,但它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM