关键词搜索

源码搜索 ×
×

python中openCV2/numpy/matplotlib.pylab/PIL使用小例子

发布2021-01-17浏览310次

详情内容

  1. CV2库进行图片操python基础教程
#图片操作
#导入对应的库
import numpy as np
import cv2

#导入对应的图片 imread为图片路径
girls = cv2.imread('./girl.jpg')
#查看图片的数据形状(黑白为2维数据,彩色为3维数据)
girls.shape

#展示图片,窗口名为girl,加载数据为girls
cv2.imshow('girl',girls)
#等待键盘输入,单位毫秒,如果为0 无限等待
cv2.waitKey(0)
#关闭图形窗口
cv2.destroyAllWindows()

#BGR 颜色通道BGR
#PIL RGB  设置为灰色
girls2 = cv2.cvtColor(girls,code = cv2.COLOR_BGR2GRAY)
cv2.imshow('gray',girls2)
cv2.waitKey(0)
cv2.destroyAllWindows()

#图片输出保存为girls_gray.jpg
cv2.imwrite("./girls_gray.jpg",girls2)


#设置图片的大小144,158
girls3 = cv2.resize(girls,dsize = (144,158))
cv2.imshow('min',girls3)
#设置退出条件
while True:
    if ord('q') == cv2.waitKey(3000):
        break

cv2.destroyAllWindows()
    1. 数据操作
    #导入库
    import numpy as np
    
    #设置一个列表
    l = [1,2,3,4,5,6,7]
    #作为array数组
    nd = np.array(l)
    #求和
    nd.sum()
    #标准差
    nd.var()
    #标准差
    nd.std()
    #查看数据类型
    nd.dtype
    #指数 e
    np.exp(3)
    
    a = np.array([1,3,5,7,np.NAN])
    b = a + 10
    #统计学  平均值/中位数/方差/标准差/协方差(两个属性求解)/关联(相关性系数)
    #a和b相关
    np.corrcoef(a,b)
    
    
    nd = np.random.randint(0,1000, size= 100)
    #查找数组nd中少于109的值
    np.argwhere(nd <= 109)
    #直方图,统计数据出现的频次
    np.histogram(nd,bins = 10)
    #保存数据
    np.savetxt('./data.txt',nd)
    #读取文件
    np.loadtxt('./data.txt')
    #获取最小的5个数
    np.partition(nd,kth = 5)[:5]
    #获取最大的5个数
    np.partition(nd,kth = -5)[-5:]
    
    
    #随机生成array数组 4*5
    nd2 = np.random.randint(0,150,size= (4,5))
    #选取数组中的元素
    nd2[1,1]
    #进行切片操作,前三行
    nd2[0: 3]
    #前三行,前三列
    nd2[0:3,0:3]
    
      38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    1. PIL图片操作
    #导入对应库
    from PIL import Image
    #导入对应图片
    girl = Image.open('girls.jpg')
    
    #图片数据为ndarray
    #彩色图片三维,高度/宽度/像素 红绿黄
    girls = np.array(girl)
    #行 列 像素
    girls.shape
    #数据类型
    girls.dtype
    #最大值
    girls.max()
    
    #array转换成image
    girls2 = girls[:,:,::-1]
    Image.fromarray(girls2)
    
    #图片缩小
    Image.fromarray(girls[::5,::5])
    
    #更改颜色
    Image.fromarray(girls[:,:,[1,0,2]])
    
    #图片变为黑白
    Image.fromarray(girls[:,:,0])
    复制代码
    4. matplotlib.pylab图片操作
    
    复制代码
    #导入对应包
    import matplotlib.pylab as plt
    %matplotlib inline
    
    #加载图片
    plt.imshow(girls)
    
    #图片旋转180
    plt.imshow(girls[::-1])
    
    #图片模糊处理
    plt.imshow(girls[::-10,::-10])
    
    # 剪切图片
    girls6 = girls[:,:230]
    plt.imshow(girls6)
    
    #拼接图片
    girls7 = np.concatenate([girls6,girls],axis = 1)
    plt.imshow(girls7)
    复制代码
    5. openCV操作视频
    
    复制代码
    #导入对应库
    import numpy as np
    import cv2
    
    #读取视频
    cap = cv2.VideoCapture('./video.mp4')
    #读取视频中的每一帧数据
    flag,frame = cap.read()
    
    #播放视频画面  一帧
    cv2.imshow('nothing',frame)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    #循环播放每一帧画面
    while cap.isOpened():
    #每一帧数据都读取
        flag,frame = cap.read()
        cv2.imshow('norhing',frame)
    #读取到最后一帧
        if flag == False:
            break
    #输入q为退出
        if ord('q') == cv2.waitKey(5):
            break
    cv2.destroyAllWindows()
    #资源释放
    cap.release
    
    
    #参数为零调取本地摄像头
    # cap = cv2.VideoCapture(0)
    cap = cv2.VideoCapture('./video.mp4')
    
    #调取训练集 https://github.com/opencv/opencv/tree/master/data/haarcascades_cuda
    detector = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
    
    while cap.isOpened():
        flag,frame = cap.read()
        
    #设置红色圆圈圈出人脸
        gray = cv2.cvtColor(frame,code = cv2.COLOR_BGR2BGRA)
        
    #设置对应参数,获得最优识别
        face_zone = detector.detectMultiScale(gray,scaleFactor = 1.3, minNeighbors = 5)
        
    #对应坐标画圆
        for x,y,w,h in face_zone:
            cv2.circle(frame,center = (x + w/https://cdn.jxasp.com:9143/image/2,y + h/https://cdn.jxasp.com:9143/image/2),radius = w/https://cdn.jxasp.com:9143/image/2, color = [0,0,255],thickness = 2)
        
        if flag == False:
            break
            
        cv2.imshow('norhing',frame)
        
        if ord('q') == cv2.waitKey(5):
            break
    cv2.destroyAllWindows()
    cap.release
    
      38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114

    相关技术文章

    点击QQ咨询
    开通会员
    返回顶部
    ×
    微信扫码支付
    微信扫码支付
    确定支付下载
    请使用微信描二维码支付
    ×

    提示信息

    ×

    选择支付方式

    • 微信支付
    • 支付宝付款
    确定支付下载