【OpenCV 例程 300篇】257.OpenCV 生成随机矩阵

『youcans 的 OpenCV 例程300篇 - 总目录』


【youcans 的 OpenCV 例程 300篇】257. OpenCV 生成随机矩阵


3.2 OpenCV 创建随机图像

OpenCV 中提供了 cv.randncv.randu 函数生成随机数矩阵,也可以用于创建随机图像。

函数 cv.randn 生成的矩阵服从正态分布,函数 cv.randu 生成的矩阵服从均匀分布

函数说明:

cv.randn(dst, mean, stddev[, ]) → dst,生成正态分布矩阵

cv.randu(dst, low, high [, ]) → dst,生成均匀分布矩阵

cv.randShuffle(dst[, iterFactor=1.]) → dst,随机打乱一维数组 dst 的数组元素

参数说明:

  • dst:输入/输出数组,定义多维数组的形状,通道数 1~4
  • mean:生成正态分布矩阵的均值
  • stddev:生成正态分布矩阵的标准差,向量或方阵
  • low:生成均匀分布矩阵的最小值(lower boundary),包括 low
  • high:生成均匀分布矩阵的上限值(upper boundary),不包括 high
  • iterFactor:随机交换次数的比例

注意事项:

  1. 使用 cv.randncv.randu 函数,要预先定义 Numpy 数组 dst 。
  2. 当定义的 dst 为单通道时,参数 mean、stddev、low、high 是标量;当 dst 为多通道(如 3通道或4 通道时),参数 mean、stddev、low、high 是数组,数组长度等于通道数,数组的数据结构是列表或元组。
  3. 使用 cv.randu 函数生成均匀分布的随机数,随机数的取值范围是 [low, high)。

基本例程:

    # 1.14+ OpenCV 创建随机图像
    import cv2 as cv
    # (1) 通过宽度高度值创建多维数组
    h, w, ch = 20, 30, 3  # 行/高度, 列/宽度, 通道数
    imgEmpty = np.empty((h, w, ch), np.uint8)  # 创建空白数组
    imgBlack = np.zeros((h, w, ch), np.uint8)  # 创建黑色图像 RGB=0
    imgWhite = np.ones((h, w, ch), np.uint8) * 255  # 创建白色图像 RGB=255
    # (2) 通过 OpenCV 函数生成随机数矩阵
    img = np.empty((20, 30), np.uint8)
    imgRandN = cv.randn(img, mean=128, stddev=64)  # 正态分布 randn
    print(imgRandN.min(), imgRandN.max(), imgRandN.mean())  # 0~255
    img = np.empty((20, 30), np.uint8)
    imgRandU = cv.randu(img, low=1, high=10)  # 均匀分布 randu,[low, high)
    print(imgRandU.min(), imgRandU.max(), imgRandU.mean())  # 1~9
    # (3) 矩阵转向量
    dst1 = cv.reduce(imgRandU, 0, cv.REDUCE_AVG)
    dst2 = cv.reduce(imgRandU, 0, cv.REDUCE_MAX)
    dst3 = cv.reduce(imgRandU, 0, cv.REDUCE_MIN)
    print(dst1.shape, dst2.shape, dst3.shape)  # (1, 30) (1, 30) (1, 30)
    print(dst1)  # [[5 4 5 5 5 6 5 5 6 4 5 4 5 5 5 6 5 5 6 5 5 5 5 4 4 5 5 5 5 6]]
    print(dst2)  # [[9 8 9 9 9 9 9 9 9 8 9 9 8 9 9 9 9 9 9 9 9 9 9 9 9 9 8 9 8 9]]
    print(dst3)  # [[1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 3]]

    plt.figure(figsize=(9, 6))
    plt.subplot(221), plt.title("1. Black image"), plt.axis('off')
    plt.imshow(cv2.cvtColor(imgBlack, cv2.COLOR_BGR2RGB))
    plt.subplot(222), plt.title("2. White image"), plt.axis('off')
    plt.imshow(cv2.cvtColor(imgWhite, cv2.COLOR_BGR2RGB))
    plt.subplot(223), plt.title("3. Normal rand"), plt.axis('off')
    plt.imshow(imgRandN, cmap='gray', vmin=0, vmax=255)
    plt.subplot(224), plt.title("4. Uniform  rand"), plt.axis('off')
    plt.imshow(imgRandU, cmap='gray')
    plt.show()

