简体   繁体   English

向图像中的某个区域添加噪声

[英]adding noise to an area in the image

As part of image preprocessing I want to corrupt an image by adding random pixel values to a part of the image, specified with a mask.作为图像预处理的一部分,我想通过将随机像素值添加到使用掩码指定的图像部分来破坏图像。 I'm working with python.我正在使用 python。 Are there any common ways to do this, or maybe is there a paper published with this information?是否有任何常见的方法可以做到这一点,或者是否有发表过这些信息的论文? All help very much appreciated非常感谢所有帮助

Random pixel values are just random integers between 0 and 255 (for color pictures).随机像素值只是 0 到 255 之间的随机整数(对于彩色图片)。 So you can just pick a random pixel on an image, and replace it by 3 random RGB values.因此,您可以在图像上随机选取一个像素,然后将其替换为 3 个随机 RGB 值。 Let's say you have a picture (all black so we can visualize):假设您有一张图片(全黑,以便我们可以形象化):

import numpy as np
import matplotlib.pyplot as plt

pic = np.full((10, 10, 3), 0)

在此处输入图片说明

Then you can replace a coordinate inside the dimensions of the image (10 by 10 here) by 3 random RGB values between 0 and 255.然后,您可以用 0 到 255 之间的 3 个随机 RGB 值替换图像尺寸(此处为 10 x 10)内的坐标。

pic[np.random.randint(0, 10, 5), 
    np.random.randint(0, 10, 5 )] = \
    np.random.randint(0, 256, (5, 3))

The logic is as follows: take 5 random points in X, Y and replace them with 3 random values between 0 and 255.逻辑如下:在X,Y中取5个随机点,并用0到255之间的3个随机值替换它们。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM