NoiseRemover in Android
import android.graphics.Bitmap;
public class NoiseRemover {
// Function to remove noise from image
public static Bitmap removeNoise(Bitmap bitmap) {
// Define the size of the kernel for the filter
int kernelSize = 3;
// Define the threshold for the filter
int threshold = 50;
// Create a new bitmap to store the filtered image
Bitmap filteredBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
// Apply the filter to each pixel in the image
for (int i = kernelSize; i < bitmap.getWidth() - kernelSize; i++) {
for (int j = kernelSize; j < bitmap.getHeight() - kernelSize; j++) {
// Get the color of the current pixel and its neighbors
int pixel = bitmap.getPixel(i, j);
int neighbor1 = bitmap.getPixel(i - 1, j - 1);
int neighbor2 = bitmap.getPixel(i - 1, j);
int neighbor3 = bitmap.getPixel(i - 1, j + 1);
int neighbor4 = bitmap.getPixel(i, j - 1);
int neighbor5 = bitmap.getPixel(i, j + 1);
int neighbor6 = bitmap.getPixel(i + 1, j - 1);
int neighbor7 = bitmap.getPixel(i + 1, j);
int neighbor8 = bitmap.getPixel(i + 1, j + 1);
// Calculate the average color of the neighbors
int avgNeighborColor = (neighbor1 + neighbor2 + neighbor3 + neighbor4 +
neighbor5 + neighbor6 + neighbor7 + neighbor8) / 8;
// Calculate the difference between the current pixel and the average neighbor color
int colorDiff = Math.abs(pixel - avgNeighborColor);
// If the color difference is above the threshold, set the current pixel to the average neighbor color
if (colorDiff > threshold) {
filteredBitmap.setPixel(i, j, avgNeighborColor);
} else {
filteredBitmap.setPixel(i, j, pixel);
}
}
}
return filteredBitmap;
}
}