本例程的运行结果如下

0 255 128.6
1 9 5.1
(1, 30) (1, 30) (1, 30)
[[5 4 5 5 5 6 5 5 6 4 5 4 5 5 5 6 5 5 6 5 5 5 5 4 4 5 5 5 5 6]]
[[9 8 9 9 9 9 9 9 9 8 9 9 8 9 9 9 9 9 9 9 9 9 9 9 9 9 8 9 8 9]]
[[1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 3]]

在这里插入图片描述


Numpy 创建随机图像

OpenCV 中图像对象的数据结构是 ndarray 多维数组,因此可以用 Numpy 创建多维数组来生成图像。特别对于空白、黑色、白色、随机等特殊图像,用 Numpy 创建图像非常方便。

Numpy 可以使用 np.zeros() 等方法创建指定大小、类型的图像对象,也可以使用 np.zeros_like() 等方法创建与已有图像大小、类型相同的新图像。

函数说明:

numpy.empty(shape[, dtype, order]) # 返回一个指定形状和类型的空数组

numpy.zeros(shape[, dtype, order]) # 返回一个指定形状和类型的全零数组

numpy.ones(shape[, dtype, order]) # 返回一个指定形状和类型的全一数组

numpy.empty_like(img) # 返回一个与图像 img 形状和类型相同的空数组

numpy.zeros_like(img) # 返回一个与图像 img 形状和类型相同的全零数组

numpy.ones_like(img) # 返回一个与图像 img 形状和类型相同的全一数组

参数说明:

  • shape:整型元组,定义返回多维数组的形状
  • dtype:数据类型,定义返回多维数组的类型,可选项
  • img:ndarray 多维数组,表示一个灰度或彩色图像

基本例程:

    # 1.14 Numpy 创建图像
    # 创建彩色图像(RGB)
    # (1) 通过宽度高度值创建多维数组
    height, width, channels = 400, 300, 3  # 行/高度, 列/宽度, 通道数
    imgEmpty = np.empty((height, width, channels), np.uint8)  # 创建空白数组
    imgBlack = np.zeros((height, width, channels), np.uint8)  # 创建黑色图像 RGB=0
    imgWhite = np.ones((height, width, channels), np.uint8) * 255  # 创建白色图像 RGB=255
    # (2) 创建彩色随机图像 RGB=random
    import os
    randomByteArray = bytearray(os.urandom(height * width * channels))
    flatNumpyArray = np.array(randomByteArray)
    imgRGBRand = flatNumpyArray.reshape(width, height, channels)
    # (3) 创建灰度图像
    imgGrayWhite = np.ones((height, width), np.uint8) * 255  # 创建白色图像 Gray=255
    imgGrayBlack = np.zeros((height, width), np.uint8)  # 创建黑色图像 Gray=0
    imgGrayEye = np.eye(width)  # 创建对角线元素为1 的单位矩阵
    randomByteArray = bytearray(os.urandom(height*width))
    flatNumpyArray = np.array(randomByteArray)
    imgGrayRand = flatNumpyArray.reshape(height, width)  # 创建灰度随机图像 Gray=random
    
    print("Shape of image: gray {}, RGB {}".format(imgGrayRand.shape, imgRGBRand.shape))
    cv2.imshow("DemoGray", imgGrayRand)  # 在窗口显示 灰度随机图像
    cv2.imshow("DemoRGB", imgRGBRand)  # 在窗口显示 彩色随机图像
    cv2.imshow("DemoBlack", imgBlack)  # 在窗口显示 黑色图像
    key = cv2.waitKey(0)  # 等待按键命令

【本节完】

版权声明:
youcans@xupt 原创作品,转载必须标注原文链接:(https://blog.csdn.net/youcans/article/details/128569524)
Copyright 2022 youcans, XUPT
Crated:2023-1-5

猜你喜欢

转载自blog.csdn.net/youcans/article/details/128569524
今日推荐