type()、np.dtype()、np.astype()、np.shape()、Numpy数组的数据类型范围

前言

记录一下小白生涯

1. type()函数:获取变量的数据类型

2. np.dtype()函数:获取数组的数据类型

3. np.astype()函数:转换numpy数组的数据类型

4. isinstance()函数:判断 变量(数组)的类型

        isinstance()函数有两个参数:第一个是待确定类型的数据;第二个是指定一个数据类型。

        根据两个参数返回一个为布尔类型的值,True表示类型一致,False表示类型不一致。

5. np.shape()函数:一般用于读取数组的行、列、通道数

        np.shape[0]:为第一维度的长度(行)

        np.shape[1]:为第二维度的长度(列)

        np.shape[1]:为第二维度的长度(通道数)

6. 代码和现实结果

6.1 代码

import cv2

import numpy as np

img = cv2.imread('E:/Desktop/lenna.png')  # 读取图片路径(不能有中文!)

print('img的数据类型:', type(img))

print('img的大小:', img.shape)

print('img数组的数据类型:', img.dtype)

print("cv2? ", isinstance(img, np.ndarray))

print('img:\n', img)

gray = img.mean(axis=-1)  # 平均值灰度化

print('gray的数据类型:', type(img))

print('gray的大小:', gray.shape)

print('gray数组的数据类型:', gray.dtype)

print("cv2? ", isinstance(gray, np.ndarray))

print('gray:\n', gray)

gray = gray.astype(np.uint8)  # 转换数据类型为unint(0-255)显示图像

print('gray(转化为uint8):\n', gray)

cv2.imshow('img', img)

cv2.imshow('gray', gray)

cv2.waitKey(0)

cv2.destroyAllWindows()

6.2 cv2.imshow打印出来的结果

6.3 print打印出来的结果分析

 

 

7. Numpy数组的数据类型范围

  • 24
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值