Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android为位图添加细微的噪声_Android_Image Processing_Bitmap - Fatal编程技术网

Android为位图添加细微的噪声

Android为位图添加细微的噪声,android,image-processing,bitmap,Android,Image Processing,Bitmap,有人知道如何给灰度图像添加细微的噪声吗? 我有一个灰度图像,我想添加一些微妙的黑白噪音。有人知道怎么做吗 我目前正在使用这种方法,但这只是生成随机颜色并用这些颜色交换现有像素。如何仅在位图上的一些像素上添加一些细微的黑白噪声 public static Bitmap applyFleaEffect(Bitmap source) { // get source image size int width = source.getWidth(); int height = source.getH

有人知道如何给灰度图像添加细微的噪声吗? 我有一个灰度图像,我想添加一些微妙的黑白噪音。有人知道怎么做吗

我目前正在使用这种方法,但这只是生成随机颜色并用这些颜色交换现有像素。如何仅在位图上的一些像素上添加一些细微的黑白噪声

public static Bitmap applyFleaEffect(Bitmap source) {
// get source image size
 int width = source.getWidth();
  int height = source.getHeight();
 int[] pixels = new int[width * height];
 // get pixel array from source
 source.getPixels(pixels, 0, width, 0, 0, width, height);
 // create a random object
 Random random = new Random();

int index = 0;
// iteration through pixels
for(int y = 0; y < height; ++y) {
for(int x = 0; x < width; ++x) {
// get current index in 2D-matrix
index = y * width + x;
// get random color
int randColor = Color.rgb(random.nextInt(255),
  random.nextInt(255), random.nextInt(255));
// OR
pixels[index] |= randColor;
public静态位图applyFleaEffect(位图源){
//获取源图像大小
int width=source.getWidth();
int height=source.getHeight();
int[]像素=新int[宽度*高度];
//从源获取像素阵列
getPixels(像素,0,宽度,0,0,宽度,高度);
//创建一个随机对象
随机=新随机();
int指数=0;
//像素迭代
对于(int y=0;y
} } //输出位图 Bitmap bmOut=Bitmap.createBitmap(宽度、高度、source.getConfig()); 设置像素(像素,0,宽度,0,0,宽度,高度); 返回bmOut; } }

更新:添加完整代码以显示灰度阶跃和尝试的噪波阶跃

      //First, apply greyscale to make Image black and white
    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, mBitmap.getConfig());
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);

    //Apply noise after grayscale
    int[] pixels = new int[width * height];

    bmpGrayscale.getPixels(pixels, 0, width, 0, 0, width, height);
    // a random object
    Random random = new Random();

    int index = 0;

    // Note: Declare the c and randColor variables outside of the for loops
    int co = 0;
    int randColor = 0;
    // iteration through pixels
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            if (random.nextInt(101) < percentNoise) {
                // Skip this iteration a certain percentage of the time
                continue;
            }
            // get current index in 2D-matrix
            index = y * width + x;


            co = random.nextInt(255);


            randColor = Color.rgb(co, co, co);
            pixels[index] |= randColor;




        }
    }
    // output bitmap
    mBitmap = Bitmap.createBitmap(width, height, bmpGrayscale.getConfig());
    mBitmap.setPixels(pixels, 0, width, 0, 0, width, height);

    c.drawBitmap(mBitmap, 0, 0, paint);
    return bmpGrayscale;
}
//首先,应用灰度使图像黑白
位图bmpGrayscale=Bitmap.createBitmap(宽度、高度、mBitmap.getConfig());
画布c=新画布(bmpGrayscale);
油漆=新油漆();
ColorMatrix cm=新的ColorMatrix();
cm.设定饱和度(0);
ColorMatrixColorFilter f=新的ColorMatrixColorFilter(厘米);
油漆。设置颜色过滤器(f);
//在灰度之后应用噪波
int[]像素=新int[宽度*高度];
getPixels(像素,0,宽度,0,0,宽度,高度);
//随机物体
随机=新随机();
int指数=0;
//注意:在for循环之外声明c和randColor变量
int co=0;
int-randColor=0;
//像素迭代
对于(int y=0;y
首先,
randColor
不太可能是代码中的灰色。要生成随机的灰度颜色,请执行以下操作:

int c = random.nextInt(255);
int randColor = Color.rgb(c, c, c);
考虑到这一点,我将如何重新编写代码(注意:根据您的喜好调整
percentNoise
参数):

public静态位图applyFleaEffect(位图源,int-percentNoise){
//获取源图像大小
int width=source.getWidth();
int height=source.getHeight();
int[]像素=新int[宽度*高度];
//从源获取像素阵列
getPixels(像素,0,宽度,0,0,宽度,高度);
//创建一个随机对象
随机=新随机();
int指数=0;
//注意:在for循环之外声明c和randColor变量
int c=0;
int-randColor=0;
//遍历像素
对于(int y=0;y
Funkystein的可能重复当我问一个完全不同的问题时,这怎么可能重复?另外,这个问题的方法和我用的是一样的。我只想添加更微妙的黑白噪音。不是随机颜色。只能使用黑色而不是随机颜色。或者(甚至更好)在转换为灰度之前应用噪波:噪波也将是灰度的。@Funkystein说得通,谢谢。我要做的是让噪音变得微妙而轻,而不是强烈,并覆盖整个图像。你认为使噪声半透明会有帮助吗?半透明会“更微妙”,更难捕捉,但在整个图像上仍然稀疏(与你运行的迭代次数一样密集)。我不知道一个算法来定位轮廓附近的“灰尘”(也许你想要这个?)。你能看看我的编辑吗?我试着先把图像转换成灰度,然后给它添加很小的噪声。我在噪音场附近玩耍。越接近零,位图的白色似乎越多,接近100%的噪波越黑。对此有什么见解吗?
public static Bitmap applyFleaEffect(Bitmap source, int percentNoise) {
    // get source image size
    int width = source.getWidth();
    int height = source.getHeight();
    int[] pixels = new int[width * height];
    // get pixel array from source
    source.getPixels(pixels, 0, width, 0, 0, width, height);
    // create a random object
    Random random = new Random();

    int index = 0;
    // Note: Declare the c and randColor variables outside of the for loops
    int c = 0;
    int randColor = 0;
    // iterate through pixels
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            if (random.nextInt(101) < percentNoise) {
                // Skip this iteration a certain percentage of the time
                continue;
            }
            // get current index in 2D-matrix
            index = y * width + x;
            // get random color
            c = random.nextInt(255);
            randColor = Color.rgb(c, c, c);
            pixels[index] |= randColor;
        }
    }
    Bitmap bmOut = Bitmap.createBitmap(width, height, source.getConfig());
    bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
    return bmOut;
}