简体   繁体   English

向图像心理添加噪声

[英]Adding noise to an image psychopy

I am trying to add noise to images in psychopy.我试图在心理中为图像添加噪音。 I was using imread but do not appear able to read images in the format needed to add noise to individual pixels throughout the image.我正在使用 imread 但似乎无法以向整个图像中的单个像素添加噪声所需的格式读取图像。 I'm hoping someone may have ideas of how I might be able to do this.我希望有人可能对我如何能够做到这一点有想法。

Specifically, my task presents stimuli to participants at their own perceptual threshold.具体来说,我的任务是以参与者自己的感知阈值向参与者呈现刺激。 I am using an adaptive staircasing procedure (the quest handler) to titrate between trials the amount of noise added to an image to reach this threshold.我正在使用自适应楼梯程序(任务处理程序)在试验之间滴定添加到图像中的噪声量以达到此阈值。 ie, the participant identifies the image correctly, the next image has more noise;即参与者正确识别图像,下一个图像有更多的噪声; they identify an image incorrectly, the next trial has less noise.他们错误地识别了图像,下一次试验的噪音更少。 We do this over repeated trials to get the amount of noise needed for participants to answer a certain percentage of trials correctly.我们在重复试验中这样做,以获得参与者正确回答一定百分比试验所需的噪音量。

I'm trying to add an amount of noise at each trial equal to the percent of noise passed from the quest handler by altering individual pixels to add gaussian noise.我试图通过改变单个像素来添加高斯噪声,在每次试验中添加一个等于从任务处理程序传递的噪声百分比的噪声量。 I do not wish to alter the original image.我不想改变原始图像。 I envision this working by reading the image in as a matrix of pixels, copying it, adding noise to pixels in that matrix, and presenting that new stimulus for that trial.我设想通过将图像作为像素矩阵读取,复制它,向该矩阵中的像素添加噪声,并为该试验呈现新的刺激来实现这一工作。 The imread function I was using to read in my images does not appear able to do this - does anyone have any suggestions?我用来读取图像的 imread 函数似乎无法做到这一点 - 有没有人有任何建议?

I have demonstrated how one can read an image with cv2 and apply varying levels of noise to it.我已经演示了如何使用cv2读取图像并cv2应用不同级别的噪声。 The noise addition does not modify the original image.添加噪声不会修改原始图像。 As @Michael MacAskill wrote in his comments, you can apply noise to an image with a single vector operation.正如@Michael MacAskill 在他的评论中所写的那样,您可以使用单个矢量操作将噪声应用于图像。 In my answer, I create a Gaussian with mean 1 and the same shape as the image, and I multiply it against the image.在我的回答中,我创建了一个均值为 1 且形状与图像相同的高斯,并将其与图像相乘。 The level of noise can be increased by increasing the standard deviation of the Gaussian noise distribution.可以通过增加高斯噪声分布​​的标准偏差来增加噪声水平。

import cv2
import matplotlib.pyplot as plt
import numpy as np


def apply_noise(image, scale):
    """Return image with noise.

    Parameters
    ----------
    image : ndarray, image to which noise is applied.
    scale : positive float, standard deviation of Gaussian 
        noise distribution.
    """
    image = np.asarray(image)
    # Create a Gaussian noise array.
    noise = np.random.normal(loc=1.0, scale=scale, size=image.shape)
    # Apply the noise array.
    noisy = image * noise
    # Tranform to integer type.
    noisy = noisy.astype(np.int32)
    # Clip the values to RGB bounds.
    noisy = noisy.clip(0, 255)
    return noisy

I downloaded a sample image using我下载了一个示例图像使用

wget -qO "astronaut.jpg" https://live.staticflickr.com/8674/16504233985_9f1060624e_q_d.jpg

and here are sample results.这是示例结果。 The original image:原图:

img = cv2.imread("astronaut.jpg")
# Transform from BGR to RGB
img = img[..., ::-1]
plt.imshow(img)

宇航员

The image with some noise applied:应用了一些噪声的图像:

img_a = apply_noise(image=img, scale=0.1)
plt.imshow(img_a)
plt.title("Gaussian std. dev. = 0.1")

应用了一些噪音的宇航员

The image with more noise applied:应用了更多噪声的图像:

img_b = apply_noise(image=img, scale=0.5)
plt.imshow(img_b)
plt.title("Gaussian std. dev. = 0.5")

应用更多噪音的宇航员

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

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