-
Notifications
You must be signed in to change notification settings - Fork 451
/
opencv_024.py
44 lines (34 loc) · 1.12 KB
/
opencv_024.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import cv2 as cv
import numpy as np
def add_salt_pepper_noise(image):
h, w = image.shape[:2]
nums = 10000
rows = np.random.randint(0, h, nums, dtype=np.int)
cols = np.random.randint(0, w, nums, dtype=np.int)
for i in range(nums):
if i % 2 == 1:
image[rows[i], cols[i]] = (255, 255, 255)
else:
image[rows[i], cols[i]] = (0, 0, 0)
return image
def gaussian_noise(image):
noise = np.zeros(image.shape, image.dtype)
m = (15, 15, 15)
s = (30, 30, 30)
cv.randn(noise, m, s)
dst = cv.add(image, noise)
cv.imshow("gaussian noise", dst)
return dst
src = cv.imread("./test.png")
h, w = src.shape[:2]
copy = np.copy(src)
copy = add_salt_pepper_noise(copy)
result = np.zeros([h, w*2, 3], dtype=src.dtype)
result[0:h,0:w,:] = src
result[0:h,w:2*w,:] = copy
cv.putText(result, "original image", (10, 30), cv.FONT_HERSHEY_PLAIN, 2.0, (0, 255, 255), 1)
cv.putText(result, "salt pepper image", (w+10, 30), cv.FONT_HERSHEY_PLAIN, 2.0, (0, 255, 255), 1)
cv.imshow("salt pepper noise", result)
cv.imwrite("./result.png", result)
cv.waitKey(0)
cv.destroyAllWindows()