-1

I'm trying to add a random noise from uniform distribution between min pixel value and 0.1 times the maximum pixel value to each pixel for each channel of original image.

Here's my code so far:

[in]:

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


# Read image with cv2
image = cv2.imread('example_image.jpg' , 1)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display image
imshow(image_rgb)
# R,G,B channel separation
R, G, B = cv2.split(image_rgb)
# Creating Noise

noise_R = np.random.uniform(R.min(),R.max()*0.1, R.size)
noise_R.shape = (256,256)
noise_G = np.random.uniform(B.min(),B.max()*0.1, G.size)
noise_G.shape = (256,256)
noise_B = np.random.uniform(G.min(), G.max()*0.1, B.size)
noise_B.shape = (256,256)

# Adding noise to each channel separately

R = R + noise_R
G = G + noise_G
B = B + noise_B
rgb_noise = R + G + B

noisy_image = image + rgb_noise

[out]:

ValueError: operands could not be broadcast together with shapes (256,256,3) (256,256) 

I'm getting an ValueError that the array shapes for rgb_noise and image are not equal. I've tried changing the shape of rgb_noise to that of image's but the I get a size error. How to fix it ? Is there any better method ?

Mine
  • 831
  • 1
  • 8
  • 27
  • 3
    Only you can decide if the output looks like you want. – mkrieger1 Oct 22 '19 at 19:19
  • While your target is the same, this is an entirely different question. You are now using a different library and you have removed input / output info. It is OK to *refine* your target, but changing it altogether should be keenly avoided. Please consider restoring the original question and asking a new question if you are not able to cope with the issue you have with this new code. – norok2 Oct 22 '19 at 20:26
  • @norok2 I realized I will use the other code for my project and the code being here might be regarded as plagiarism so I did not want to take the risk. It's my fault really,definitely will not make the same mistake again ! – Mine Oct 22 '19 at 20:42
  • If it is your code, there is no such thing as *self-plagiarim*. As far as the code from others in StackOverflow see [here](https://meta.stackexchange.com/questions/12527/do-i-have-to-worry-about-copyright-issues-for-code-posted-on-stack-overflow). – norok2 Oct 22 '19 at 20:49

2 Answers2

1

Your solution is a bit verbose, and could be made more compact. However, the reason why you do not get white-ish noise is that you compute your red channel differently from the other two.

Changing this:

noise_R = np.random.uniform(R_min,R_max*0.3, image_G.size)

to this:

noise_R = np.random.uniform(R_min,R_max*0.1, image_R.size)
norok2
  • 25,683
  • 4
  • 73
  • 99
1

You can be simplistic and add the noise by only the numpy array.

import numpy
import matplotlib.pyplot as plt
import cv2

Look, plotting the image will only work good with jupyter notebooks. Do cv2.imshow() for other IDEs.

1) Have your Image

img = cv2.imread('path').astype(np.uint0)

2) Make a random noise

r, g, b = img.shape
noise = np.random.randint(0,255,r*g*b).reshape(r,g,b)

3) Blend them

image_with_noise = cv2.addWeighted(img,0.5,noise,0.5,0)

You can adjust the value of alpha and beta values.

There you have a noisy image!

AAYUSH SHAH
  • 131
  • 2
  • 6
  • Which part does create random noise from uniform distribution between min pixel value and 0.1 times the maximum pixel value ? – Mine Oct 22 '19 at 19:59
  • 1
    @moli That is the second step... This was just an example, you can change the values bu your own... That lies between 0 to 255 – AAYUSH SHAH Oct 23 '19 at 06:35
  • @AAYUH SHAH in "addWeighted" part do you give half/half weights to image and the noise ? What does the "0" denote that is in addWeighted part – Mine Oct 23 '19 at 09:34
  • 1
    @moli The fifth parameter which I have given in the addweighted() is the Gamma parameter. The gamma essentially adjusts the Brightness of the image in simple terms. But in a technical way, brightness and gamma are a bit different. Here we only need the image with noise. Then, we don't need to have the gamma and there, it is 0. It is the compulsory parameter. – AAYUSH SHAH Oct 24 '19 at 06:28