gpt4 book ai didi

Android实时黑白阈值图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:43:37 24 4
gpt4 key购买 nike

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

// scan through all pixels
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
// get pixel color
pixel = bitmap.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));
}
}

如您所见,我遍历了原始位图的所有像素点,然后将颜色的分量与给定阈值(在本例中为 128)进行比较,然后如果它高于我就说它是白色,否则它将是黑色像素。

我现在想做的是一个可以改变阈值的 Spinner,然后 BW 图像就会不同。

为此,我需要重新绘制所有图像,这非常耗费 CPU 时间,再次遍历所有像素也需要时间。

有什么方法可以实时使用不同的 BW 阈值更改图像?

有人告诉我使用 GIF,然后我会做的只是更改 GIF 的查找表值,有没有人知道 Android 上的这方面的知识?

最佳答案

自问这个问题以来已经过去了一段时间,但我在寻找其他东西时遇到了这个问题并且碰巧找到了解决方案。您可以在没有 OpenCV 或任何其他第三方库的情况下实现这一点,仅使用 ColorMatrixColorFilter自 API 级别 1 起可用。

以下是您可以使用的矩阵:

//matrix that changes picture into gray scale
public static ColorMatrix createGreyMatrix() {
ColorMatrix matrix = new ColorMatrix(new float[] {
0.2989f, 0.5870f, 0.1140f, 0, 0,
0.2989f, 0.5870f, 0.1140f, 0, 0,
0.2989f, 0.5870f, 0.1140f, 0, 0,
0, 0, 0, 1, 0
});
return matrix;
}

// matrix that changes gray scale picture into black and white at given threshold.
// It works this way:
// The matrix after multiplying returns negative values for colors darker than threshold
// and values bigger than 255 for the ones higher.
// Because the final result is always trimed to bounds (0..255) it will result in bitmap made of black and white pixels only
public static ColorMatrix createThresholdMatrix(int threshold) {
ColorMatrix matrix = new ColorMatrix(new float[] {
85.f, 85.f, 85.f, 0.f, -255.f * threshold,
85.f, 85.f, 85.f, 0.f, -255.f * threshold,
85.f, 85.f, 85.f, 0.f, -255.f * threshold,
0f, 0f, 0f, 1f, 0f
});
return matrix;
}

下面是如何使用它们:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;

//load source bitmap and prepare destination bitmap
Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.thePicture, options);
Bitmap result = Bitmap.createBitmap(pic.getWidth(), pic.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(result);

//first convert bitmap to grey scale:
bitmapPaint.setColorFilter(new ColorMatrixColorFilter(createGreyMatrix()));
c.drawBitmap(pic, 0, 0, bitmapPaint);

//then convert the resulting bitmap to black and white using threshold matrix
bitmapPaint.setColorFilter(new ColorMatrixColorFilter(createThresholdMatrix(128)));
c.drawBitmap(result, 0, 0, bitmapPaint);

//voilà! You can now draw the result bitmap anywhere You want:
bitmapPaint.setColorFilter(null);
otherCanvas.drawBitmap(result, null, new Rect(x, y, x + size, y + size), bitmapPaint);

希望这会对某人有所帮助。

关于Android实时黑白阈值图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22731653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com