使用Android拍摄黑白照片(单色照片)

5

我希望我的应用程序可以拍摄真正的黑白照片。我搜索了解决方案(包括在此网站上),但我总是找到将照片变为灰度的解决方案(例如在这个主题中),但这不是我要寻找的...

我还发现了一个主题,提出了以下建议:

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;

    }

但是图片质量很糟糕……有没有人有想法?谢谢。

嗯,这可能是一个愚蠢的问题,但“真正的黑白”和灰度之间有什么区别?两者都由各种灰色调制而成,但这些术语是否有特定的定义?顺便说一句,“50种灰色”的笑话是被禁止的! :) - Alex
@Alex - 我也在想同样的问题,并偶然发现了这个例子http://farm3.staticflickr.com/2393/3533241378_04c2b649c2_b.jpg。顶部的图像是单色的,底部的是灰度图像。我想这取决于OP试图实现什么-从手机相机中捕获真正的单色图像还是转换现有图像?我问的唯一原因是因为他已经发布了转换后的示例 :-) - shri046
@shri046,如果没有一些词语,你的例子就不是一个解释。单色意味着一种颜色与其他颜色的背景相对比。黑白意味着它是黑色在白色背景下。我猜你展示的例子只是使用不同的滤镜从彩色转换为黑白。 - slim
我认为这可能是合适的:https://dev59.com/jUzSa4cB1Zd3GeqPpL1x - slim
@slim 我原以为 OP 不想要真正的单色(即仅有像素值 0 和 255),这让我感到困惑。由于我在工作中,无法检查大多数链接的图像(被封锁了,呃)。顺便说一句,黑白也可以指黑底白字!斑马是黑色带白条纹还是白色带黑条纹? :) - Alex
2个回答

18

如果你想让图像是1位的黑白色,你可以使用一个简单(而且较慢)的阈值算法。

public static Bitmap createBlackAndWhite(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;

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

但是,如果只是这样处理图像的话,效果不够好。想要更好的结果,您需要使用抖动算法,可以参考算法概述,其中包括阈值方法。


对于256级灰度转换:

根据http://www.mathworks.de/help/toolbox/images/ref/rgb2gray.html,您可以将每个像素的灰度计算为gray = 0.2989 * R + 0.5870 * G + 0.1140 * B,这将转化为:

public static Bitmap createGrayscale(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;

    // 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);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            int gray = (int) (0.2989 * R + 0.5870 * G + 0.1140 * B);
            // set new pixel color to output bitmap
            bmOut.setPixel(x, y, Color.argb(A, gray, gray, gray));
        }
    }
    return bmOut;
}

但这样做非常慢,因为你必须对数百万个像素分别进行操作。

https://dev59.com/iWox5IYBdhLWcg3wLhYA#9377943 提供了一种更好的方式来实现同样的功能。

// code from that answer put into method from above
public static Bitmap createGrayscale(Bitmap src) {
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmOut);
    ColorMatrix ma = new ColorMatrix();
    ma.setSaturation(0);
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(ma));
    canvas.drawBitmap(src, 0, 0, paint);
    return bmOut;
}

非常感谢,您的“createBlackAndWhite()”方法正是我在寻找的。就像您说的有点慢,但这是真正的黑白效果。我会尝试对它进行一些优化,但它运行良好。 - Vince
一个问题:在这段代码中"(0.2989 * R + 0.5870 * G + 0.1140 * B)",为什么要使用这些值:0.2989、0.5870和0.1140? - Vince
1
@Vince 这是将彩色转换为灰度的可能方法之一。 人类对颜色的亮度有不同的感知(例如,蓝色比绿色明亮得多),因此这种方法尝试匹配人类的感知。 可以查看这里的图像或阅读https://dev59.com/23RB5IYBdhLWcg3wNk93。 - zapl

1

G = Color.red(pixel);

G = Color.green(pixel);

B = Color.red(pixel);

B = Color.blue(pixel);

看看这些变化(加粗部分)是否有帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接