• OpenCV使用教程-读取图像imread使用说明


    1、方法说明:

    import cv2 as cv
    img1 =cv.imread(filename[, flags])
    
    • 1
    • 2
    参数说明
    filename图片路径地址
    flags读取图片方式

    2、目前支持的文件格式
    . - Windows bitmaps - *.bmp, *. dib (always supported)
    . - JPEG files - *.jpeg, *.jpg, *.jpe (see the Note section)
    . - JPEG 2000 files - *.jp2 (see the Note section)
    . - Portable Network Graphics - *.png (see the Note section)
    . - WebP - *. webp (see the Note section)
    . - Portable image format - *. pbm, *. pgm, *. ppm *. pxm, *. pnm (always supported)
    . - PFM files - *. pfm (see the Note section)
    . - Sun rasters - *. sr, *. ras (always supported)
    . - TIFF files - *.tiff, *.tif (see the Note section)
    . - OpenEXR Image files - *. exr (see the Note section)
    . - Radiance HDR - *. hdr, *. pic (always supported)
    3、flags参数说明:

    flags参数代号功能
    cv2.IMREAD_COLOR1将图像转换 3 通道BGR彩色图像,默认方式
    cv2.IMREAD_GRAYSCALE0将图像转换为单通道灰度图像
    cv2.IMREAD_UNCHANGED-1按原样返回加载Alpha通道的图像
    cv2.IMREAD_ANYDEPTH2在输入具有相应深度时返回16位/ 32位图像,否则将其转换为8位
    cv2.IMREAD_ANYCOLOR4以任何可能的颜色格式读取图像

    注意:
    (1)imread( )方法默认读取的格式是BGR,不是我们熟悉的RGB格式哦!

    python常用第三方包图像读取格式
    OpenCVBGR
    PILRGB
    PyQtRGB
    matplotlibRGB

    (2)OpenCV 读取图像文件,返回值是一个【numpy.array】多维数组,即三阶矩阵;若没有读取到对应的图片不会报错,而是会返回一个空矩阵;
    (3)filename路径中不支持中文和空格,如果生产环境需要,可以考虑用imdecode( )进行处理;

    4、案例教程:

    # 1 图像的读取【BGR】
    import matplotlib.pyplot as plt
    import cv2 as cv
    
    # 读取文件的路径
    imgFile = "./sources/cyq.jpg"
    
    # flags=1 读取彩色图像(BGR),即flags=cv.IMREAD_COLOR
    img = cv.imread(imgFile, flags=cv.IMREAD_COLOR)
    
    # 图片展示
    plt.imshow(img)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    展示结果:

    原图
    读取结果
    # 2 图像的读取【BGR=>RGB】
    import matplotlib.pyplot as plt
    import cv2 as cv
    
    # 读取文件的路径
    imgFile = "./sources/cyq.jpg"
    
    # flags=1 读取彩色图像(BGR),即flags=cv.IMREAD_COLOR
    img = cv.imread(imgFile, flags=cv.IMREAD_COLOR)
    
    # 由于matplotlib的图片像素格式为(RGB),因此需要做格式转化;
    img=cv.cvtColor(img,cv.COLOR_BGR2RGB)
    
    # 图片展示
    plt.imshow(img)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    展示结果:

    原图
    读取结果
    # 3 读取并展示图片,按任意键退出
    import cv2 as cv
    
    def show_image(name,img):
        """
        展示图片,按任意键退出
        :param name: 图片名称 
        :param img: 图片矩阵 
        :return:
        """
        cv.imshow(name,img) #展示图片
        cv.waitKey(0)#等待时间,毫秒级,0表示任意键终止
        cv.destroyWindow(name)
    
    if __name__ == '__main__':
        img=cv.imread("../sources/cyq.jpg", cv.IMREAD_GRAYSCALE)
        # 矩阵形状
        print(img.shape)
        # 像素点个数
        print(img.size)
        # 编码类型
        print(img.dtype)
        # 图片截取
        sub_img=img[0:600,400:1000] # [height,width]
        # 保存图片
        cv.imwrite("../data/陈钰琪.jpg",img)
        # 展示图片,按任意键退出
        show_image("cyq",sub_img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
  • 相关阅读:
    java集合概述
    JS深拷贝和浅拷贝的区别
    <Senior High School Math>: inequality question
    Java 多线程系列Ⅳ(单例模式+阻塞式队列+定时器+线程池)
    微信小程序的养发护发馆客户管理系统
    前端任意修改地图风格颜色
    阶段总结(技术向)
    libtorch c++ 定义全链接网络
    Jwt介绍
    游戏软件开发与应用软件开发有什么不同呢?
  • 原文地址:https://blog.csdn.net/weixin_44894162/article/details/126618073