python – How to change noise color in an image?

python – How to change noise color in an image?

Here is one way to do that in Python/OpenCV. Create a grayscale noise image using numpy as a mask. Create a colored image. Do bitwise_and to combine omg and blue using the noise as a mask.

Input:

enter

import cv2
import numpy as np

# load image and get dimensions
img = cv2.imread(zelda1.jpg)
hh, ww = img.shape[:2]

# create noise image (multiplier increase noise in result)
noise = (800*np.random.random((hh,ww))).clip(0,255).astype(np.uint8)

# make blue image
blue = np.full_like(img, (255,0,0))

# combine img and blue using noise as mask
img_masked = cv2.bitwise_and(img, img, mask=255-noise)
blue_masked = cv2.bitwise_and(blue, blue, mask=noise)
result = cv2.add(img_masked, blue_masked)
    
# write result to disk
cv2.imwrite(noise.jpg, noise)
cv2.imwrite(blue.jpg, blue)
cv2.imwrite(zelda1_blue_noise.jpg, result)

# display it
cv2.imshow(noise, noise)
cv2.imshow(blue, blue)
cv2.imshow(result, result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Noise image:

enter

Blue image:

enter

Result:

enter

python – How to change noise color in an image?

Leave a Reply

Your email address will not be published.