opencv中numpy的使用

# import numpy as np  # 导入numpy模块
# list = [1, 2, 3]  # 列表
# # 创建浮点型数组
# n1 = np.array(list, dtype=np.float_)
# # 或者
# n1 = np.array(list, dtype=float)
# print(n1)
# print(n1.dtype)
# print(type(n1[0]))

# import numpy as np
# nd1 = [1, 2, 3]
# nd2 = np.array(nd1, ndmin=3) #三维数组
# print(nd2)

# import numpy as np
# n = np.empty([2, 3])  # 2*3的空数组
# print(n)

# import numpy as np
# n = np.ones((3, 3), np.uint8)  # 元素均为1,OpenCV经常使用该方法创建纯掩膜
# print(n)

# import numpy as np
# n1=np.array([1,2,3])  #创建一维数组
# print(n1[0])  # 索引,索引左闭右开

# #纯黑色图像
# import cv2
# import numpy as np
#
# width = 200  # 图像的宽
# height = 100  # 图像的高
# # 创建指定宽高、单通道、像素值都为0的图像
# img = np.zeros((height, width), np.uint8)
# cv2.imshow("img", img)  # 展示图像
# cv2.waitKey()  # 按下任何键盘按键后
# cv2.destroyAllWindows()  # 释放所有窗体

# #创建纯白图像
# import cv2
# import numpy as np
#
# width = 200  # 图像的宽
# height = 100  # 图像的高
# # 创建指定宽高、单通道、像素值都为1的图像
# img = np.ones((height, width), np.uint8) * 255
# cv2.imshow("img", img)  # 展示图像
# cv2.waitKey()  # 按下任何键盘按键后
# cv2.destroyAllWindows()  # 释放所有窗体

# OpenCV中彩色图像默认为BGR格式,彩色图像的第三个索引表示的就是蓝绿红三种颜色的分量
# 创建彩色图像数组时要将数组创建成三维数组,然后复制出三个副本,三个副本分别修改最后一个索引代表的元素值,索引0表示蓝色分量,索引1表示绿色分量,索引2表示红色分量
# import cv2
# import numpy as np
#
# width = 200  # 图像的宽
# height = 100  # 图像的高
# # 创建指定宽高、3通道、像素值都为0的图像
# img = np.zeros((height, width, 3), np.uint8)
# blue = img.copy()  # 复制图像
# blue[:, :, 0] = 255  # 1通道所有像素都为255
# green = img.copy()
# green[:, :, 1] = 255  # 2通道所有像素都为255
# red = img.copy()
# red[:, :, 2] = 255  # 3通道所有像素都为255
# cv2.imshow("blue", blue)  # 展示图像
# cv2.imshow("green", green)
# cv2.imshow("red", red)
# cv2.waitKey()  # 按下任何键盘按键后
# cv2.destroyAllWindows()  # 释放所有窗体

# 生成随机像素
# import cv2
# import numpy as np
#
#
# width = 200  # 图像的宽
# height = 100  # 图像的高
# # 创建指定宽高、单通道、随机像素值的图像,随机值在0~256之间,数字为无符号8位格式
# img = np.random.randint(256, size=(height, width), dtype=np.uint8)
# cv2.imshow("img", img)  # 展示图像
# cv2.waitKey()  # 按下任何键盘按键后
# cv2.destroyAllWindows()  # 释放所有窗体

# 图像的拼接
# hstack可以对数组进行水平拼接,vstack可以对数组进行垂直拼接
# import cv2
# import numpy as np
#
# img = cv2.imreadqq.jpg")  # 读取原始图像
# img_h = np.hstack((img, img))  # 水平拼接两个图像
# img_v = np.vstack((img, img))  # 垂直拼接两个图像
# cv2.imshow("img_h", img_h)  # 展示拼接之后的效果
# cv2.imshow("img_v", img_v)
# cv2.waitKey()  # 按下任何键盘按键后
# cv2.destroyAllWindows()  # 释放所有窗体
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值