美文网首页
OpenCV+Python基本图像数据操作

OpenCV+Python基本图像数据操作

作者: 音符纸飞机 | 来源:发表于2018-07-04 22:55 被阅读240次

    numpy数组与python内建bytearray互相转换

    import cv2
    import numpy as np
    
    img = cv2.imread("laugh.jpg", cv2.IMREAD_GRAYSCALE)
    print(img.shape)
    print(type(img))       # numpy.ndarray
    byte_array = bytearray(img)
    print(len(byte_array))
    img2 = np.array(byte_array).reshape(720, 180)
    cv2.imshow("reshaped", img2)
    """
    (360, 360)
    129600
    """
    
    
    bytearray reshaped

    获取图像基本属性

    print(img.shape)
    print(img.size)
    print(img.dtype)
    """
    (360, 360)
    129600
    uint8
    """
    

    图像信道的分解与合并

    b,g,r = cv2.split(img)
    img = cv2.merge((b,g,r))
    

    由于图像处理过程中图像深度会变成32或者64,最后要变回uint8,使用下面这个方法

    dst = cv2.convertScaleAbs(img)
    # 该方法做了三件事 1.scaling 2.取绝对值 3. 映射到uint8
    

    相关文章

      网友评论

          本文标题:OpenCV+Python基本图像数据操作

          本文链接:https://www.haomeiwen.com/subject/xegyuftx.html