如何在图像上添加椒盐噪声?
0 1962
2
该提问暂无详细描述
收藏
2021-04-09 15:17 更新 正直的烤面包 •  3970
共 2 个回答
高赞 时间
3

可以用Python向图像添加椒盐噪声


噪声:在计算机版本中,噪声是指信号中的随机干扰。在图像中添加噪声,这里的信号是图像,对图像亮度和颜色的随机干扰称为图像噪声。

椒盐:仅在灰度图像(黑白图像)中存在。顾名思义,

  • 胡椒粉(黑色)中的盐(白色)——深色区域中的白色斑点;
  • 盐(白色)中的胡椒粉(黑色)——白色区域中的黑色斑点。

换句话说,具有椒盐噪声的图像在明亮区域中将具有一些暗像素,在黑暗区域中将具有一些亮像素,椒盐噪声也称为脉冲噪声。产生椒盐造成的原因有很多,例如像素坏点,模数转换错误,位传输错误等。


具体如何在图像中添加椒盐噪声

  • 椒盐噪声只能添加到灰度图像中,因此需要将输入图像转换为灰度图
  • 随机选择要添加噪声的像素数(number_of_pixels)
  • 随机选择图像中添加噪声的像素点,可以通过随机选择x和y坐标来完成
  • 注意生成的随机值必须在图像尺寸的范围内,x和y坐标必须在图像尺寸范围内
  • 可以使用代码中使用的random.randint之类的随机数生成器函数来生成随机数
  • 将一部分随机选择的像素设置为黑色,将其值设置为0
  • 将一部分随机选取的像素设为白色,将其值设置为255
  • 保存图像的值

Python代码实现

import random
import cv2
  
def add_noise(img):
  
    # Getting the dimensions of the image
    row , col = img.shape
      
    # Randomly pick some pixels in the
    # image for coloring them white
    # Pick a random number between 300 and 10000
    number_of_pixels = random.randint(300, 10000)
    for i in range(number_of_pixels):
        
        # Pick a random y coordinate
        y_coord=random.randint(0, row - 1)
          
        # Pick a random x coordinate
        x_coord=random.randint(0, col - 1)
          
        # Color that pixel to white
        img[y_coord][x_coord] = 255
          
    # Randomly pick some pixels in
    # the image for coloring them black
    # Pick a random number between 300 and 10000
    number_of_pixels = random.randint(300 , 10000)
    for i in range(number_of_pixels):
        
        # Pick a random y coordinate
        y_coord=random.randint(0, row - 1)
          
        # Pick a random x coordinate
        x_coord=random.randint(0, col - 1)
          
        # Color that pixel to black
        img[y_coord][x_coord] = 0
          
    return img
  
# salt-and-pepper noise can
# be applied only to greyscale images
# Reading the color image in greyscale image
img = cv2.imread('lena.jpg',
                 cv2.IMREAD_GRAYSCALE)
  
#Storing the image
cv2.imwrite('salt-and-pepper-lena.jpg',
            add_noise(img))

输入

输出

参考https://www.geeksforgeeks.org/add-a-salt-and-pepper-noise-to-an-image-with-python/

收藏
2021-04-10 11:20 更新 汉乐府 •  21
1

椒盐噪声是最简单的噪声模型之一。因为它的概率分布函数只是每个脉冲在图像上随机出现的机会。可以设置它使黑色像素的数量是白色像素的两倍。

从公式中可以看出,椒盐不必严格按照黑色和白色划分。我们也可以将这些脉冲值设置为自定义像素值。下图说明了每个值的分布,可见,仅在两个强度值处存在尖峰。

下面是针对黑白强度编写的C#函数,你也可以对其修改为每个脉冲设置自定义强度。

public static Bitmap ImpulseNoise(this Bitmap image)
     {
         int w = image.Width;
         int h = image.Height;

         BitmapData image_data = image.LockBits(
             new Rectangle(0, 0, w, h),
             ImageLockMode.ReadOnly,
             PixelFormat.Format24bppRgb);
         int bytes = image_data.Stride * image_data.Height;
         byte[] buffer = new byte[bytes];
         byte[] result = new byte[bytes];
         Marshal.Copy(image_data.Scan0, buffer, 0, bytes);
         image.UnlockBits(image_data);

         Random rnd = new Random();
         int noise_chance = 10;
         for (int i = 0; i < bytes; i+=3)
         {
             int max = (int)(1000 / noise_chance);
             int tmp = rnd.Next(max + 1);
             for (int j = 0; j < 3; j++)
             {
                 if (tmp == 0 || tmp == max)
                 {
                     int sorp = tmp / max;
                     result[i + j] = (byte)(sorp * 255);
                 }
                 else
                 {
                     result[i + j] = buffer[i + j];
                 }
             }
         }

         Bitmap result_image = new Bitmap(w, h);
         BitmapData result_data = result_image.LockBits(
             new Rectangle(0, 0, w, h),
             ImageLockMode.WriteOnly,
             PixelFormat.Format24bppRgb);
         Marshal.Copy(result, 0, result_data.Scan0, bytes);
         result_image.UnlockBits(result_data);

         return result_image;
     }

参考https://epochabuse.com/salt-and-pepper-noise/

收藏
2021-04-10 10:43 更新 阿托 •  16831