gpt4 book ai didi

python - 在python中为RGB图像添加噪声

转载 作者:行者123 更新时间:2023-12-05 07:36:45 25 4
gpt4 key购买 nike

我需要在 python 中为多个彩色图像(文件格式为 ppm;来源:http://benchmark.ini.rub.de/Dataset/GTSRB_Final_Training_Images.zip)添加噪声。噪声输出图像应该仍然是彩色的。

我尝试了以下方法:

from scipy import misc
import numpy as np
import cv2
import imageio

# Read image ('00000_00030.ppm') from file system
image = misc.imread('./00000/00000_00030.ppm', mode="RGB")

# Add noise to the input image
noised_image = image + 3 * image.std() * np.random.random(image.shape)

# Plot original and noisy images
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 10))
f, axarr = plt.subplots(2, 2)

axarr[0, 0].imshow(image)
axarr[0, 0].set_title('Original image')

axarr[0, 1].imshow(noised_image)
axarr[0, 1].set_title('Noised image')

plt.show()

# Save noised image to file system
saved_image = cv2.imwrite("./noised.ppm", noised_image)

但首先的问题是在 jupyter notebook 中无法正确绘制噪声图像(见图 1):

figure 1

第二个问题是 RG channel (红色和绿色)丢失(在保存的文件中):

figure 2

那么如何保留噪声图像中的所有 RGB 颜色?

经过长时间的搜索,我有了现在的解决方案 - 保存的文件现在保留了所有 RGB 颜色(参见以下代码中的第 8 行;参见图 3):

from scipy import misc
import numpy as np
import cv2
import imageio

# Read image ('00000_00030.ppm') from file system
# image = misc.imread('./00000/00000_00030.ppm', mode="RGB")
image = cv2.imread('./00000/00000_00030.ppm',1)

# Add noise to the input image
noised_image = image + 3 * image.std() * np.random.random(image.shape)

# Plot original and noisy images
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 10))
f, axarr = plt.subplots(2, 2)

axarr[0, 0].imshow(image)
axarr[0, 0].set_title('Original image')

axarr[0, 1].imshow(noised_image)
axarr[0, 1].set_title('Noised image')

plt.show()

# Save noised image to file system
saved_image = cv2.imwrite("./noised1.ppm", noised_image)

Figure 3

但绘制的数字仍然是错误的:

Figure 4

这是在 python 中为 RGB 图像添加噪声并正确绘制它们的最终代码:

from scipy import misc
import numpy as np
import cv2
import imageio

# Read image ('00000_00030.ppm') from file system
# image = misc.imread('./00000/00000_00030.ppm', mode="RGB")
image = cv2.imread('./00000/00000_00030.ppm',1)

# Add noise to the input image
noised_image = image + 3 * image.std() * np.random.random(image.shape)

RGB_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Plot original and noisy images
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 10))
f, axarr = plt.subplots(2, 2)

axarr[0, 0].imshow(RGB_image)
axarr[0, 0].set_title('Original image')

axarr[0, 1].imshow(noised_image)
axarr[0, 1].set_title('Noised image')

plt.show()

# Save noised image to file system
saved_image = cv2.imwrite("./noised1.ppm", noised_image)

最佳答案

这将获取给定图像的像素值,并开始对您作为输入提供给像素中最低有效位的噪声进行编码。图像输出会略有不同。

def asciiToBin(ascii):
return ''.join(str(bin(ord(byte)))[2:].zfill(8) for byte in ascii)

def hide(img, data, outName):
dataBin = asciiToBin(data)
pixels, mode = list(img.getdata()), img.mode
newPixels = []

for i in range(len(dataBin)):
newPixel = list(pixels[i])
newPixel[i%len(mode)] = setLSB(newPixel[i%len(mode)], dataBin[i])
newPixels.append(tuple(newPixel))

newData = newPixels + pixels[len(newPixels):]

img.putdata(newData)
img.save(outName, "PNG")

def setLSB(target, value):
binary = str(bin(target))[2:]
if binary[-1] != value:
binary = binary[:-1] + value
return int(binary, 2)

关于python - 在python中为RGB图像添加噪声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48990614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com