有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用OpenCV将图像转换为黑白图像并在ANDROID中消除阴影

我有一个将RGB位图转换为黑白位图的代码,使用以下代码:

  public static Bitmap setDefaultValues(Bitmap bmp) {

    Mat srcMat = new Mat();
    org.opencv.安卓.Utils.bitmapToMat(bmp, srcMat, true);


    final Bitmap bitmap = Bitmap.createBitmap(srcMat.clone().width(), srcMat.clone().height(), Bitmap.Config.ARGB_8888);

    Imgproc.cvtColor(srcMat, srcMat, Imgproc.COLOR_BGR2GRAY, 0);

    Mat srcMat1 = srcMat;
    Imgproc.GaussianBlur(srcMat1, srcMat1, new Size(3, 3), 0);

    //Mat srcMat1 = new Mat(srcMat.rows(), srcMat.cols(), CV_8UC1);
    //int kernalsize = 3;
    //Imgproc.bilateralFilter(srcMat, srcMat1, kernalsize, kernalsize * 2, kernalsize / 2);

    srcMat1.convertTo(srcMat1, 0, 1.9, -120);
    srcMat1.convertTo(srcMat1, CvType.CV_8U, 1.9, -120);
    Imgproc.cvtColor(srcMat1, srcMat1, Imgproc.COLOR_GRAY2RGBA, 4);

    org.opencv.安卓.Utils.matToBitmap(srcMat, bitmap, true);
    return bitmap;

}

我已经实现了将RGB图像转换成黑白的代码。 这是返回我的权利,但我的问题是在这里,我不能从图像中删除阴影

我还比较了其他应用程序,这是完美的转换,我不明白我错在哪里

这是原始图像: enter image description here

这是我的应用程序输出 enter image description here

这是其他应用程序输出 enter image description hereenter image description here

所以请帮助我如何实现我的目标


共 (3) 个答案

  1. # 1 楼答案

    请使用以下代码将彩色图像转换为黑白

    public static Bitmap createContrast(Bitmap src, double value) {
    // image size
    int width = src.getWidth();
    int height = src.getHeight();
    // create output bitmap
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
    // color information
    int A, R, G, B;
    int pixel;
    // get contrast value
    double contrast = Math.pow((100 + value) / 100, 2);
    
    // scan through all pixels
    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < height; ++y) {
            // get pixel color
            pixel = src.getPixel(x, y);
            A = Color.alpha(pixel);
            // apply filter contrast for every channel R, G, B
            R = Color.red(pixel);
            R = (int)(((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
            if(R < 0) { R = 0; }
            else if(R > 255) { R = 255; }
    
            G = Color.red(pixel);
            G = (int)(((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
            if(G < 0) { G = 0; }
            else if(G > 255) { G = 255; }
    
            B = Color.red(pixel);
            B = (int)(((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
            if(B < 0) { B = 0; }
            else if(B > 255) { B = 255; }
    
            // set new pixel color to output bitmap
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }
    
    return bmOut;}
    
  2. # 2 楼答案

    如果你能找到解决方案,请试试这个

    public static Bitmap test(Bitmap src){
      int width = src.getWidth();
        int height = src.getHeight();
        // create output bitmap
        Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
        // color information
        int A, R, G, B;
        int pixel;
        for (int x = 0; x < width; ++x) {
            for (int y = 0; y < height; ++y) {
                // get pixel color
                pixel = src.getPixel(x, y);
                A = Color.alpha(pixel);
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
                int gray = (int) (0.2989 * R + 0.5870 * G + 0.1140 * B);
                // use 128 as threshold, above -> white, below -> black
                if (gray > 128) {
                    gray = 255;
                }
                else{
                    gray = 0;
                }
                    // set new pixel color to output bitmap
                bmOut.setPixel(x, y, Color.argb(A, gray, gray, gray));
            }
        }
        return bmOut;
    }