Issue
I am trying to add Gaussian noise to my image using opencv-python. I have created the noise function but adding the noise function to the image is producing unexpected results.
I have created a noise function using normal Gaussian distribution from the numpy.random. Then after resizing the noise function I added it to my image.I tried printing the array. There sum is in the interval [0, 255] but then also parts of the image are washed out. I also tried printing the data types of the array. The initial was uint8 while later was float64 (I don't think that will make any difference).
import numpy as np
import cv2
fast = cv2.imread('Fast8.jpg', 0)
row, col = fast.shape
noise = np.random.normal(0, 1, (row, col))
fast = fast + noise
cv2.namedWindow('Noisy', cv2.WINDOW_NORMAL)
cv2.imshow('Noisy', fast)
cv2.waitKey(0)
cv2.destroyAllWindows()
In the result of the above code I am getting a washed out image with only some areas slightly visible.
Solution
You are loading the image as uint8, but when summing float you get a float out. In order to see the result you need to cast it as int again.
Try this
cv2.imshow('Noisy', fast.astype(np.uint8))
Of course you will get a change in value only when the noise is big enough to make the pixel value jump from one integer to the next.
As an alternative you could work in the range [0,1] or [-1, 1] using scikit-image, which often uses these other conventions.
[Extra clarification] cv2.imshow works as explained in this stack overflow thread -> LINK So, you should decide if using float images in the range [0, 1] or uint8 images in the range [0, 255] or uint16/uint32 images with a bigger integer range. Since stochastic functions often generate on a small float range around 0, I suggest you to convert your image by dividing it by 255.0 to get it in the range [0, 1] float and work from there.
Answered By - steve3nto
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.