randint函数解析
import numpy as np
int_a = np.random.randint(5)
int_a = np.random.randint(low=5)
int_a = np.random.randint(1, 5)
int_a = np.random.randint(low=1, high=5)
array_a = np.random.randint(1, 5, (3,2))
array_a = np.random.randint(low=1, high=5, size=(3,2))
array_a = np.random.randint(1, 5, (3,2), np.uint8)
array_a = np.random.randint(low=1, high=5, size=(3,2), dtype=np.uint8)
print(array_a.dtype)
实战1:生成一个随机噪声灰度图像,像素值是范围[0, 256)内的整数,图像形状为(512, 512),并显示图像
代码
import numpy as np
import cv2
img = np.random.randint(0, 256, size=[512, 512], dtype=np.uint8)
cv2.imshow("noise img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
运行结果