簡體   English   中英

如何使用OpenCV將圖像轉換為黑白圖像並消除andorid中的陰影

[英]How to convert image to black & white image and remove shadow in andorid Using OpenCV

我有一個使用以下代碼將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圖像轉換為黑白圖像的代碼。 這是正確的返回我,但我的問題是我無法從圖像中去除陰影。

我也比較其他應用程序,這是完美轉換,我不明白我在哪里錯。

這是原始圖片: 在此處輸入圖片說明

這是我的應用程序輸出 在此處輸入圖片說明

這是其他應用程序的輸出 在此處輸入圖片說明 在此處輸入圖片說明

因此,請幫助我如何實現我的目標。

請使用以下代碼將彩色圖像轉換為黑白圖像。

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

如果您可以找到解決方案,請嘗試此操作

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

請查看該線程的答案。 他已經解釋並在輸出中提供了良好的結果。 @ 使用opencv(Java)的閾值圖像

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM