如何知道图像是否为灰度?

时间:2019-03-14 14:38:45

标签: android

使用BufferedImage时,我可以使用getColorModel和getType确定哪种类型的图像,但是如何在位图上实现呢?

我已经尝试过一次,布尔值总是返回true;

public boolean isGreyscale(Bitmap image)
{
    int width = image.getWidth();
    int height = image.getHeight();
    int pixel,red, green, blue;
    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            pixel = image.getPixel(i, j);
            red = (pixel >> 16) & 0xff;
            green = (pixel >> 8) & 0xff;
            blue = (pixel) & 0xff;
            if (red != green || green != blue ) return false;
        }
    }
    return true;
}

然后我尝试使用OpenCV,但不知道该怎么做,因此我会检查频道并始终返回4个频道(包括Alpha),

 public boolean checkIfGrayscale(Bitmap bitmap){
    Mat matImages = new Mat();
    Utils.bitmapToMat(bitmap, matImages);
    matImages.channels();

    if(matImages.channels()>1){
        return  false;
    }
    return true;
}

因为我认为OpenCV灰度图像是1通道,而rgb是4通道

没有任何效果。当我尝试输入rgb时,返回值仍然为true。我被困了一个星期。

0 个答案:

没有答案
相关问题