【问题标题】:How to convert image to black & white image and remove shadow in andorid Using OpenCV如何使用 OpenCV 将图像转换为黑白图像并在 android 中去除阴影
【发布时间】:2017-10-13 10:58:20
【问题描述】:

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

  public static Bitmap setDefaultValues(Bitmap bmp) {

    Mat srcMat = new Mat();
    org.opencv.android.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.android.Utils.matToBitmap(srcMat, bitmap, true);
    return bitmap;

}

我已经实现了将 RGB 图像转换为黑白图像的代码。 这是正确的回报我,但我的问题是我无法从图像中移除阴影。

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

这是原始图像:

这是我的应用程序输出

这是其他应用程序输出

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

【问题讨论】:

  • 你甚至不需要 OpenCV。 ColorMatrices 非常快。并且可以轻松进行灰度和亮度/对比度操作。
  • 但我想要返回白色输出而不是灰度。我也上传了想要的示例图片。
  • 是同一个概念:先灰度图像,再拍摄亮度和对比度。
  • 我有这样的实现,但它返回阴影(意味着无法删除阴影,根据我的要求) ColorMatrix cm = new ColorMatrix(new float[] { 1.0f, 0, 0, 0, 55.0f, 0, 1.0f, 0, 0, 55.0f, 0, 0, 1.0f, 0, 55.0f, 0, 0, 0, 1.0f, 0 });画布画布 = 新画布(srcBtm);油漆油漆 = 新油漆(); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(srcBtm, 0, 0, 油漆);返回 srcBtm;
  • 执行单个 ColorMatrix 是不够的。您必须一个接一个地运行它们。做一些实验,哪些是提高亮度和对比度的最佳值。这将消除大部分灰色,留下大部分黑色和白色。但请注意,您仍然需要至少 一些 灰色,以获得抗锯齿效果。否则,线条会非常参差不齐。意思是对角线和弯曲的。

标签: java android opencv bitmap


【解决方案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;}

【讨论】:

  • 逐像素扫描图像是slooooooooooooooooooow!!请改用 ColorMatrices。他们一次处理整个图像,一次完成。
  • 转换需要时间,(例如,当我发送 3 张图像时需要 10 到 12 秒),当我们增加对比度值时,它也会影响我想要的效果。
【解决方案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;
}

【讨论】:

  • (质量下降)这就是那样回报我。链接:-imgur.com/4FSAb2Z 但我希望你能看到我的问题......
【解决方案3】:

请在此线程上查看答案。他已经解释并提供了良好的输出结果。 @Threshold image using opencv (Java)

【讨论】:

    猜你喜欢
    • 2011-11-29
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    相关资源
    最近更新 更多