Android用opencv - 图像到灰度

时间:2023-01-15 22:56:16

I'm looking for simple example of app in Android which convert color image to grayscale using opencv. I'm trying with thic code, but app crash after this line Mat tmp = new Mat(bmp.getWidth(), bmp.getHeight(), CvType.CV_8U); .

我正在寻找Android中的应用程序的简单示例,它使用opencv将彩色图像转换为灰度。我正在尝试使用这些代码,但应用程序在此行之后崩溃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);
}

3 个解决方案

#1


0  

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)

#2


0  

Your layout has not been laid out yet, while you are in onCreate(), that is why bmp.getWidth() and bmp.getHeight() return 0.
One way to do it is to override onWindowFocusChanged()

你的布局还没有布局,当你在onCreate()时,这就是bmp.getWidth()和bmp.getHeight()返回0的原因。一种方法是覆盖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);
}

#3


0  

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

}

#1


0  

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)

#2


0  

Your layout has not been laid out yet, while you are in onCreate(), that is why bmp.getWidth() and bmp.getHeight() return 0.
One way to do it is to override onWindowFocusChanged()

你的布局还没有布局,当你在onCreate()时,这就是bmp.getWidth()和bmp.getHeight()返回0的原因。一种方法是覆盖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);
}

#3


0  

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

}