简体   繁体   English

带有opencv的Android-图像到灰度

[英]Android with opencv - image to grayscale

I'm looking for simple example of app in Android which convert color image to grayscale using opencv. 我正在寻找Android中简单的应用示例,该示例使用opencv将彩色图像转换为灰度图像。 I'm trying with thic code, but app crash after this line Mat tmp = new Mat(bmp.getWidth(), bmp.getHeight(), CvType.CV_8U); 我正在尝试使用thic代码,但此行后应用程序崩溃Mat tmp = new Mat(bmp.getWidth(), bmp.getHeight(), CvType.CV_8U); .

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

    ImageView img=(ImageView)findViewById(R.id.imageView1);

    Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);

    Mat tmp = new Mat(bmp.getWidth(), bmp.getHeight(), CvType.CV_8U);

    Utils.bitmapToMat(bmp, tmp);

    Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
    Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY,4);

    Utils.matToBitmap(tmp, bmp);
    img.setImageBitmap(bmp);
}

Real quick way to do it. 真正快捷的方法。 Without opencv. 没有opencv。

 private Button button;
 private ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button)findViewById(R.id.button1);
    image = (ImageView)findViewById(R.id.imageView1);


    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            ColorMatrix matrix = new ColorMatrix();
            matrix.setSaturation(0);

            ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
            image.setColorFilter(filter);


        }
    });

I just tried it, works for me. 我只是尝试过,为我工作。

edit: Check out the link here for official doc. 编辑:在此处查看官方文档链接。 http://developer.android.com/reference/android/graphics/ColorMatrix.html#setSaturation(float) http://developer.android.com/reference/android/graphics/ColorMatrix.html#setSaturation(float)

Your layout has not been laid out yet, while you are in onCreate() , that is why bmp.getWidth() and bmp.getHeight() return 0. 当您处于onCreate() ,尚未布置您的布局,这就是为什么bmp.getWidth()bmp.getHeight()返回0的原因。
One way to do it is to override onWindowFocusChanged() 一种方法是重写onWindowFocusChanged()

private ImageView img;
private boolean isFullyInitialized = false;

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

    img=(ImageView)findViewById(R.id.imageView1);
}


@Override
public void onWindowFocusChanged(boolean hasFocus)
{
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus && !isFullyInitialized)
    {
        isFullyInitialized = true;
        openCvCode();
    }
}

private void openCvCode()
{
    Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);

    Mat tmp = new Mat(bmp.getWidth(), bmp.getHeight(), CvType.CV_8U);

    Utils.bitmapToMat(bmp, tmp);

    Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
    Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY,4);

    Utils.matToBitmap(tmp, bmp);
    img.setImageBitmap(bmp);
}

Problem solved: 问题解决了:

public class MainActivity extends Activity {

ImageView img;
Bitmap bitmap;

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

    img=(ImageView)findViewById(R.id.imageView1);

    bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.lena);

    if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
    {
        Log.e("TEST", "Cannot connect to OpenCV Manager");
    }


}

private BaseLoaderCallback  mOpenCVCallBack = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {

                    Mat tmp = new Mat(bitmap.getWidth(), bitmap.getHeight(),CvType.CV_8UC1);
                     // Convert
                    Utils.bitmapToMat(bitmap, tmp);

                    Mat gray = new Mat(bitmap.getWidth(), bitmap.getHeight(),CvType.CV_8UC1);
                     // Conver the color
                    Imgproc.cvtColor(tmp, gray, Imgproc.COLOR_RGB2GRAY);
                     // Convert back to bitmap
                    Mat destination = new Mat(gray.rows(),gray.cols(),gray.type());

                    Imgproc.adaptiveThreshold(gray, destination, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 15, 4);

                    Utils.matToBitmap(destination, bitmap);
                    img.setImageBitmap(bitmap);

                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
    }
    };

} }

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

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