Android Java - 如何快速去除图像噪点?
Android Java - How to remove image noise in faster way?
我有这些代码可以减少图像噪声:
for (int x = 0; x < bitmap.getWidth(); x++) {
for (int y = 0; y < bitmap.getHeight(); y++) {
// get one pixel color
int pixel = processedBitmap.getPixel(x, y);
// retrieve color of RGB
int R = Color.red(pixel);
int G = Color.green(pixel);
int B = Color.blue(pixel);
// convert into single value
R = G = B = (int) (0.299 * R + 0.587 * G + 0.114 * B);
// convert to black and white + remove noise
if (R > 162 && G > 162 && B > 162)
bitmap.setPixel(x, y, Color.WHITE);
else if (R < 162 && G < 162 && B < 162)
bitmap.setPixel(x, y, Color.BLACK);
}
}
但是需要很长时间才能产生结果。有没有其他方法可以优化这些代码以使其更快?
不要使用 getPixel。获取图像数据作为数组并使用数学来访问正确的像素。编写数学运算,以便使用尽可能少的乘法。 setPixel 相同。
不要使用Color.red()、Color.green()等。使用掩码,它比函数调用更有效。
更好的是,加入 NDK 并在 C 中执行此操作。Java 中的图像处理通常不是最佳的。
我有这些代码可以减少图像噪声:
for (int x = 0; x < bitmap.getWidth(); x++) {
for (int y = 0; y < bitmap.getHeight(); y++) {
// get one pixel color
int pixel = processedBitmap.getPixel(x, y);
// retrieve color of RGB
int R = Color.red(pixel);
int G = Color.green(pixel);
int B = Color.blue(pixel);
// convert into single value
R = G = B = (int) (0.299 * R + 0.587 * G + 0.114 * B);
// convert to black and white + remove noise
if (R > 162 && G > 162 && B > 162)
bitmap.setPixel(x, y, Color.WHITE);
else if (R < 162 && G < 162 && B < 162)
bitmap.setPixel(x, y, Color.BLACK);
}
}
但是需要很长时间才能产生结果。有没有其他方法可以优化这些代码以使其更快?
不要使用 getPixel。获取图像数据作为数组并使用数学来访问正确的像素。编写数学运算,以便使用尽可能少的乘法。 setPixel 相同。
不要使用Color.red()、Color.green()等。使用掩码,它比函数调用更有效。
更好的是,加入 NDK 并在 C 中执行此操作。Java 中的图像处理通常不是最佳的。