【问题标题】:Converting simple image to greyscale将简单图像转换为灰度
【发布时间】:2013-04-26 08:20:42
【问题描述】:

我正在尝试将普通彩色图像转换为灰度图像。代码非常简单,但不知道为什么会出错。我只是逐像素更改颜色值,然后将其存储在新的位图中。错误当我尝试将像素设置为新位图时即将到来。

 Bitmap c=BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/1.jpg");

    int width=c.getWidth();
    int height=c.getHeight();
    int A,B,R,G;
    int pixel;




    for(int x = 0; x < width; x++) {
        for(int y = 0; y < height; y++) {
            // get one pixel color

       //     pixel = c.getPixel(x, y);
            // retrieve color of all channels

  //          A = Color.alpha(c.getPixel(x, y));
            R = Color.red(c.getPixel(x, y));
            G = Color.green(c.getPixel(x, y));
            B = Color.blue(c.getPixel(x, y));

            // take conversion up to one single value
            R = G = B = (int)(0.299 * R + 0.587 * G + 0.114 * B);
            // set new pixel color to output bitmap
            h=String.valueOf(R);
           bmOut.isMutable();
           bmOut.setPixel(x, y, Color.argb(Color.alpha(c.getPixel(x, y)), R, G, B));
        }
    //  Toast.makeText(getApplicationContext(), h, Toast.LENGTH_SHORT).show();    
    }

这是我的日志

  05-02 13:37:37.858: E/AndroidRuntime(19254): FATAL EXCEPTION: main
  05-02 13:37:37.858: E/AndroidRuntime(19254): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.otsu/com.example.otsu.MainActivity}: java.lang.NullPointerException
  05-02 13:37:37.858: E/AndroidRuntime(19254):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
  05-02 13:37:37.858: E/AndroidRuntime(19254):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
 05-02 13:37:37.858: E/AndroidRuntime(19254):   at android.app.ActivityThread.access$1500(ActivityThread.java:135)
 05-02 13:37:37.858: E/AndroidRuntime(19254):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
 05-02 13:37:37.858: E/AndroidRuntime(19254):   at android.os.Handler.dispatchMessage(Handler.java:99)
 05-02 13:37:37.858: E/AndroidRuntime(19254):   at android.os.Looper.loop(Looper.java:150)
 05-02 13:37:37.858: E/AndroidRuntime(19254):   at android.app.ActivityThread.main(ActivityThread.java:4389)
 05-02 13:37:37.858: E/AndroidRuntime(19254):   at java.lang.reflect.Method.invokeNative(Native Method)
 05-02 13:37:37.858: E/AndroidRuntime(19254):   at java.lang.reflect.Method.invoke(Method.java:507)
 05-02 13:37:37.858: E/AndroidRuntime(19254):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
 05-02 13:37:37.858: E/AndroidRuntime(19254):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
 05-02 13:37:37.858: E/AndroidRuntime(19254):   at dalvik.system.NativeStart.main(Native Method)
 05-02 13:37:37.858: E/AndroidRuntime(19254): Caused by: java.lang.NullPointerException
 05-02 13:37:37.858: E/AndroidRuntime(19254):   at com.example.otsu.MainActivity.onCreate(MainActivity.java:58)
 05-02 13:37:37.858: E/AndroidRuntime(19254):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
 05-02 13:37:37.858: E/AndroidRuntime(19254):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
 05-02 13:37:37.858: E/AndroidRuntime(19254):   ... 11 more

【问题讨论】:

  • MainActivity.java:58 的线路是什么?
  • 你在第 58 行有一个 Null 对象:在 com.example.otsu.MainActivity.onCreate(MainActivity.java:58) 你能指出那是哪一行代码吗?
  • 您的 onCreate 方法上有一个 NPE,与上述代码无关。话虽如此,here is a simpler solution to convert an image to grayscale
  • 我正在初始化 R、G 和 B 的行。

标签: android image bitmap


【解决方案1】:

我在尝试实现相同目标时发现了两种方法。

  1. 使用ColorMatrix

    private Bitmap androidGrayScale(final Bitmap bmpOriginal) {
        int width, height;
        height = bmpOriginal.getHeight();
        width = bmpOriginal.getWidth();
        Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmpGrayscale);
        Paint paint = new Paint();
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0);
        ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
        paint.setColorFilter(colorMatrixFilter);
        canvas.drawBitmap(bmpOriginal, 0, 0, paint);
        return bmpGrayscale;
    }
    
  2. 使用OpenCV

下载OpenCV库并作为库项目导入。将此库作为参考库添加到您的项目中。

下载链接:OpenCV

private Bitmap openCVGrayScale(final Bitmap bmpOriginal, final String filePath) {
        Mat imgToProcess;
        Mat imgToDest = new Mat();
        imgToProcess = Highgui.imread(filePath, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
        org.opencv.android.Utils.bitmapToMat(bmpOriginal, imgToProcess);
        Imgproc.cvtColor(imgToProcess, imgToDest, Imgproc.COLOR_BGR2GRAY);
        Bitmap bmpGrayscale = Bitmap.createBitmap(imgToDest.cols(), imgToDest.rows(), Bitmap.Config.ARGB_8888); 
        org.opencv.android.Utils.matToBitmap(imgToDest, bmpGrayscale);
        return bmpGrayscale;
    }

不要忘记签入您的Activity

static {
    if (!OpenCVLoader.initDebug()) {
        android.util.Log.e("TAG", "Error");
    }
}

谢谢。

【讨论】:

  • 问题已解决。非常感谢。
猜你喜欢
  • 2010-11-20
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
相关资源
最近更新 更多