【问题标题】:How to detect all pixels of specific color (and similar ones) in a bitmap?如何检测位图中特定颜色(和类似颜色)的所有像素?
【发布时间】:2019-03-25 06:15:09
【问题描述】:

我正在 Android Studio 中处理位图图像,并且我知道如何检测所有红色像素作为示例,但不仅仅是严格等于 Color.RED 的像素。我只是举一个红色的例子,但我想用所有不同的颜色来做。

I tried to do this:
...
int[] pixels = new int[width * height];
myBitmap.getPixels(pixels, 0, width, 0, 0, width, height);
for (int i = 0; i < width * height; i++) {
    if (pixels[i] == Color.RED) {
        // color detected
    }
...

但这不起作用,因为我的图像不包含 (255, 0, 0) 的红色像素,但包含可能是 (251, 30, 77) 或 (239, 23, 42) 的像素,即还有“红色”等……

那么我怎么能对这样的事情做呢?我也尝试过类似的东西:

int reference; // color as rgb
if ( (Color.red(pixels[i]) > Color.red(reference) - 20) &&
     (Color.red(pixels[i]) < Color.red(reference) + 20 &&
...
{
   // pixel detected
}
...

我认为 (r +- 20, g +- 20, b +-20) 是 (r, g, b) 的一部分(我的意思是,对于人眼)

知道我该怎么做吗?

【问题讨论】:

标签: java android image-processing colors bitmap


【解决方案1】:

您可以接受在您想要的颜色一定距离内的所有像素。假设您想要 RED 以及 RED 的 10 欧几里得距离内的所有内容:

boolean distance(int a, int b) {
return Math.sqrt(Math.pow(Color.red(a) - Color.red(b), 2) + Math.pow(Color.blue(a) - Color.blue(b), 2) + Math.pow(Color.green(a) - Color.green(b), 2));
}
....
int reference = Color.RED;
if (distance(reference, pixels[i]) < 10) {
//pixel accepted
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-12
    • 2018-07-16
    • 2011-06-04
    • 2021-08-17
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多